From 3fb6ad19bcc102b397702d9b9de3ed6c59c8485a Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Sun, 12 Apr 2020 15:45:45 -0500 Subject: slightly better mimetypes for images --- posterbot.lisp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'posterbot.lisp') 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))) -- cgit v1.2.3