Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions spec/jukebox_web/models/library_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
(it "returns a random song with no prefix when no argument is provided"
(should-not-be-nil (library/random-song)))

(it "returns a random song from the given path"
(it "returns the default song if no user songs exist"
(let [selections (take 10 (map library/random-song (repeat "user")))]
(should-not (includes? (map #(.getName %) selections) "jukebox.mp3")))))
(should (includes? (map #(.getName %) selections) "jukebox2.mp3")))))

(describe "owner"
(it "returns nil for a path without a user"
Expand Down
23 changes: 15 additions & 8 deletions src/jukebox_web/models/library.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
(mkdir-p dir)
(mv file new-file)))

(defn create-user-directory [username]
(mkdir-p (file-path *music-library* username)))

(defn save-file [tempfile user ext]
(let [file-with-ext (io/as-file (file-path *music-library* (str (UUID/randomUUID) "." ext)))]
(io/copy tempfile file-with-ext)
Expand Down Expand Up @@ -54,12 +57,13 @@
(.isFile (file-on-disk relative-path)))

(defn all-tracks
([] (all-tracks ""))
([]
(all-tracks ""))
([path]
(let [contents (file-seq (io/file *music-library* path))]
(->> contents
(filter #(.endsWith (.getName %) ".mp3"))
(map #(relativize *music-library* %))))))
(let [contents (file-seq (io/file *music-library* path))]
(->> contents
(filter #(.endsWith (.getName %) ".mp3"))
(map #(relativize *music-library* %))))))

(defn owner [song]
(let [path (.getPath (relativize *music-library* song))
Expand All @@ -68,10 +72,13 @@
(first filename-parts))))

(defn random-song
([] (random-song ""))
([]
(random-song ""))
([path]
(let [tracks (all-tracks path)]
(if-not (empty? tracks) (rand-nth (shuffle tracks)) nil))))
(let [tracks (all-tracks path)]
(if-not (empty? tracks)
(rand-nth (shuffle tracks))
(random-song "")))))

(defn play-count [track]
(let [play-count-row (first (db/find-by-field *play-counts-model* "track" (str track)))]
Expand Down
4 changes: 3 additions & 1 deletion src/jukebox_web/models/playlist.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@

(defn add-random-song! []
(loop [song (random-song) attempts 0]
(if (or (nil? song) (.contains @recent-songs-atom song))
(if (and (< attempts 4)
(or (nil? song)
(.contains @recent-songs-atom song)))
(recur (random-song) (inc attempts))
(add-song! song {:login "(randomizer)"}))))

Expand Down
4 changes: 3 additions & 1 deletion src/jukebox_web/models/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
(defn sign-up! [user-args]
(let [errors (validate-for-sign-up user-args)]
(if (empty? errors)
[(db/insert *model* (build-user user-args)) errors]
(do
(library/create-user-directory (:login user-args))
[(db/insert *model* (build-user user-args)) errors])
[nil errors])))

(defn authenticate [login password]
Expand Down