ipfs.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix ipfs)
  19. #:use-module (json)
  20. #:use-module (srfi srfi-1)
  21. #:use-module (srfi srfi-11)
  22. #:use-module (rnrs io ports)
  23. #:use-module (ice-9 match)
  24. #:use-module (web uri)
  25. #:use-module (web client)
  26. #:use-module (web response)
  27. #:export (%ipfs-base-url
  28. add-data
  29. add-file
  30. content?
  31. content-name
  32. content-hash
  33. content-size
  34. add-empty-directory
  35. add-to-directory
  36. read-contents
  37. publish-name))
  38. ;;; Commentary:
  39. ;;;
  40. ;;; This module implements bindings for the HTTP interface of the IPFS
  41. ;;; gateway, documented here: <https://docs.ipfs.io/reference/api/http/>. It
  42. ;;; allows you to add and retrieve files over IPFS, and a few other things.
  43. ;;;
  44. ;;; Code:
  45. (define %ipfs-base-url
  46. ;; URL of the IPFS gateway.
  47. (make-parameter "http://localhost:5001"))
  48. (define* (call url decode #:optional (method http-post)
  49. #:key body (false-if-404? #t) (headers '()))
  50. "Invoke the endpoint at URL using METHOD. Decode the resulting JSON body
  51. using DECODE, a one-argument procedure that takes an input port; when DECODE
  52. is false, return the input port. When FALSE-IF-404? is true, return #f upon
  53. 404 responses."
  54. (let*-values (((response port)
  55. (method url #:streaming? #t
  56. #:body body
  57. ;; Always pass "Connection: close".
  58. #:keep-alive? #f
  59. #:headers `((connection close)
  60. ,@headers))))
  61. (cond ((= 200 (response-code response))
  62. (if decode
  63. (let ((result (decode port)))
  64. (close-port port)
  65. result)
  66. port))
  67. ((and false-if-404?
  68. (= 404 (response-code response)))
  69. (close-port port)
  70. #f)
  71. (else
  72. (close-port port)
  73. (throw 'ipfs-error url response)))))
  74. ;; Result of a file addition.
  75. (define-json-mapping <content> make-content content?
  76. json->content
  77. (name content-name "Name")
  78. (hash content-hash "Hash")
  79. (bytes content-bytes "Bytes")
  80. (size content-size "Size" string->number))
  81. ;; Result of a 'patch/add-link' operation.
  82. (define-json-mapping <directory> make-directory directory?
  83. json->directory
  84. (hash directory-hash "Hash")
  85. (links directory-links "Links" json->links))
  86. ;; A "link".
  87. (define-json-mapping <link> make-link link?
  88. json->link
  89. (name link-name "Name")
  90. (hash link-hash "Hash")
  91. (size link-size "Size" string->number))
  92. ;; A "binding", also known as a "name".
  93. (define-json-mapping <binding> make-binding binding?
  94. json->binding
  95. (name binding-name "Name")
  96. (value binding-value "Value"))
  97. (define (json->links json)
  98. (match json
  99. (#f '())
  100. (links (map json->link links))))
  101. (define %multipart-boundary
  102. ;; XXX: We might want to find a more reliable boundary.
  103. (string-append (make-string 24 #\-) "2698127afd7425a6"))
  104. (define (bytevector->form-data bv port)
  105. "Write to PORT a 'multipart/form-data' representation of BV."
  106. (display (string-append "--" %multipart-boundary "\r\n"
  107. "Content-Disposition: form-data\r\n"
  108. "Content-Type: application/octet-stream\r\n\r\n")
  109. port)
  110. (put-bytevector port bv)
  111. (display (string-append "\r\n--" %multipart-boundary "--\r\n")
  112. port))
  113. (define* (add-data data #:key (name "file.txt") recursive?)
  114. "Add DATA, a bytevector, to IPFS. Return a content object representing it."
  115. (call (string-append (%ipfs-base-url)
  116. "/api/v0/add?arg=" (uri-encode name)
  117. "&recursive="
  118. (if recursive? "true" "false"))
  119. json->content
  120. #:headers
  121. `((content-type
  122. . (multipart/form-data
  123. (boundary . ,%multipart-boundary))))
  124. #:body
  125. (call-with-bytevector-output-port
  126. (lambda (port)
  127. (bytevector->form-data data port)))))
  128. (define (not-dot? entry)
  129. (not (member entry '("." ".."))))
  130. (define* (add-file file #:key (name (basename file)))
  131. "Add FILE under NAME to the IPFS and return a content object for it."
  132. (add-data (match (call-with-input-file file get-bytevector-all)
  133. ((? eof-object?) #vu8())
  134. (bv bv))
  135. #:name name))
  136. (define* (add-empty-directory #:key (name "directory"))
  137. "Return a content object for an empty directory."
  138. (add-data #vu8() #:recursive? #t #:name name))
  139. (define* (add-to-directory directory file name)
  140. "Add FILE to DIRECTORY under NAME, and return the resulting directory.
  141. DIRECTORY and FILE must be hashes identifying objects in the IPFS store."
  142. (call (string-append (%ipfs-base-url)
  143. "/api/v0/object/patch/add-link?arg="
  144. (uri-encode directory)
  145. "&arg=" (uri-encode name) "&arg=" (uri-encode file)
  146. "&create=true")
  147. json->directory))
  148. (define* (read-contents object #:key offset length)
  149. "Return an input port to read the content of OBJECT from."
  150. (call (string-append (%ipfs-base-url)
  151. "/api/v0/cat?arg=" object)
  152. #f))
  153. (define* (publish-name object)
  154. "Publish OBJECT under the current peer ID."
  155. (call (string-append (%ipfs-base-url)
  156. "/api/v0/name/publish?arg=" object)
  157. json->binding))