From 4736db9bd2115c0ae39bc8339f73a3bde4d3823e Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Sun, 18 Sep 2022 23:02:06 +0100 Subject: [PATCH] add validation package. --- src/validation.lisp | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/validation.lisp diff --git a/src/validation.lisp b/src/validation.lisp new file mode 100644 index 0000000..9ae33a6 --- /dev/null +++ b/src/validation.lisp @@ -0,0 +1,57 @@ +(in-package #:cl-user) +(defpackage #:validation + (:use #:cl + #:app-constants) + (:export #:has-static-assets-extention? + #:is-valid-favicon-type? + #:favicon-need-resizing?) + (:documentation "Package for validating 'stuff'.")) +(in-package #:validation) + +(defun file-has-valid-static-assets-extention? (filename) + (cond ((or (string= "css" (pathname-type filename)) + (string= "js" (pathname-type filename)) + (string= "png" (pathname-type filename)) + (string= "ico" (pathname-type filename)) + (string= "gif" (pathname-type filename)) + (string= "jpg" (pathname-type filename)) + (string= "svg" (pathname-type filename)) + (string= "txt" (pathname-type filename))) + filename) + (t nil))) + +(defun file-is-valid-favicon-type? (filename) + "Returns path of file if `FILE' is a valid favicon. +Valid file types are 'ico', 'gif' and 'png'." + (cond ((or (string= "png" (pathname-type filename)) + (string= "ico" (pathname-type filename)) + (string= "gif" (pathname-type filename))) + filename) + (t nil))) + +;; TODO: PORT CODE TO CALL THIS VERSION OF STRING-IS-NIL-OR-EMPTY? +(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 favicon-need-resizing? (dimensions) + "Checks to see if the `WIDTH' or `HEIGHT' are greater than 192x192 pixels. +`DIMENSIONS' is a cons list, with the `WIDTH' being first element and +`HEIGHT' being the second. If either of them are, then you look into +scaling down the image, using code in the `UTILS' package probably." + (cond ((or (< 192 (car dimensions)) (< 192 (cdr dimensions))) + t) ; True as in the dimensions are valid + (t ;; Bit confusing with the use of `T' here but its standard + ;; practice to end `COND' with a catch-all `T'. In this + ;; case, `T' here means the dimensions are invalid, as in + ;; 'yes' the favicon needs resizing. This is confusing + ;; Boolean logic but when you call this function, the + ;; return types make sense from that point-of-view. + nil)))