diff --git a/src/storage.lisp b/src/storage.lisp index ae17413..114c47d 100644 --- a/src/storage.lisp +++ b/src/storage.lisp @@ -1,7 +1,8 @@ (in-package #:cl-user) (defpackage #:storage (:use #:cl) - (:export #:directory-exists-p + (:export #:init-storage + #:directory-exists-p #:ensure-directory-exists #:file-exists-p #:get-files-in-directory @@ -16,9 +17,32 @@ #:rename-content-file #:rename-directory #:store-file - #:store-text)) + #:store-text + #:store-with-raw-path + #:store-text-with-raw-path + #:open-text-file-with-raw-path)) (in-package #:storage) +(defun init-storage () + "Copies the initial files into their default places. +This is used as part of the /run-set defroute in web.lisp file." + (ensure-directory-exists "" "pages") + (uiop:copy-file (make-path "" "default-assets" "about") + (make-path "" "pages" "about")) + (uiop:copy-file (make-path "" "default-assets" "contact") + (make-path "" "pages" "contact")) + (uiop:copy-file (make-path "" "default-assets" "home") + (make-path "" "pages" "home")) + (uiop:copy-file (make-path "" "default-assets" "site-logo.png") + (merge-pathnames "static/images/site-logo.png" + ritherdon-archive.config:*application-root*)) + (uiop:copy-file (make-path "" "default-assets" "favicon.png") + (merge-pathnames "static/images/favicon.png" + ritherdon-archive.config:*application-root*)) + (uiop:copy-file (make-path "" "default-assets" "site-wide-snippet") + (merge-pathnames "static/site-wide-snippet" + ritherdon-archive.config:*application-root*))) + (defun directory-exists-p (username directory) "Checks to see if the specified diretory exists. The directories path is returned if it does exist and `NIL' is @@ -153,6 +177,43 @@ Storage path: `*APPLICATION-ROOT*'/storage/`USERNAME'/`SUBDIRECTORY'/`FILENAME' :if-exists :supersede) (format stream "~a~%" data)))) +(defun store-with-raw-path (path data) + "Stores `DATA' at `PATH'. Use when storing data outsite of /storage directory." + (let ((path (ensure-directories-exist + (merge-pathnames path + ritherdon-archive.config::*application-root*)))) + (with-open-file (stream + path + :direction :output + :element-type '(unsigned-byte 8) + :if-does-not-exist :create + :if-exists :supersede) + (write-sequence (slot-value (car data) 'vector) stream)))) + +(defun store-text-with-raw-path (path data) + "Stores the plain text `DATA' at `PATH'. +Use when storing text outside of /storage direcory." + (let ((path (ensure-directories-exist + (merge-pathnames path + ritherdon-archive.config::*application-root*)))) + (with-open-file (stream + path + :direction :output + :if-does-not-exist :create + :if-exists :supersede) + (format stream "~a~%" data)))) + +(defun open-text-file-with-raw-path (file-path) + "Reads the text file stored in the at `PATH'. +Use when reading text outside the /storage directory." + (let ((path (ensure-directories-exist + (merge-pathnames file-path + ritherdon-archive.config::*application-root*)))) + (with-open-file (stream path) + (let ((data (make-string (file-length stream)))) + (read-sequence data stream) + data)))) + ;;; PORTED FROM RAILS-TO-CAVEMAN PROJECT (expect it to be deleted) ;;; =============================================================================