From c42d169d842cfe4094f13a1e0170e988f5029a41 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Sat, 24 Sep 2022 01:20:54 +0100 Subject: [PATCH] begin implementing CRUD routes for /archive section (web.lisp). This is part of an end-of-session commit. I'm currently working on the /create/archive-entry section (most the HTTP POST request). I still need to start the view archive, edit archive and delete archive parts. I've not even looked at the Meilisearch stuff. For now, the aim is to get the website's main database (nera.db) working and then integrate the Meilisearch stuff in after that. --- src/web.lisp | 111 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 107 insertions(+), 4 deletions(-) diff --git a/src/web.lisp b/src/web.lisp index 9fb8450..a0a1e0f 100644 --- a/src/web.lisp +++ b/src/web.lisp @@ -546,7 +546,6 @@ (destructuring-bind (&key title page-content authenticity-token &allow-other-keys) (utils:request-params (lack.request:request-body-parameters ningle:*request*)) - (format t "~a" page-content) (cond ((not (string= authenticity-token (auth:csrf-token))) `(,+forbidden+ (:content-type "text/plain") ("Denied"))) (t (hermetic:auth @@ -636,7 +635,6 @@ (destructuring-bind (&key slug page-content authenticity-token &allow-other-keys) (utils:request-params (lack.request:request-body-parameters ningle:*request*)) - (format t "~a" page-content) (cond ((not (string= authenticity-token (auth:csrf-token))) `(,+forbidden+ (:content-type "text/plain") ("Denied"))) (t (hermetic:auth @@ -791,7 +789,7 @@ (nera:add-storage-file file-name (utils:slugify file-name) (caddr storage-file)) - (utils:create-thumbnail (utils:slugify file-name)) + (utils:create-thumbnail "media" (utils:slugify file-name)) (utils:set-alert "File uploaded.") (redirect "/storage/manage"))) ;; Not Authorised @@ -824,7 +822,7 @@ (nera:add-storage-file (caddr item) (utils:slugify (caddr item)) (cadddr item)) - (utils:create-thumbnail (utils:slugify (caddr item)))) + (utils:create-thumbnail "media" (utils:slugify (caddr item)))) (utils:set-alert "Multi-File upload complete.") (redirect "/storage/manage"))) ;; Not Authorised @@ -890,6 +888,7 @@ (null (nera:get-storage-file :slug slug))) (utils:set-alert "Unable to find entry in database. Deleted file from system.") (storage:remove-file "" "media" slug) + ;; Delete thumbnail if there is one (for image files only) (when (storage:file-exists-p "" "media" (format nil "thumbnail-~a" slug)) (storage:remove-file @@ -916,6 +915,110 @@ (progn (utils:set-alert "You are not authorised to delete page.") (redirect "/login"))))))) +(defroute ("/archive" :method :GET) () + (let ((alert (utils:get-and-reset-alert))) + (render #P"archive.html" + (append (if (hermetic:logged-in-p) + (auth:auth-user-data)) + `(:alert ,alert + :system-data ,(nera:system-data) + :archive-entries ,(nera:get-all-archive-entries)))))) + +(defroute ("/view/archive/:slug" :method :GET) (&key slug) + (format nil "Page not implemented")) + ;; (let ((alert (utils:get-and-reset-alert))) + ;; (if (storage:file-exists-p "" "pages" slug) + ;; (render #P"archive.html" + ;; (append (if (hermetic:logged-in-p) + ;; (auth:auth-user-data)) + ;; `(:alert ,alert + ;; :db-data ,(nera:get-page slug) + ;; :system-data ,(nera:system-data) + ;; :data ,(storage:open-text-file + ;; "" "pages" slug)))) +;; (on-exception *web* 404)))) + +(defroute ("/user/archive" :method :GET) () + (hermetic:auth (:logged-in) + ;; Authorised + (let ((alert (utils:get-and-reset-alert))) + (render "/user/archive.html" + (append (auth:auth-user-data) + `(:alert ,alert + :system-data ,(nera:system-data) + :archive-entries ,(nera:get-all-archive-entries))))) + ;; Not Authorised + (progn + (utils:set-alert "You are not logged in.") + (redirect "/login")))) + +(defroute ("/create/archive-entry" :method :GET) () + (hermetic:auth (:logged-in) + ;; Authorised + (let ((alert (utils:get-and-reset-alert))) + (render "/user/create-archive.html" + (append (auth:auth-user-data) + `(:alert ,alert + :system-data ,(nera:system-data))))) + ;; Not Authorised + (progn + (utils:set-alert "You are not logged in.") + (redirect "/login")))) + +(defroute ("/create/archive-entry" :method :POST) () + (destructuring-bind + (&key title keywords thumbnail-file page-content + authenticity-token &allow-other-keys) + (utils:request-params + (lack.request:request-body-parameters ningle:*request*)) + (step + (cond ((not (string= authenticity-token (auth:csrf-token))) + `(,+forbidden+ (:content-type "text/plain") ("Denied"))) + (t (hermetic:auth + (:logged-in) + ;; Authorised + (cond ((find t (mapcar #'utils:string-is-nil-or-empty? `(,title ,keywords))) + (render + "/user/create-archive.html" + (append (auth:auth-user-data) + `(:alert "Data is missing. Unable to create entry." + :title ,title + :keywords ,keywords + :data ,page-content)))) + + ((not (null (nera:get-archive-entry :title title))) + (render + "/user/create-archive.html" + (append (auth:auth-user-data) + `(:alert "Entry with that title already exists. Unable to create entry." + :title ,title + :keywords ,keywords + :data ,page-content)))) + + ;; Add condition to check thumbnail-file is an image file. + + (t (nera:create-archive-entry + title + (utils:create-timestamp-id) + (utils:slugify title) + ;; Add the 'thumbnail' pre-fix here? + (utils:slugify (cadr thumbnail-file)) ; File Name + (caddr thumbnail-file) ; File Type + (utils:asciify keywords)) + ;; parse info and enter into meilisearch database. + ;; Use placeholder thumbnail if one not detected. + (storage:store-text "" "archive" (utils:slugify title) page-content) + (storage:store-file + "" "archive" (utils:slugify (cadr thumbnail-file)) thumbnail-file) + ;; Add pre-fix option and just overwrite the original file? + (utils:create-thumbnail "archive" (utils:slugify (cadr thumbnail-file))) + (utils:set-alert "Archive entry created. Great Success!") + (storage:remove-file "" "archive" (utils:slugify (cadr thumbnail-file))) + (redirect "/dashboard"))) + ;; Not Authorised + (progn (utils:set-alert "You are not logged in.") + (redirect "/login")))))))) + ;; ;; Error pages