Compare commits

..

6 Enmetoj

Author SHA1 Message Date
Pierre Neidhardt 63ab330868 Fix files-rm. 2022-09-27 17:06:41 +02:00
Pierre Neidhardt a1c5281a38 Make files-ls more useful. 2022-09-24 10:33:22 +02:00
Pierre Neidhardt ed93813417 Allow adding directories. 2022-09-24 10:33:22 +02:00
Pierre Neidhardt 09ebe83f37 Allow reporting multiple results.
For instance when adding a directory with multiple files.
2022-09-24 10:33:22 +02:00
Pierre Neidhardt a52676d5c3 Ensure ipfs-call URL is a 'character string and not a base-string.
Otherwise it breaks PURI.
2022-09-23 10:24:32 +02:00
Pierre Neidhardt cc65064550 Use default Drakma encoding in url-encode.
This fixes the load of URLs like

    bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/I/m/Van_Gogh_-_Bildnis_der_Mutter_des_K%C3%BCnstlers.jpeg
2022-09-22 12:43:33 +02:00
3 changed files with 47 additions and 15 deletions

View File

@ -5,7 +5,7 @@ CL-IPFS-API²
:cl-ipfs-api² is a pretty simple set of IPFS bindings for Common Lisp, using
the HTTP API for (almost) everything, except for pubsub (which uses the locally
installed go-ipfs program).
It uses Drakma, YASON, and UIOP.
It uses Dexador, YASON, and UIOP.
————————————————————————————————————————
@ -118,5 +118,6 @@ BORING STUFF
————————————————————————————————————————
License is the GNU LGPLv3:
check COPYING.txt (/ipfs/QmR8Rnk5QdXgrXRqmgMLmG5PuHZEjujfa3rfVhPV99TLY7)
Author is Jaidyn Ann <jadedctrl@posteo.at>
Sauce is at https://hak.xwx.moe/jadedctrl/cl-ipfs-api2
Author is Jaidyn Ann <jadedctrl@teknik.io>
Sauce is at https://git.feneas.org/detruota/cl-ipfs-api2
https://github.com/JadedCtrl/cl-ipfs-api2

View File

@ -1,8 +1,8 @@
(defsystem "cl-ipfs-api2"
:version "0.51"
:license "LGPLv3"
:author "Jaidyn Ann <jadedctrl@posteo.at>"
:author "Jaidyn Ann <jadedctrl@teknik.io>"
:description "Bindings for the IPFS HTTP API."
:depends-on (:drakma :yason :arnesi :uiop)
:depends-on (:alexandria :drakma :yason :arnesi :uiop)
:components ((:file "package")
(:file "main")))

View File

@ -32,7 +32,9 @@
(let ((result
(multiple-value-list
(drakma:http-request
(make-call-url call arguments)
;; We ensure the string is of 'character elements and not 'base-char
;; which would break Puri.
(alexandria:copy-array (make-call-url call arguments) :element-type 'character)
:method method
:url-encoder #'ipfs::url-encode
:parameters parameters
@ -46,8 +48,10 @@
(let* ((result (cond ((stringp body) body)
((vectorp body) (flexi-streams:octets-to-string body))))
(result (if (search "application/json" (cdr (assoc :content-type headers)))
(unless (empty-string-p result)
(simplify (yason:parse result :object-as :alist)))
(mapcar (lambda (line)
(simplify (yason:parse line :object-as :alist)))
(delete "" (uiop:split-string result :separator (string #\newline))
:test 'string=))
result)))
(if (eql 200 status-code)
result
@ -83,12 +87,39 @@
;; —————————————————————————————————————
;; ROOT CALLS
(defun parent-directory (path)
(if (uiop:directory-pathname-p path)
(uiop:pathname-parent-directory-pathname path)
(uiop:pathname-directory-pathname path)))
(defun directory->parameters (dir)
(let* ((result '())
(root (uiop:ensure-pathname dir :truenamize t))
(parent (parent-directory root)))
(uiop:collect-sub*directories
root
#'uiop:directory-exists-p
(constantly t)
(lambda (subdirectory)
(setf result
(cons `("file" ,subdirectory
:content-type "application/x-directory"
:filename ,(uiop:native-namestring (uiop:enough-pathname subdirectory parent)))
(append result
(mapcar (lambda (file)
`("file" ,file
:filename ,(uiop:native-namestring (uiop:enough-pathname file parent))))
(uiop:directory-files subdirectory)))))))
result))
;; PATHNAME → (HASH-STRING SIZE-NUMBER) || (NIL STRING)
(defun add (pathname &key (pin 't) (only-hash nil) (cid-version 0))
"Add a file to IPFS, return it's hash.
/ipns/docs.ipfs.io/reference/api/http/#api-v0-add"
(ipfs-call "add" `(("pin" ,pin) ("only-hash" ,only-hash) ("cid-version" ,cid-version))
:parameters `(("file" . ,pathname))))
:parameters (if (uiop:directory-exists-p pathname)
(directory->parameters pathname)
`(("file" . ,pathname)))))
;; STRING :NUMBER :NUMBER → STRING || (NIL STRING)
(defun cat (ipfs-path &key (offset nil) (length nil))
@ -462,7 +493,7 @@
(defun files-ls (&optional (path "/"))
"List directories in local mutable namespace.
/ipns/docs.ipfs.io/reference/api/http/#api-v0-files-ls"
(ipfs-call "files/ls" `(("arg" ,path))))
(ipfs-call "files/ls" `(("arg" ,path) ("long" "true"))))
;; STRING [:BOOLEAN :NUMBER :STRING] → NIL || (NIL STRING)
(defun files-mkdir (path &key (parents nil) (cid-version nil) (hash nil))
@ -496,8 +527,8 @@
;; STRING → ALIST || (NIL STRING)
(defun files-stat (path)
"Get file status.
/ipns/docs.ipfs.io/reference/api/http/#api-v0-files-stat"
"Remove a given file.
/ipns/docs.ipfs.io/reference/api/http/#api-v0-files-rm"
(ipfs-call "files/stat" `(("arg" ,path))))
;; PATHNAME STRING [:NUMBER :BOOLEAN :BOOLEAN :BOOLEAN :NUMBER :BOOLEAN
@ -1102,5 +1133,5 @@ will be passed directly to the files-write function."
(cl-ppcre:regex-replace-all
"%2520" (drakma:url-encode
(cl-ppcre:regex-replace-all " " string "%20")
:utf-8)
drakma:*drakma-default-external-format*)
"%20"))