mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
refactor(artwork): trim disc art matching comments and table-drive tests
Doc comment on fromExternalFile is trimmed to the one non-obvious contract (caller must pre-lowercase the pattern) plus the headline behavior; the bulleted restatement of the branch logic went away. Two inline comments that narrated what the code already shows are also gone. Hoisting a `hasWildcard := strings.ContainsRune(pattern, '*')` check out of the loop avoids per-iteration extractDiscNumber calls for literal patterns (e.g. `shellac.png`) and lets the loop break as soon as a viable fallback is found, since literal patterns can never be beaten by a numbered match. Wildcard patterns keep the original scan-to-end-for-numbered-match behavior. The two regression tests added in the previous commit were structurally identical apart from discNumber/expected, so they are collapsed into a DescribeTable with two entries — matching the existing table style used for extractDiscNumber tests in the same file.
This commit is contained in:
1 parent
a2cdb8ec80
commit
8285587fcd
2 files changed
+41
-62
No files matched your search
+19
-23
@@ -215,18 +215,10 @@ func extractDiscNumber(pattern, filename string) (int, bool) {
|
||||
}
|
||||
|
||||
// fromExternalFile returns a sourceFunc that matches image files against a glob
|
||||
// pattern with disc-number-aware filtering.
|
||||
//
|
||||
// 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.
|
||||
// pattern. A numbered filename whose number equals the target disc wins over
|
||||
// any unnumbered candidate; callers must pass a lowercase pattern.
|
||||
func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string) sourceFunc {
|
||||
hasWildcard := strings.ContainsRune(pattern, '*')
|
||||
return func() (io.ReadCloser, string, error) {
|
||||
var fallback string
|
||||
for _, file := range d.imgFiles {
|
||||
@@ -241,21 +233,20 @@ func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string
|
||||
continue
|
||||
}
|
||||
|
||||
if num, hasNum := extractDiscNumber(pattern, name); hasNum {
|
||||
// Numbered filename wins immediately when its number matches.
|
||||
if num != d.discNumber {
|
||||
continue
|
||||
if hasWildcard {
|
||||
if num, hasNum := extractDiscNumber(pattern, name); hasNum {
|
||||
if num != d.discNumber {
|
||||
continue
|
||||
}
|
||||
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)
|
||||
continue
|
||||
}
|
||||
return f, file, nil
|
||||
}
|
||||
|
||||
// Unnumbered: remember the first viable candidate as a fallback,
|
||||
// but keep scanning for a numbered match that should take precedence.
|
||||
if fallback != "" {
|
||||
continue
|
||||
}
|
||||
@@ -263,6 +254,11 @@ func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string
|
||||
continue
|
||||
}
|
||||
fallback = file
|
||||
// Literal patterns have no numbered variants to prefer, so
|
||||
// stop as soon as we have a viable fallback.
|
||||
if !hasWildcard {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if fallback != "" {
|
||||
|
||||
@@ -170,46 +170,29 @@ 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},
|
||||
}
|
||||
DescribeTable("numbered match wins over shared fallback within a pattern",
|
||||
func(discNumber, expectedIdx int) {
|
||||
files := []string{
|
||||
createFile("album/disc.jpg"),
|
||||
createFile("album/disc1.jpg"),
|
||||
createFile("album/disc2.jpg"),
|
||||
}
|
||||
reader := &discArtworkReader{
|
||||
discNumber: discNumber,
|
||||
imgFiles: files,
|
||||
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))
|
||||
})
|
||||
sf := reader.fromExternalFile(ctx, "disc*.*")
|
||||
r, path, err := sf()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(r).ToNot(BeNil())
|
||||
r.Close()
|
||||
Expect(path).To(Equal(files[expectedIdx]))
|
||||
},
|
||||
Entry("disc 2 picks disc2.jpg over the shared disc.jpg", 2, 2),
|
||||
Entry("disc 3 falls back to disc.jpg when no numbered match exists", 3, 0),
|
||||
)
|
||||
|
||||
It("matches file without number in multi-folder album by folder", func() {
|
||||
f1 := createFile("album/cd1/disc.jpg")
|
||||
|
||||
Reference in new issue
Block a user