From 252eac47987842f5c5b95433e9cfa8e438549446 Mon Sep 17 00:00:00 2001 From: Steven Deobald Date: Sat, 31 Mar 2012 16:44:15 +0530 Subject: [PATCH 1/2] [sd/np] only allow 4 attempts to find a song, return the default song if user dirs are all empty --- spec/jukebox_web/models/library_spec.clj | 4 ++-- src/jukebox_web/models/library.clj | 20 ++++++++++++-------- src/jukebox_web/models/playlist.clj | 4 +++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/spec/jukebox_web/models/library_spec.clj b/spec/jukebox_web/models/library_spec.clj index 34faf20..a84d656 100644 --- a/spec/jukebox_web/models/library_spec.clj +++ b/spec/jukebox_web/models/library_spec.clj @@ -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" diff --git a/src/jukebox_web/models/library.clj b/src/jukebox_web/models/library.clj index 5d13fab..ab5ea27 100644 --- a/src/jukebox_web/models/library.clj +++ b/src/jukebox_web/models/library.clj @@ -54,12 +54,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)) @@ -68,10 +69,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)))] diff --git a/src/jukebox_web/models/playlist.clj b/src/jukebox_web/models/playlist.clj index 282cf7d..d5dc7aa 100644 --- a/src/jukebox_web/models/playlist.clj +++ b/src/jukebox_web/models/playlist.clj @@ -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)"})))) From 45a98adf42ecd53f1dbef83144a576559b13d401 Mon Sep 17 00:00:00 2001 From: Steven Deobald Date: Sat, 31 Mar 2012 17:01:12 +0530 Subject: [PATCH 2/2] [sd/np] create directory for new user on signup --- src/jukebox_web/models/library.clj | 3 +++ src/jukebox_web/models/user.clj | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/jukebox_web/models/library.clj b/src/jukebox_web/models/library.clj index ab5ea27..3b9519a 100644 --- a/src/jukebox_web/models/library.clj +++ b/src/jukebox_web/models/library.clj @@ -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) diff --git a/src/jukebox_web/models/user.clj b/src/jukebox_web/models/user.clj index 75b6038..7283827 100644 --- a/src/jukebox_web/models/user.clj +++ b/src/jukebox_web/models/user.clj @@ -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]