summaryrefslogtreecommitdiffstats
path: root/app.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app.rb')
-rw-r--r--app.rb17
1 files changed, 11 insertions, 6 deletions
diff --git a/app.rb b/app.rb
index f5c52d4..f58a99d 100644
--- a/app.rb
+++ b/app.rb
@@ -123,12 +123,17 @@ helpers do
def album_cover(dir, data)
cover = data['cover']
- if cover == '__random__'
- candidates = Dir.children(dir).select { |n| (IMAGE_EXTS + VIDEO_EXTS).include?(File.extname(n).downcase.delete_prefix('.')) }
- return candidates.sample
- end
- return cover if cover && File.exist?(File.join(dir, cover))
- Dir.children(dir).sort.find { |n| (IMAGE_EXTS + VIDEO_EXTS).include?(File.extname(n).downcase.delete_prefix('.')) }
+ return cover if cover && cover != '__random__' && File.exist?(File.join(dir, cover))
+ candidates = cover_candidates(dir)
+ cover == '__random__' ? candidates.sample : candidates.first
+ end
+
+ def cover_candidates(dir)
+ exts = (IMAGE_EXTS + VIDEO_EXTS).to_set
+ Dir.glob(File.join(dir, '**', '*'))
+ .select { |f| File.file?(f) && exts.include?(File.extname(f).downcase.delete_prefix('.')) }
+ .sort
+ .map { |f| f.delete_prefix("#{dir}/") }
end
# Returns the absolute path or halts with 404.