Skip to content

Commit 356748d

Browse files
committed
Scan nested artwork directories for covers
1 parent 7afed61 commit 356748d

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

media_detector.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ func IsAudioFile(f *StorageFile) bool {
3030
return audioExtensions.Contains(ext)
3131
}
3232

33+
var artworkDirNames = NewStringSet("scans", "covers", "artwork", "media")
34+
35+
// IsArtworkDir returns whether the given directory may contain cover images.
36+
func IsArtworkDir(d *StorageDirectory) bool {
37+
return artworkDirNames.Contains(strings.ToLower(d.Name()))
38+
}
39+
3340
type ScoredFile struct {
3441
*StorageFile
3542
Score int

media_library.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func NewMediaLibrary(store *S3Storage) *MediaLibrary {
2525
func (ml *MediaLibrary) findCover(files []*StorageFile) *StorageFile {
2626
candidates := ScoreCovers(files)
2727
if len(candidates) == 0 {
28-
// TODO: look at subdirectories such as covers, scans and artwork.
2928
return nil
3029
}
3130
sort.SliceStable(candidates, func(i, j int) bool {
@@ -34,16 +33,40 @@ func (ml *MediaLibrary) findCover(files []*StorageFile) *StorageFile {
3433
return candidates[0].StorageFile
3534
}
3635

36+
func (ml *MediaLibrary) listArtworkFiles(dirs []*StorageDirectory) ([]*StorageFile, error) {
37+
var candidates []*StorageFile
38+
for _, dir := range dirs {
39+
if !IsArtworkDir(dir) {
40+
continue
41+
}
42+
_, files, err := ml.store.List(dir.Path())
43+
if err != nil {
44+
return nil, err
45+
}
46+
candidates = append(candidates, files...)
47+
}
48+
return candidates, nil
49+
}
50+
3751
// List returns directory listing under the provided path.
3852
func (ml *MediaLibrary) List(p string) (*MediaListing, error) {
3953
dirs, files, err := ml.store.List(p)
4054
if err != nil {
4155
return nil, err
4256
}
4357

44-
// Find album cover.
58+
// Find album cover in the current directory.
4559
cover := ml.findCover(files)
4660

61+
if cover == nil {
62+
// Scan nested artwork directories for covers.
63+
artworkFiles, err := ml.listArtworkFiles(dirs)
64+
if err != nil {
65+
return nil, err
66+
}
67+
cover = ml.findCover(artworkFiles)
68+
}
69+
4770
// Find audio tracks and separate all other files.
4871
var tracks []*StorageFile
4972
var otherFiles []*StorageFile

media_library_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestMediaLibrary(t *testing.T) {
3232
"music/Aphex Twin/1999 - Windowlicker/Folder.jpg",
3333
"music/Aphex Twin/1999 - Windowlicker/back.jpg",
3434
"music/Aphex Twin/1999 - Windowlicker/covers/front_cover.jpg",
35-
"music/The Prodigy/1992 - The Prodigy Experience/scans/Cover-Case.png",
35+
"music/The Prodigy/1992 - The Prodigy Experience/Scans/Cover-Case.png",
3636
"music/The Prodigy/1992 - The Prodigy Experience/CD1/01 - Jericho.mp3",
3737
"music/The Prodigy/1992 - The Prodigy Experience/CD2/01 - Your Love.mp3",
3838
"music/Venetian Snares/2016 - Traditional Synthesizer Music/01. Dreamt Person v3.mp3",
@@ -96,8 +96,9 @@ func TestMediaLibrary(t *testing.T) {
9696
Directories: []*StorageDirectory{
9797
NewStorageDirectory("The Prodigy/1992 - The Prodigy Experience/CD1"),
9898
NewStorageDirectory("The Prodigy/1992 - The Prodigy Experience/CD2"),
99-
NewStorageDirectory("The Prodigy/1992 - The Prodigy Experience/scans"),
99+
NewStorageDirectory("The Prodigy/1992 - The Prodigy Experience/Scans"),
100100
},
101+
Cover: NewStorageFile("The Prodigy/1992 - The Prodigy Experience/Scans/Cover-Case.png", 1),
101102
},
102103
"The Prodigy/1992 - The Prodigy Experience/CD1": {
103104
CurrentDirectory: NewStorageDirectory("The Prodigy/1992 - The Prodigy Experience/CD1"),
@@ -126,7 +127,7 @@ func TestMediaLibrary(t *testing.T) {
126127
for path, expectedListing := range testCases {
127128
l, err := ml.List(path)
128129
asrt.NoError(err)
129-
asrt.EqualValues(&expectedListing, l)
130+
asrt.EqualValues(&expectedListing, l, path)
130131
}
131132

132133
// Path doesn't exist.

0 commit comments

Comments
 (0)