ipfsregistry 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/bin/bash
  2. #
  3. # An IPFS Merkle DAG hash database and address manager,
  4. #
  5. # Since I don't know if IPFS keeps a registry of all the DAGs and hashes I've
  6. # ever accessed through it, I will do this manually as an "address book."
  7. # Think of it like a bookmark manager of browsers, but on the CLI.
  8. #
  9. # EDIT: as it turns out, there's some sort of bookkeeping on this, but it only
  10. # works for pinned files (available as local storage). Also, it presents no
  11. # information on what that file might be, not even if it's a file at all
  12. # (as opposed to a folder). The command is:
  13. #
  14. # ipfs pin ls
  15. #
  16. # As of current, you're better off "bookmarking" directories instead of single
  17. # files. We will index these using the `ipfs ls` command.
  18. #
  19. # You can now tag the hash so as to make it more searchable. Separate tags with
  20. # spaces. Also, so as to better categorize the kind of content you bookmark,
  21. # you must now supply a filetype MIME to the entry. This is free text and can
  22. # easily be spoofed, but at least helps you to categorize the content. To be
  23. # safe, always remember to check incoming files with the GNU `file' utility.
  24. #
  25. # Licensed as Free Software under the terms of the GNU GPL v3:
  26. #
  27. # Copyright (C) 2016 - Klaus Zimmermann
  28. #
  29. # This program is free software: you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License as published by
  31. # the Free Software Foundation, either version 3 of the License, or
  32. # (at your option) any later version.
  33. #
  34. # This program is distributed in the hope that it will be useful,
  35. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. # GNU General Public License for more details.
  38. #
  39. # You should have received a copy of the GNU General Public License
  40. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  41. #
  42. # are we using mysql or sqlite3? (Others are unsupported yet)
  43. BACKEND="sqlite3"
  44. # change this value to suit your current sqlite3 configuration:
  45. DB="$HOME/.ipfslinks.db"
  46. # change these values to suit your current MySQL configuration:
  47. MYSQL_USER=""
  48. MYSQL_PASSWORD=""
  49. MYSQL_SCHEMA=""
  50. if [[ "$BACKEND" == "mysql" ]]
  51. then
  52. if [[ -z "$MYSQL_PASSWORD" ]]; then
  53. DB_COMMAND="mysql -u $MYSQL_USER -D $MYSQL_SCHEMA -e"
  54. else
  55. DB_COMMAND="mysql -u $MYSQL_USER -p $MYSQL_PASSWORD -D $MYSQL_SCHEMA -e"
  56. fi
  57. else
  58. DB_COMMAND="sqlite3 $DB"
  59. fi
  60. # -- functions --
  61. create_table() {
  62. if [[ "$BACKEND" == "mysql" ]]
  63. then
  64. $DB_COMMAND "
  65. CREATE TABLE IF NOT EXISTS ipfs_tracker (
  66. id INTEGER PRIMARY KEY AUTO_INCREMENT,
  67. description VARCHAR(512) NOT NULL,
  68. hash VARCHAR(46) NOT NULL,
  69. filetype VARCHAR(12) NOT NULL,
  70. tags MEDIUMTEXT
  71. );
  72. "
  73. else
  74. $DB_COMMAND "
  75. CREATE TABLE IF NOT EXISTS ipfs_tracker (
  76. id INTEGER PRIMARY KEY AUTOINCREMENT,
  77. description TEXT NOT NULL,
  78. hash TEXT NOT NULL,
  79. filetype TEXT NOT NULL,
  80. tags TEXT
  81. );
  82. "
  83. fi
  84. }
  85. insert() {
  86. # Pass a description of the DAG and its hash as arguments.
  87. insert_query="INSERT INTO ipfs_tracker (description, hash, filetype, tags)
  88. VALUES ('$1', '$2', '$3', '$4');"
  89. $DB_COMMAND "$insert_query"
  90. }
  91. search() {
  92. # Pass a search text as the first argument.
  93. results="$($DB_COMMAND "SELECT description, hash, filetype FROM ipfs_tracker
  94. WHERE description LIKE '%$1%'
  95. OR tags LIKE '%$1%'")"
  96. if [[ -n "$results" ]]
  97. then
  98. echo "$results"
  99. else
  100. echo "Token '$1' not found."
  101. fi
  102. }
  103. delete() {
  104. # Pass an id number as the first argument.
  105. $DB_COMMAND "DELETE FROM ipfs_tracker WHERE id=$1" ||
  106. echo "Error: entry not found."
  107. }
  108. show_help() {
  109. echo "$(basename $0) - an IPFS address bookmarker"
  110. echo "USAGE: $(basename $0) COMMAND"
  111. echo "Commands are: 'init', 'register', 'search', 'delete'"
  112. echo "Written by Klaus Zimmermann - https://quitter.se/kzimmermann"
  113. }
  114. # -- /functions --
  115. case "$1" in
  116. "--help" | "-h" )
  117. show_help
  118. exit 0
  119. ;;
  120. "register" )
  121. title=""
  122. dag_hash=""
  123. filetype=""
  124. tags=""
  125. while [[ -z "$title" ]]
  126. do
  127. printf "Type a short description of the entry: "
  128. read title
  129. done
  130. while [[ -z "$dag_hash" ]]
  131. do
  132. printf "Enter the hash of the entry: "
  133. read dag_hash
  134. done
  135. while [[ -z "$filetype" ]]
  136. do
  137. printf "Enter the file type of the entry (jpg, gif, pdf, zip...): "
  138. read filetype
  139. done
  140. while [[ -z "$tags" ]]
  141. do
  142. printf "Type a few tags to aid the searching of this entry later: "
  143. read tags
  144. done
  145. insert "$title" "$dag_hash" "$filetype" "$tags" ||
  146. echo "Error registering '$title'"
  147. exit 0
  148. ;;
  149. "search" )
  150. printf "Enter a string to search for (empty for ALL): "
  151. read token
  152. search "$token"
  153. exit 0
  154. ;;
  155. "delete" )
  156. $DB_COMMAND "SELECT id, description FROM ipfs_tracker"
  157. printf "Enter the id you would like to delete: "
  158. read del_id
  159. delete "$del_id"
  160. exit 0
  161. ;;
  162. "init" )
  163. create_table && echo "All necessary tables initialized."
  164. exit 0
  165. ;;
  166. * )
  167. echo "Unknown option '$1'"
  168. show_help
  169. exit 1
  170. ;;
  171. esac