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.
This commit is contained in:
Deluan committed 2026-04-11 19:03:43 -04:00
1 parent 0de7293a51
commit 7524ca6633
2 files changed
+5 -14

No files matched your search

+5 -8
View File
@@ -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 {
-6
View File
@@ -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]()