-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage-local.lisp
60 lines (53 loc) · 2.46 KB
/
storage-local.lisp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: RLGL-SERVER; Base: 10 -*-
;;;
;;; Copyright (C) 2018, 2019, 2020, 2021, 2023 Anthony Green <[email protected]>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.
(in-package :rlgl-server)
;;; MVP storage driver using local storage
(defclass storage/local (storage-backend)
((local-dir :reader local-dir)))
(defmethod initialize-instance :after ((backend storage/local) &key)
"Initialize a local storage backend."
(log:info "Initializing storage/local")
(setf (slot-value backend 'local-dir)
(or (gethash "local-dir" (config backend) "/var/rlgl/docs")))
(let ((filename (str:concat (local-dir backend) "/.key" )))
(if (probe-file filename)
(setf (slot-value backend 'key)
(alexandria:read-file-into-string filename :external-format :latin-1))
(let ((key (rlgl-util:random-base36-string)))
(with-open-file (stream filename
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(format stream key)
(setf (slot-value backend 'key) key))))))
(defmethod delete-document ((backend storage/local) ref)
"Delete a document from local storage."
(delete-file (str:concat (local-dir backend) "/" (string ref))))
(defmethod read-document ((backend storage/local) ref)
"Return an octet vector containing the document."
(alexandria:read-file-into-byte-vector (str:concat (local-dir backend) "/" (string ref))))
(defmethod store-document ((backend storage/local) document)
"Store a document into local storage."
(let ((filename (concatenate 'string "RLGL-" (rlgl-util:random-base36-string))))
(with-open-file (stream (str:concat (local-dir backend) "/" filename)
:direction :output
:if-exists :supersede
:if-does-not-exist :create
:element-type '(unsigned-byte 8))
(write-sequence document stream)
filename)))