From 7a1e8d41ce96a7514adb31529f05aaed6a13c259 Mon Sep 17 00:00:00 2001 From: Grant Shangreaux Date: Tue, 27 Dec 2022 19:22:46 -0600 Subject: Clean: zipper.lisp implementation and defuns, fewer iterations --- utilities.lisp | 3 +++ zipper.lisp | 48 +++++++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/utilities.lisp b/utilities.lisp index 1ed13a3..0e258b6 100644 --- a/utilities.lisp +++ b/utilities.lisp @@ -127,3 +127,6 @@ (if (apply pred args) (apply then args) (apply else args)))) + +(defun clean-slashes (str) + (cl-ppcre:regex-replace-all "/" str "-")) diff --git a/zipper.lisp b/zipper.lisp index 2151019..36e62f9 100644 --- a/zipper.lisp +++ b/zipper.lisp @@ -3,29 +3,12 @@ (in-package :vampire) (defun zip-playlist (playlist) - "Copies all playlist tracks into a download directory and compresses them into a zip archive. -Returns the filename of the zip file." + "Compresses playlist tracks into a zip archive." (with-temp-dir (tmpdir) - (let* ((tracks (mapcar (lambda (track) (merge-pathnames tmpdir track)) - (zip-playlist-filenames playlist))) - (zip-dir (merge-pathnames "media/bundled-playlists" (static-directory *config*))) - (out (format nil "~a/~a.zip" zip-dir (clean-slashes (playlist-title playlist))))) + (let ((zip-dir (zipped-playlist-path))) (ensure-directories-exist zip-dir) - (copy-audio-files-for-download playlist tmpdir) - (org.shirakumo.zippy:compress-zip tracks out :if-exists :overwrite) - (format nil "/media/bundled-playlists/~a.zip" (clean-slashes (playlist-title playlist)))))) - -(defun copy-audio-files-for-download (playlist dir) - (let ((audio-files (mapcar #'track-file (playlist-tracks playlist))) - (dest-files (zip-playlist-filenames playlist))) - (loop for file in audio-files - for dest in dest-files - do (uiop:copy-file file (format nil "~a~a" dir dest))))) - -(defun zip-playlist-filenames (playlist) - (loop for track in (playlist-tracks playlist) - for i from 1 - collect (zip-track-filename track i))) + (org.shirakumo.zippy:compress-zip + (copy-audio-files-for-download playlist tmpdir) zip-dir :if-exists :overwrite)))) (defun zip-track-filename (track pos) "Return a filename for a track. `NN-ARTIST-ALBUM-TITLE.CODEC'" @@ -33,5 +16,24 @@ Returns the filename of the zip file." (format nil "~2,'0d-~a-~a-~a.~a" pos (clean-slashes artist) (clean-slashes album) (clean-slashes title) codec))) -(defun clean-slashes (str) - (cl-ppcre:regex-replace-all "/" str "-")) +(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 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)) -- cgit v1.2.3