From 7524ca6633fa3f490c01e480d92d704d36d12718 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 11 Apr 2026 19:03:43 -0400 Subject: [PATCH] refactor(artwork): simplify disc art matching and drop redundant comments - Lowercase the pattern and filename once in fromExternalFile and pass lowered values into extractDiscNumber, eliminating the duplicate strings.ToLower calls inside that helper. - Drop narrating comments in reader_disc.go and reader_disc_test.go that duplicated information already conveyed by nearby code or doc comments. --- core/artwork/reader_disc.go | 13 +++++-------- core/artwork/reader_disc_test.go | 6 ------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/core/artwork/reader_disc.go b/core/artwork/reader_disc.go index b0562407f..e757246b3 100644 --- a/core/artwork/reader_disc.go +++ b/core/artwork/reader_disc.go @@ -172,10 +172,9 @@ func (d *discArtworkReader) fromDiscSubtitle(ctx context.Context, subtitle strin // It finds the portion of the filename that the wildcard matched and parses leading // digits as the disc number. Returns (0, false) if the pattern doesn't match or // no leading digits are found in the wildcard portion. +// +// Both pattern and filename must already be lowercased by the caller. func extractDiscNumber(pattern, filename string) (int, bool) { - filename = strings.ToLower(filename) - pattern = strings.ToLower(pattern) - matched, err := filepath.Match(pattern, filename) if err != nil || !matched { return 0, false @@ -227,10 +226,12 @@ func extractDiscNumber(pattern, filename string) (int, bool) { // for every disc (shared disc art). DiscArtPriority ordering decides whether // a more specific numbered pattern wins. func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string) sourceFunc { + pattern = strings.ToLower(pattern) return func() (io.ReadCloser, string, error) { for _, file := range d.imgFiles { _, name := filepath.Split(file) - match, err := filepath.Match(pattern, strings.ToLower(name)) + name = strings.ToLower(name) + match, err := filepath.Match(pattern, name) if err != nil { log.Warn(ctx, "Error matching disc art file to pattern", "pattern", pattern, "file", file) continue @@ -239,7 +240,6 @@ func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string continue } - // Try to extract disc number from filename if num, hasNum := extractDiscNumber(pattern, name); hasNum { // Numbered filename: must match target disc. if num != d.discNumber { @@ -253,9 +253,6 @@ func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string continue } } - // Unnumbered, single-folder: shared across all discs of the album. - // Accept the match; DiscArtPriority order decides whether a more - // specific numbered pattern wins. f, err := os.Open(file) if err != nil { diff --git a/core/artwork/reader_disc_test.go b/core/artwork/reader_disc_test.go index 4e81a9e47..4a7ea2eea 100644 --- a/core/artwork/reader_disc_test.go +++ b/core/artwork/reader_disc_test.go @@ -131,7 +131,6 @@ var _ = Describe("Disc Artwork Reader", func() { discFolders: map[string]bool{filepath.Join(tmpDir, "album"): true}, } - // Numbered pattern: disc-number filter must still return disc 2. sf := reader.fromExternalFile(ctx, "disc*.*") r, path, err := sf() Expect(err).ToNot(HaveOccurred()) @@ -139,9 +138,6 @@ var _ = Describe("Disc Artwork Reader", func() { r.Close() Expect(path).To(Equal(f3)) - // Unnumbered pattern against the same reader: single-folder shared - // disc art branch must still return cover.png even though numbered - // files are present in imgFiles. sf = reader.fromExternalFile(ctx, "cover.*") r, path, err = sf() Expect(err).ToNot(HaveOccurred()) @@ -159,7 +155,6 @@ var _ = Describe("Disc Artwork Reader", func() { discFolders: map[string]bool{filepath.Join(tmpDir, "album"): true}, } - // Numbered pattern first → disc1.jpg wins. ff := reader.fromDiscArtPriority(ctx, nil, "disc*.*, cover.*") Expect(ff).To(HaveLen(2)) r, path, err := ff[0]() @@ -167,7 +162,6 @@ var _ = Describe("Disc Artwork Reader", func() { Expect(path).To(Equal(f2)) r.Close() - // Unnumbered pattern first → cover.png wins. ff = reader.fromDiscArtPriority(ctx, nil, "cover.*, disc*.*") Expect(ff).To(HaveLen(2)) r, path, err = ff[0]()