Public archive for the Return to Ritherdon project. https://www.nicolaellisandritherdon.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

123 lines
4.3 KiB

(in-package #:cl-user)
(defpackage #:utils
(:use #:cl
#:caveman2
#:log4cl
#:app-constants)
(:export #:i18n-load
#:_
#:parse-iso-date
#:request-params
#:string-is-nil-or-empty?
#:separate-files-in-web-request
#:set-alert
#:get-alert
#:get-and-reset-alert
#:checkbox-to-bool
#:asciify
#:slugify
#:get-image-dimensions
#:run-bash-command
#:create-thumbnail
#:create-timestamp-id)
(:documentation "Utilities that do not depend on models."))
(in-package #:utils)
(defun asciify (string)
(str:downcase (slug:asciify string)))
(defun slugify (string)
"Turns a string of text into a slug."
(str:downcase (slug:slugify string)))
(defun request-params (request)
(loop :for (key . value) :in request
:collect (let ((*package* (find-package :keyword)))
(read-from-string key))
:collect value))
;; PORT CODE TO NO LONGER CALL THIS VERSION OF THE FUNCTION (MOVED TO VALIDATION PACKAGE).
(defun string-is-nil-or-empty? (string-to-test)
"Tests to see if `STRING-TO-TEST' is empty of just whitespace.
This is essentially the 'IsNullOrWhiteSpace' function I use in C#. It
expands the 'empty string' check to include a check to see if there is
string with just a '(white) space' in it."
(if (or (string= string-to-test " ")
(zerop (length string-to-test))
(null string-to-test))
t
nil))
(defun separate-files-in-web-request (request &optional request-value)
"Creates a new list of 'upload' files from a web `REQUEST'.
You will mostly use this for processing a multi-file upload (HTML)
form. The standard value for the 'name' attribute in (file) input tag
in the HTML form is `CONTENT-FILES' but you can use a different
name. Just specify it in this function's `REQUEST-VALUE' argument."
(loop :for item :in request
if (or (string= "CONTENT-FILES" (car item))
(string= request-value (car item)))
collect item))
(defun set-alert (message)
"Sets the alert `MESSAGE' stored in session, provide info. to users.
The intention is store a `MESSAGE' across a redirect during a HTTP
POST request."
(setf (gethash :alert ningle:*session*) message))
(defun get-alert ()
"Get alert message from session data."
(gethash :alert ningle:*session*))
(defun get-and-reset-alert ()
"Returns the `ALERT' message and clears its content from the session hash."
(let ((message (get-alert)))
(set-alert nil)
message))
(defun checkbox-to-bool (value)
"Converts a HTML Checkbox `VALUE' to a Boolean.
The `VALUE' will either be 'on' or 'off'. 'Boolean' in this instance
is assuming you are using SQLite and need to convert `VALUE' to an
integer/number. If you are needing a traditional Boolean value, DO NOT USE
THIS FUNCTION."
(cond ((or (string= "checked" value) (string= "on" value)) +true+)
((or (string= "off" value) (null value)) +false+)
((null value) +false+)))
(defun get-image-dimensions (filepath)
"Uses Image Magick (via Bash) to get the resolution of an image as 'WxH'.
The `FILEPATH' must be already merged with
`ritherdon-archive.config::*application-root*' before you call this function."
(let* ((command
(format nil "identify -format \"%wx%h\" ~a" filepath))
(out-message (uiop:run-program command :output :string
:ignore-error-status t
:error-output :string)))
out-message))
(defun run-bash-command (command)
"Runs the Bash command."
(uiop:run-program command :output :string
:ignore-error-status t
:error-output :string))
(defun create-thumbnail (storage-sub-directory file-name)
"Runs a Bash command to convert a file to a thumbnail in /storage/media dir.
The file is reduced to 512x512 pixels if bigger than that. A new file
is then created with a 'thumbnail-' pre-fix. This process relies on
Image Magick. So, it must be installed on the system for this function
to operate properly."
(run-bash-command
(format nil "convert ~a -resize 512x512\\> ~a"
(storage:file-exists-p "" storage-sub-directory file-name)
(storage:make-path "" storage-sub-directory
(format nil "thumbnail-~a" file-name)))))
(defun create-timestamp-id ()
"Creates a integer based on time the function is called, in YYYYMMDD format."
(multiple-value-bind
(second minute hour day month year)
(get-decoded-time)
(format nil "~d~2,'0d~d~2,'0d~2,'0d~2,'0d" year month day hour minute second)))