summaryrefslogtreecommitdiff
path: root/examples/file/eris-file.lisp
blob: fdc53efb249a1345f0b97fa56f2142dd90f17853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(in-package :eris-file)
(defvar *eris-directory* "/tmp/")

(defun output-to-file (block reference)
  (let* ((base32 (eris:bytes-to-base32-unpadded reference))
         (file (concatenate 'string *eris-directory* base32)))
    (unless (probe-file file)
      (alexandria:write-byte-vector-into-file block file))))

(defun input-from-file (reference)
  (let* ((base32 (eris:bytes-to-base32-unpadded reference))
         (file (concatenate 'string *eris-directory* base32)))
    (if (probe-file file)
      (alexandria:read-file-into-byte-vector file)
      nil)))

(defun encode-file (input-file &optional (output-directory *eris-directory*))
  (let ((*eris-directory* output-directory))
    (with-open-file (file input-file :direction :input :element-type '(unsigned-byte 8))
      (eris:read-capability-to-urn
       (eris:eris-encode file
                         (if (< (file-length file) (* 1024 16)) 1024 eris:32kib)
                         #'output-to-file
                         :hash-output nil)))))

(defun decode-file (urn output-file &optional (output-directory *eris-directory*))
  (let ((*eris-directory* output-directory))
    (with-open-file (file output-file :direction :output :element-type '(unsigned-byte 8))
      (alexandria:copy-stream (eris:eris-decode (eris:urn-to-read-capability urn) #'input-from-file)
                              file))))