Skip to content

Commit

Permalink
cover display
Browse files Browse the repository at this point in the history
  • Loading branch information
onli committed Jul 6, 2013
1 parent 6ec259a commit 9b69a6e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
26 changes: 23 additions & 3 deletions public/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,33 @@ function addPlayerFunctions() {
}

insertOrReplace('#player', createPlayer(0, songs), '#currentMedia');
var cover = document.querySelector('#cover');
if (cover != null) {
cover.parentNode.removeChild(cover);
}
// remove cover of the prior album if it exists
showCover(songs[0].id);
}
}

http.open("GET","/getTracks?artist="+artist +"&album="+album, true);
http.open("GET","/tracks?artist="+artist +"&album="+album, true);
http.send();
showDownloadButton(artist, album);
}
});

function showCover(songid) {
var cover = document.createElement("img");
cover.addEventListener("load", function() {
var curMedia = document.querySelector("#currentMedia");
if (cover.complete) {
curMedia.insertBefore(cover, curMedia.firstChild);
}
});
cover.id = "cover";
cover.src = "/cover?id="+songid;
}

function showDownloadButton(artist, album) {
var button = document.createElement("button");
var form = document.createElement("form");
Expand All @@ -44,14 +62,16 @@ function addPlayerFunctions() {

form.method = "GET";
form.action = "/download";
form.id = "downloadForm";

button.innerHTML = "Download";
button.title = "Download album";
button.className = "icon-download";

form.appendChild(button);
form.appendChild(artistInput);
form.appendChild(albumInput);

insertOrReplace('#downloadForm', form);
insertOrReplace('#downloadForm', form, "#mediaDBList");
}

/**
Expand Down
22 changes: 20 additions & 2 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def emptyMediaDB?
erb :mediaDB, :locals => {:mediaDB => Database.new.getMediaDB}
end

get '/getTracks' do
get '/tracks' do
protected!
tracks = Database.new.getTracks(params[:artist], params[:album]).delete_if{|key, value| key.is_a? Integer}
JSON(tracks)
Expand Down Expand Up @@ -168,7 +168,6 @@ def emptyMediaDB?
name += ".mp3" unless name.end_with?(".mp3")
z.put_next_entry(name)
z.print(open(Database.new.getPath(track["id"])) {|f| f.read })
p track["title"] + ' added to file'
end
end

Expand All @@ -179,6 +178,25 @@ def emptyMediaDB?

end

# returns a cover, expects a id of one of the songs of the current album
get '/cover' do
protected!
path = Database.new.getPath(params[:id])
path = File.dirname(path)
images = Dir[path + "/*.jpg"]
if (images.size > 1)
["folder.jpg", "front.jpg", "large.jpg"].each do |preferredImage|
index = images.index { |i| i.downcase.include? preferredImage}
if (index)
send_file images[index]
end
end
end
if images.size > 0
send_file images[0]
end
end

get %r{/track/([0-9]+)} do |id|
path = Database.new.getPath(id)
type = FileMagic.new(FileMagic::MAGIC_MIME).file(path)
Expand Down

0 comments on commit 9b69a6e

Please sign in to comment.