mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
fix(artwork): prefer numbered disc art over shared fallback within a pattern
Review feedback: with files [disc.jpg, disc1.jpg, disc2.jpg] in a single folder, the previous single-folder fall-through returned the first match in imgFiles order. Because compareImageFiles sorts 'disc' before 'disc1' and 'disc2', disc.jpg would mask the per-disc numbered files for every disc, regressing the behavior from before the shared-disc-art change. Within a single pattern the loop now records the first viable unnumbered candidate as a fallback and keeps scanning for a numbered match equal to the target disc. Numbered matches still win immediately; the shared file is only returned when no numbered match for the target disc exists. Also drops the redundant strings.ToLower(pattern) at the top of fromExternalFile; fromDiscArtPriority already lowercases the whole priority string before splitting, so the function contract is now 'pattern must be lowercase' (documented on the function).
This commit is contained in:
1 parent
7524ca6633
commit
a2cdb8ec80
2 files changed
+72
-19
No files matched your search
+31
-19
@@ -217,17 +217,18 @@ func extractDiscNumber(pattern, filename string) (int, bool) {
|
||||
// fromExternalFile returns a sourceFunc that matches image files against a glob
|
||||
// pattern with disc-number-aware filtering.
|
||||
//
|
||||
// Matching rules:
|
||||
// - If a disc number can be extracted from the filename, the file matches only if
|
||||
// the number equals the target disc number.
|
||||
// - If no number is found and this is a multi-folder album, the file matches if
|
||||
// it's in a folder containing tracks for this disc.
|
||||
// - If no number is found and this is a single-folder album, the file matches
|
||||
// for every disc (shared disc art). DiscArtPriority ordering decides whether
|
||||
// a more specific numbered pattern wins.
|
||||
// Within a single pattern, a numbered filename whose number equals the target
|
||||
// disc always wins over an unnumbered candidate. An unnumbered candidate is
|
||||
// returned only as a fallback, and only when:
|
||||
// - the album has multiple folders and the file lives in a folder containing
|
||||
// tracks for this disc (the image belongs to a specific disc); or
|
||||
// - the album has a single folder (the file is shared across every disc).
|
||||
//
|
||||
// The caller (fromDiscArtPriority) already lowercases the pattern, so this
|
||||
// function assumes pattern is lowercase and only lowercases each filename.
|
||||
func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string) sourceFunc {
|
||||
pattern = strings.ToLower(pattern)
|
||||
return func() (io.ReadCloser, string, error) {
|
||||
var fallback string
|
||||
for _, file := range d.imgFiles {
|
||||
_, name := filepath.Split(file)
|
||||
name = strings.ToLower(name)
|
||||
@@ -241,25 +242,36 @@ func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string
|
||||
}
|
||||
|
||||
if num, hasNum := extractDiscNumber(pattern, name); hasNum {
|
||||
// Numbered filename: must match target disc.
|
||||
// Numbered filename wins immediately when its number matches.
|
||||
if num != d.discNumber {
|
||||
continue
|
||||
}
|
||||
} else if d.isMultiFolder {
|
||||
// Unnumbered, multi-folder: match by folder association — the
|
||||
// image lives alongside the tracks of a specific disc.
|
||||
dir := filepath.Dir(file)
|
||||
if !d.discFolders[dir] {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
log.Warn(ctx, "Could not open disc art file", "file", file, err)
|
||||
continue
|
||||
}
|
||||
return f, file, nil
|
||||
}
|
||||
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
log.Warn(ctx, "Could not open disc art file", "file", file, err)
|
||||
// Unnumbered: remember the first viable candidate as a fallback,
|
||||
// but keep scanning for a numbered match that should take precedence.
|
||||
if fallback != "" {
|
||||
continue
|
||||
}
|
||||
return f, file, nil
|
||||
if d.isMultiFolder && !d.discFolders[filepath.Dir(file)] {
|
||||
continue
|
||||
}
|
||||
fallback = file
|
||||
}
|
||||
|
||||
if fallback != "" {
|
||||
f, err := os.Open(fallback)
|
||||
if err != nil {
|
||||
log.Warn(ctx, "Could not open disc art file", "file", fallback, err)
|
||||
} else {
|
||||
return f, fallback, nil
|
||||
}
|
||||
}
|
||||
return nil, "", fmt.Errorf("disc %d: pattern '%s' not matched by files", d.discNumber, pattern)
|
||||
}
|
||||
|
||||
@@ -170,6 +170,47 @@ var _ = Describe("Disc Artwork Reader", func() {
|
||||
r.Close()
|
||||
})
|
||||
|
||||
It("prefers disc-numbered match over shared unnumbered match within the same pattern", func() {
|
||||
// Natural sort of basenames puts disc < disc1 < disc2, so
|
||||
// disc.jpg appears first in imgFiles. The loop must not return
|
||||
// disc.jpg for disc 2 when disc2.jpg also matches the pattern.
|
||||
f1 := createFile("album/disc.jpg")
|
||||
f2 := createFile("album/disc1.jpg")
|
||||
f3 := createFile("album/disc2.jpg")
|
||||
reader := &discArtworkReader{
|
||||
discNumber: 2,
|
||||
imgFiles: []string{f1, f2, f3},
|
||||
discFolders: map[string]bool{filepath.Join(tmpDir, "album"): true},
|
||||
}
|
||||
|
||||
sf := reader.fromExternalFile(ctx, "disc*.*")
|
||||
r, path, err := sf()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(r).ToNot(BeNil())
|
||||
r.Close()
|
||||
Expect(path).To(Equal(f3))
|
||||
})
|
||||
|
||||
It("falls back to shared unnumbered match when no numbered file matches the target disc", func() {
|
||||
// Disc 3 has no disc3.jpg; disc.jpg should be used as the shared
|
||||
// fallback so a disc without dedicated art still gets something.
|
||||
f1 := createFile("album/disc.jpg")
|
||||
f2 := createFile("album/disc1.jpg")
|
||||
f3 := createFile("album/disc2.jpg")
|
||||
reader := &discArtworkReader{
|
||||
discNumber: 3,
|
||||
imgFiles: []string{f1, f2, f3},
|
||||
discFolders: map[string]bool{filepath.Join(tmpDir, "album"): true},
|
||||
}
|
||||
|
||||
sf := reader.fromExternalFile(ctx, "disc*.*")
|
||||
r, path, err := sf()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(r).ToNot(BeNil())
|
||||
r.Close()
|
||||
Expect(path).To(Equal(f1))
|
||||
})
|
||||
|
||||
It("matches file without number in multi-folder album by folder", func() {
|
||||
f1 := createFile("album/cd1/disc.jpg")
|
||||
f2 := createFile("album/cd2/disc.jpg")
|
||||
|
||||
Reference in new issue
Block a user