summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Okay <cbeok@protonmail.com>2020-04-12 15:45:45 -0500
committerColin Okay <cbeok@protonmail.com>2020-04-12 15:45:45 -0500
commit3fb6ad19bcc102b397702d9b9de3ed6c59c8485a (patch)
tree73e37a9212a8cd84db8218194dfb1e1f3c504839
parent946c0a3dd1c6646d9ed55f91f4d76e3adef6b6ac (diff)
slightly better mimetypes for images
-rw-r--r--posterbot.lisp14
1 files changed, 11 insertions, 3 deletions
diff --git a/posterbot.lisp b/posterbot.lisp
index 0ff3160..d2d3531 100644
--- a/posterbot.lisp
+++ b/posterbot.lisp
@@ -4,25 +4,33 @@
(defclass posterbot (client auto-joiner) ())
-(defvar *posterbot* nil "dynamic variable holding the bot so I dont have to pass it around")
+(defvar *posterbot* nil
+ "Dynamic variable holding the bot instance. Bound by HANDLE-EVENT.")
(defparameter +image-link-regex+
(ppcre:create-scanner "http.+\.(png|gif|jpeg|bmp|jpg)"
:case-insensitive-mode t))
(defun download-link (link)
+ "Downloads the file at LINK to a temporary file. Returns the path to
+the downloaded file."
(cl-fad:with-output-to-temporary-file (out :element-type '(unsigned-byte 8))
(let ((buffer (make-array 4096 :element-type '(unsigned-byte 8)))
(file-stream (drakma:http-request link :want-stream t)))
(loop :for bytes = (read-sequence buffer file-stream)
:while (plusp bytes) :do (write-sequence buffer out)))))
-
(defun filename-from-link (link)
+ "Extracts the filename of a link. I.e. everything after the last / character."
(first (last (ppcre:split "/" link))))
(defun make-mime-type (filename)
- (format nil "image/~a" (pathname-type filename)))
+ "Given a string FILENAME, returns a string representing a sensible guess for a mimetype."
+ (format nil "image/~a"
+ (let ((type (string-downcase (pathname-type filename))))
+ (cond ((member type '("jpg" "jpeg") :test #'equal) "jpeg")
+ ((equal type "svg") "svg+xml")
+ (t type)))))
(defun handle-link-candiate (word)
(let ((link (ppcre:scan-to-strings +image-link-regex+ word)))