diff options
| author | Jakob L. Kreuze | 2023-08-05 14:16:02 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze | 2023-08-05 14:16:02 -0400 |
| commit | fd46852736c377bf8f161fab7a422adee25b9440 (patch) | |
| tree | e596e5650d3d92e8262e29b43b21c5b5110c868f /haunt/jakob | |
| parent | 78a88bcb9096f0622d23c64064128f440f53b704 (diff) | |
[dynamic] Re-implement gallery using SSR
Diffstat (limited to 'haunt/jakob')
| -rw-r--r-- | haunt/jakob/dynamic/capabilities/common.scm | 16 | ||||
| -rw-r--r-- | haunt/jakob/dynamic/capabilities/gallery.scm | 82 |
2 files changed, 44 insertions, 54 deletions
diff --git a/haunt/jakob/dynamic/capabilities/common.scm b/haunt/jakob/dynamic/capabilities/common.scm index 4f6d3d9..0e7d7b8 100644 --- a/haunt/jakob/dynamic/capabilities/common.scm +++ b/haunt/jakob/dynamic/capabilities/common.scm @@ -1,3 +1,19 @@ +;;; Copyright © 2019 - 2023 Jakob L. Kreuze <zerodaysfordays@sdf.org> +;;; +;;; This program is free software; you can redistribute it and/or +;;; modify it under the terms of the GNU 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 +;;; General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see +;;; <http://www.gnu.org/licenses/>. + (define-module (jakob dynamic capabilities common) #:use-module (jakob dynamic util) #:use-module (json) diff --git a/haunt/jakob/dynamic/capabilities/gallery.scm b/haunt/jakob/dynamic/capabilities/gallery.scm index f0c5b66..591335e 100644 --- a/haunt/jakob/dynamic/capabilities/gallery.scm +++ b/haunt/jakob/dynamic/capabilities/gallery.scm @@ -15,15 +15,15 @@ ;;; <http://www.gnu.org/licenses/>. (define-module (jakob dynamic capabilities gallery) - #:use-module (ice-9 binary-ports) - #:use-module (ice-9 ftw) + #:use-module (haunt html) #:use-module (ice-9 match) #:use-module (jakob dynamic database) #:use-module (jakob dynamic errors) #:use-module (jakob dynamic util) - #:use-module (json) + #:use-module (jakob theme) #:use-module (squee) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-11) #:use-module (web request) #:use-module (web response) #:use-module (web uri) @@ -45,61 +45,35 @@ (exec-query conn "SELECT * FROM images WHERE vanity = $1" (list code)))))) -(define (get-gallery-images code) - "Handler for enumerating the image in a gallery." - (define (format-image image) - (match image - ((title filename thumbnail datetime) - `((title . ,title) - (filename . ,filename) - (thumbnail . ,thumbnail) - (datetime . ,datetime))))) - (let* ((images (exec-query conn "SELECT title, filename, thumb_filename, datetime FROM images WHERE vanity = $1" (list code)))) - (list->vector (map format-image images)))) - -(define (get-gallery-info code) - "Handler for enumerating the image in a gallery." - (define (format-gallery info) - (match info - ((title description datetime) - `((title . ,title) - (description . ,description) - (datetime . ,datetime))))) - (let* ((info (exec-query conn "SELECT title, description, datetime FROM galleries WHERE vanity = $1" (list code)))) - (format-gallery (car info)))) +(define (render-gallery code) + (define info + (first + (exec-query conn "SELECT title, description, datetime FROM galleries WHERE vanity = $1" (list code)))) + (define images + (exec-query conn "SELECT title, filename, thumb_filename, datetime FROM images WHERE vanity = $1" (list code))) + (match info + ((title description datetime) + `(div (@ (id "gallery-container")) + (h1 ,title) + (h3 ,description) + ,(map (lambda (image) + (match image + ((title filename thumbnail datetime) + `(a (@ (href ,(format #f "/static-ext/~a" filename))) + (img (@ (src ,(format #f "/static-ext/~a" thumbnail)) + (alt ,title) + (title ,(format #f "~a - ~a" title datetime)))))))) + images))))) (define (get-gallery request body) (let* ((query-string (uri-query (request-uri request))) (params (if query-string (decode-form query-string) '())) - (code (car (assoc-ref params "g")))) + (code (if (assoc-ref params "g") + (car (assoc-ref params "g")) + (panic "no gallery code provided")))) (unless (valid-gallery-code code) (panic "invalid gallery code")) - (values '((content-type . (application/json))) - (scm->json-string `((info . ,(get-gallery-info code)) - (images . ,(get-gallery-images code))))))) - -(define (image-exists? file-name) - (define (string/= a b) (not (string= a b))) - (and (string/= file-name ".") - (string/= file-name "..") - (member file-name (scandir (%gallery-image-directory))))) - -(define (read-image file-name) - (let* ((ext (string-downcase (last (string-split file-name #\.)))) - (mime (cond ((string= ext "jpg") 'image/jpeg) - ((string= ext "png") 'image/png) - (else (error "Unknown MIME type."))))) - (values `((content-type . (,mime))) - (call-with-input-file (format #f "~a/~a" (%gallery-image-directory) file-name) - (lambda (port) - (get-bytevector-all port)))))) - -(define (get-image request body) - (let* ((query-string (uri-query (request-uri request))) - (params (if query-string - (decode-form query-string) - '())) - (file-name (car (assoc-ref params "name")))) - (unless (image-exists? file-name) (panic "invalid filename")) - (read-image file-name))) + (values '((content-type . (text/html))) + (sxml->html-string + (theme #:title "Photo Gallery" #:content (render-gallery code)))))) |