Skip to content

Commit

Permalink
Initial changes to implement #4 (art thumbnail caching)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fitzsimmons committed Jan 15, 2011
1 parent 8ef3376 commit 2879a12
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ location: D:/mp3/
db: library.sqlite
skip_discovery: true
skip_album_art: false
cache_thumbnails: true

handlers:
flac_to_mp3:
Expand Down
51 changes: 42 additions & 9 deletions src/streamroller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,54 @@ def initialize
halt f
end

def cached_pic(id, size)
size = size.to_i
f = $db[:songs].filter(:id => id).first()

uncached_path = "art/#{f[:art]}"

return false if f[:art].nil? or f[:art] == 'f'
return false if not File.exists?(uncached_path)

if $config['cache_thumbnails']
begin
Dir.mkdir("art/#{size}")
rescue Errno::EEXIST
end

path = "art/#{size}/#{id}.#{$imgformat}"
if File.exists?(path)
return File.new(path).read
end

cached = File.new(path, "w")
converted = convert_pic(uncached_path, size)
cached.write(converted)
cached.close

return converted
end

return convert_pic(uncached_path, size)
end

def convert_pic(path, size)
i = Magick::Image.read(path)[0]
r = i.resize(size,size)
r.format = $imgformat
content_type mime_type($imgformat)
s = r.to_blob
return s
end

#TODO: Maybe there's a better way to do optional params with sinatra.
def handle_pic(id, size=96)
size = size.to_i
Timeout.timeout(10) do
f = $db[:songs].filter(:id => id).first()
return false if f[:art].nil? or f[:art] == 'f'
begin
i = Magick::Image.read("art/#{f[:art]}")[0]
r = i.resize(size,size)
r.format = $imgformat
content_type mime_type($imgformat)
s = r.to_blob
return s
return cached_pic(id, size)
rescue
puts "Error sending album art: #{f[:file]} #{$!}"
puts "Error sending album art: #{id} #{$!}"
end
end
end
Expand Down

0 comments on commit 2879a12

Please sign in to comment.