aboutsummaryrefslogtreecommitdiffhomepage
path: root/zipper.lisp
blob: 91fafba0a6190e7122d2398bb7cb33344407e1ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
;;;; zipper.lisp

(in-package :vampire)

(defun zip-playlist (playlist)
  "Compresses playlist tracks into a zip archive."
  (unless (zipped-playlist-exists-p playlist)
    (let ((zip-file (zipped-playlist-path playlist)))
      (ensure-directories-exist zip-file)
      (with-temp-dir (tmpdir)
        (copy-audio-files-for-download playlist tmpdir)
        (zippy:compress-zip
         tmpdir zip-file :if-exists :supersede)))))


(defun zip-track-filename (track pos)
  "Return a filename for a track. `NN-ARTIST-ALBUM-TITLE.CODEC'"
  (with-slots (artist album title codec) track
    (clean-filename (format nil "~2,'0d-~a-~a-~a.~a" pos artist album title codec))))

(defun zipped-playlist-filename (playlist)
  "Return a url-safe zip filename for a playlist."
  (concatenate 'string (clean-filename (playlist-title playlist)) ".zip"))

(defun zipped-playlist-path (playlist)
  "Returns the zipped playlist path relative to the configured static directory."
  (merge-pathnames (zipped-playlist-url playlist :absolute nil)
                   (static-directory *config*)))

(defun zipped-playlist-url (playlist &key (absolute t))
  "Returns the url where the playlist's zip file is expected to exist."
  (format nil "~:[~;/~]media/bundled-playlists/~a"
          absolute
          (zipped-playlist-filename playlist)))

(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" :auto-place nil)))

(defun delete-zipped-playlist (playlist-ctl)
  "Deletes the zipped playlist file and removes the associated link element.
Returns T if it was deleted, NIL otherwise."
  (when (uiop:delete-file-if-exists (zipped-playlist-path (playlist playlist-ctl)))
    (unless (null (pl-zip playlist-ctl))
      (destroy (pl-zip playlist-ctl)))))