ipfs.scm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 (guix base64)
  21. #:use-module ((guix build utils) #:select (dump-port))
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-11)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (rnrs io ports)
  26. #:use-module (rnrs bytevectors)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 ftw)
  29. #:use-module (web uri)
  30. #:use-module (web client)
  31. #:use-module (web response)
  32. #:export (%ipfs-base-url
  33. add-data
  34. add-file
  35. content?
  36. content-name
  37. content-hash
  38. content-size
  39. add-empty-directory
  40. add-to-directory
  41. read-contents
  42. publish-name))
  43. ;;; Commentary:
  44. ;;;
  45. ;;; This module implements bindings for the HTTP interface of the IPFS
  46. ;;; gateway, documented here: <https://docs.ipfs.io/reference/api/http/>. It
  47. ;;; allows you to add and retrieve files over IPFS, and a few other things.
  48. ;;;
  49. ;;; Code:
  50. (define %ipfs-base-url
  51. ;; URL of the IPFS gateway.
  52. (make-parameter "http://localhost:5001"))
  53. (define* (call url decode #:optional (method http-post)
  54. #:key body (false-if-404? #t) (headers '()))
  55. "Invoke the endpoint at URL using METHOD. Decode the resulting JSON body
  56. using DECODE, a one-argument procedure that takes an input port; when DECODE
  57. is false, return the input port. When FALSE-IF-404? is true, return #f upon
  58. 404 responses."
  59. (let*-values (((response port)
  60. (method url #:streaming? #t
  61. #:body body
  62. ;; Always pass "Connection: close".
  63. #:keep-alive? #f
  64. #:headers `((connection close)
  65. ,@headers))))
  66. (cond ((= 200 (response-code response))
  67. (if decode
  68. (let ((result (decode port)))
  69. (close-port port)
  70. result)
  71. port))
  72. ((and false-if-404?
  73. (= 404 (response-code response)))
  74. (close-port port)
  75. #f)
  76. (else
  77. (close-port port)
  78. (throw 'ipfs-error url response)))))
  79. ;; Result of a file addition.
  80. (define-json-mapping <content> make-content content?
  81. json->content
  82. (name content-name "Name")
  83. (hash content-hash "Hash")
  84. (bytes content-bytes "Bytes")
  85. (size content-size "Size" string->number))
  86. ;; Result of a 'patch/add-link' operation.
  87. (define-json-mapping <directory> make-directory directory?
  88. json->directory
  89. (hash directory-hash "Hash")
  90. (links directory-links "Links" json->links))
  91. ;; A "link".
  92. (define-json-mapping <link> make-link link?
  93. json->link
  94. (name link-name "Name")
  95. (hash link-hash "Hash")
  96. (size link-size "Size" string->number))
  97. ;; A "binding", also known as a "name".
  98. (define-json-mapping <binding> make-binding binding?
  99. json->binding
  100. (name binding-name "Name")
  101. (value binding-value "Value"))
  102. (define (json->links json)
  103. (match json
  104. (#f '())
  105. (links (map json->link links))))
  106. (define %multipart-boundary
  107. ;; XXX: We might want to find a more reliable boundary.
  108. (string-append (make-string 24 #\-) "2698127afd7425a6"))
  109. (define (bytevector->form-data bv port)
  110. "Write to PORT a 'multipart/form-data' representation of BV."
  111. (display (string-append "--" %multipart-boundary "\r\n"
  112. "Content-Disposition: form-data\r\n"
  113. "Content-Type: application/octet-stream\r\n\r\n")
  114. port)
  115. (put-bytevector port bv)
  116. (display (string-append "\r\n--" %multipart-boundary "--\r\n")
  117. port))
  118. (define* (add-data data #:key (name "file.txt") recursive?)
  119. "Add DATA, a bytevector, to IPFS. Return a content object representing it."
  120. (call (string-append (%ipfs-base-url)
  121. "/api/v0/add?arg=" (uri-encode name)
  122. "&recursive="
  123. (if recursive? "true" "false"))
  124. json->content
  125. #:headers
  126. `((content-type
  127. . (multipart/form-data
  128. (boundary . ,%multipart-boundary))))
  129. #:body
  130. (call-with-bytevector-output-port
  131. (lambda (port)
  132. (bytevector->form-data data port)))))
  133. (define (not-dot? entry)
  134. (not (member entry '("." ".."))))
  135. (define* (add-file file #:key (name (basename file)))
  136. "Add FILE under NAME to the IPFS and return a content object for it."
  137. (add-data (match (call-with-input-file file get-bytevector-all)
  138. ((? eof-object?) #vu8())
  139. (bv bv))
  140. #:name name))
  141. (define* (add-empty-directory #:key (name "directory"))
  142. "Return a content object for an empty directory."
  143. (add-data #vu8() #:recursive? #t #:name name))
  144. (define* (add-to-directory directory file name)
  145. "Add FILE to DIRECTORY under NAME, and return the resulting directory.
  146. DIRECTORY and FILE must be hashes identifying objects in the IPFS store."
  147. (call (string-append (%ipfs-base-url)
  148. "/api/v0/object/patch/add-link?arg="
  149. (uri-encode directory)
  150. "&arg=" (uri-encode name) "&arg=" (uri-encode file)
  151. "&create=true")
  152. json->directory))
  153. (define* (read-contents object #:key offset length)
  154. "Return an input port to read the content of OBJECT from."
  155. (call (string-append (%ipfs-base-url)
  156. "/api/v0/cat?arg=" object)
  157. #f))
  158. (define* (publish-name object)
  159. "Publish OBJECT under the current peer ID."
  160. (call (string-append (%ipfs-base-url)
  161. "/api/v0/name/publish?arg=" object)
  162. json->binding))