From a2cdb8ec805fe7cc2e5afb5c99f20273627769af Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 11 Apr 2026 19:35:52 -0400 Subject: [PATCH] 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). --- core/artwork/reader_disc.go | 50 ++++++++++++++++++++------------ core/artwork/reader_disc_test.go | 41 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/core/artwork/reader_disc.go b/core/artwork/reader_disc.go index e757246b3..445984bbb 100644 --- a/core/artwork/reader_disc.go +++ b/core/artwork/reader_disc.go @@ -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) } diff --git a/core/artwork/reader_disc_test.go b/core/artwork/reader_disc_test.go index 4a7ea2eea..7bdf88d87 100644 --- a/core/artwork/reader_disc_test.go +++ b/core/artwork/reader_disc_test.go @@ -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")