summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorPiotr Szarmanski2022-09-27 17:41:09 +0200
committerPiotr Szarmanski2022-09-27 17:41:09 +0200
commitb89032fbac3dd7895393b1db19fe1155f435948f (patch)
tree3bb8d45818dc8af7a74e22047ea1d2340f9be7a7 /examples
parente612b3d6658489fed51d6bc9a3c5c09cb60f4b74 (diff)
Add an examples directory and a simple file-encoding system.
Diffstat (limited to 'examples')
-rw-r--r--examples/file/eris-file.asd9
-rw-r--r--examples/file/eris-file.lisp30
-rw-r--r--examples/file/package.lisp2
3 files changed, 41 insertions, 0 deletions
diff --git a/examples/file/eris-file.asd b/examples/file/eris-file.asd
new file mode 100644
index 0000000..c86c956
--- /dev/null
+++ b/examples/file/eris-file.asd
@@ -0,0 +1,9 @@
+(defsystem "eris-file"
+ :name "eris-file"
+ :author "mail@ykonai.net"
+ :license "LGPLv3 or later"
+ :depends-on ("eris")
+ :serial t
+ :components
+ ((:file "package")
+ (:file "eris-file")))
diff --git a/examples/file/eris-file.lisp b/examples/file/eris-file.lisp
new file mode 100644
index 0000000..fdc53ef
--- /dev/null
+++ b/examples/file/eris-file.lisp
@@ -0,0 +1,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))))
diff --git a/examples/file/package.lisp b/examples/file/package.lisp
new file mode 100644
index 0000000..0dc504b
--- /dev/null
+++ b/examples/file/package.lisp
@@ -0,0 +1,2 @@
+(defpackage :eris-file
+ (:use common-lisp))