;;;; zipper.lisp (in-package :vampire) (defvar *zip-cluster*) (defun start-zipper-service (config) (let ((zipped-dir (merge-pathnames "media/bundled-playlists/" (static-directory config)))) (ensure-directories-exist zipped-dir) (setf *zip-cluster* (legion:make-cluster (downloader-threads config) (lambda (job) (funcall job))))) (legion:start *zip-cluster*)) (defun add-zip-playlist-job (playlist ok err) "PLAYLIST is a PLAYLIST instance. OK ... ERR ..." (legion:add-job *zip-cluster* (lambda () (handler-case (funcall ok (zip-playlist playlist)) (error (e) (funcall err e)))))) (defun zip-playlist (playlist) "Compresses playlist tracks into a zip archive." (unless (zipped-playlist-exists-p playlist) (let ((zip-file (zipped-playlist-path playlist))) (with-temp-dir (tmpdir) (with-open-file (_foo zip-file :if-does-not-exist :create :direction :output :if-exists :overwrite) (org.shirakumo.zippy:compress-zip (copy-audio-files-for-download playlist tmpdir) zip-file :if-exists :overwrite)))))) (defun zip-track-filename (track pos) "Return a filename for a track. `NN-ARTIST-ALBUM-TITLE.CODEC'" (with-slots (artist album title codec) track (format nil "~2,'0d-~a-~a-~a.~a" pos (clean-slashes artist) (clean-slashes album) (clean-slashes title) codec))) (defun zipped-playlist-filename (playlist) "Return a url-safe zip filename for a playlist." (concatenate 'string (clean-slashes (playlist-title playlist)) ".zip")) (defun zipped-playlist-path (playlist) "Returns the zipped playlist path relative to the configured static directory." (merge-pathnames (pathname-utils:to-relative (zipped-playlist-url playlist)) (static-directory *config*))) (defun zipped-playlist-url (playlist) "Returns the url where the playlist's zip file is expected to exist." (merge-pathnames (zipped-playlist-filename playlist) "/media/bundled-playlists/")) (defun zipped-playlist-exists-p (playlist) (uiop:file-exists-p (zipped-playlist-path playlist))) (defun copy-audio-files-for-download (playlist dir) "Copies all playlist tracks into a temporary directory and returns a list of the pathnames." (loop for track in (playlist-tracks playlist) for pos from 1 for file = (track-file track) for dest = (merge-pathnames (zip-track-filename track pos) dir) do (uiop:copy-file file dest) collect dest)) (defun make-zipped-playlist-link (playlist context) (when (zipped-playlist-exists-p playlist) (create-a context :link (zipped-playlist-url playlist) :content "Download" :target "_blank"))) (defun delete-zipped-playlist (playlist-ctl) "Deletes the zipped playlist file. Returns T if it was deleted, NIL otherwise." (print "Deleting zipped playlist") (uiop:delete-file-if-exists (zipped-playlist-path (playlist playlist-ctl))) (destroy (pl-zip playlist-ctl)))