-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.clj
35 lines (31 loc) · 1.24 KB
/
utils.clj
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
(ns michmusic.utils
(:require [clojure.contrib.str-utils2 :as str-utils2])
(:use [clojure.contrib.duck-streams :only (copy to-byte-array)]
[clojure.contrib.java-utils :only (file)]
[clojure.contrib.str-utils :only (str-join)])
(:import [java.security MessageDigest]
[java.util.zip ZipEntry ZipOutputStream]))
(defn- song-filename
"Converts a song struct to a filename appropriate for the mp3."
[song]
(let [escaped-title (str-utils2/replace (song :title) "/" "_")]
(str (song :track) " " escaped-title ".mp3")))
(defn zip-files
"Given a list of songs, dumps them zipped into ostream."
[artist album songs ostream]
(let [dir-name (str artist " - " album "/")]
(with-open [out (ZipOutputStream. ostream)]
(doseq [song songs]
(.putNextEntry out (ZipEntry. (str dir-name (song-filename song))))
(copy (file (song :path)) out)
(.closeEntry out)))))
(defn sha-1
"Generates a SHA-1 hash of the given input plaintext.
=> (sha-1 \"hi\")
\"c22b5f917834269428d6f51b2c5af4cbde6a42\"
"
[input]
(let [md (MessageDigest/getInstance "SHA-1")]
(. md update (to-byte-array input))
(let [digest (.digest md)]
(str-join "" (map #(Integer/toHexString (bit-and % 0xff)) digest)))))