Fix spaces in CoverArtPriority, more trace logs in artwork resolution

This commit is contained in:
Deluan 2022-12-23 09:53:13 -05:00 committed by Deluan Quintão
parent 3dbd5c8d31
commit f5719a7571
2 changed files with 33 additions and 32 deletions

View File

@ -75,7 +75,7 @@ func (a *artwork) Get(ctx context.Context, id string, size int) (io.ReadCloser,
return r, err return r, err
} }
type fromFunc func() (io.ReadCloser, string) type fromFunc func() (io.ReadCloser, string, error)
func (f fromFunc) String() string { func (f fromFunc) String() string {
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
@ -91,7 +91,7 @@ func (a *artwork) get(ctx context.Context, artID model.ArtworkID, size int) (rea
case model.KindMediaFileArtwork: case model.KindMediaFileArtwork:
reader, path = a.extractMediaFileImage(ctx, artID) reader, path = a.extractMediaFileImage(ctx, artID)
default: default:
reader, path = fromPlaceholder()() reader, path, _ = fromPlaceholder()()
} }
return reader, path, ctx.Err() return reader, path, ctx.Err()
} }
@ -99,7 +99,7 @@ func (a *artwork) get(ctx context.Context, artID model.ArtworkID, size int) (rea
func (a *artwork) extractAlbumImage(ctx context.Context, artID model.ArtworkID) (io.ReadCloser, string) { func (a *artwork) extractAlbumImage(ctx context.Context, artID model.ArtworkID) (io.ReadCloser, string) {
al, err := a.ds.Album(ctx).Get(artID.ID) al, err := a.ds.Album(ctx).Get(artID.ID)
if errors.Is(err, model.ErrNotFound) { if errors.Is(err, model.ErrNotFound) {
r, path := fromPlaceholder()() r, path, _ := fromPlaceholder()()
return r, path return r, path
} }
if err != nil { if err != nil {
@ -114,7 +114,7 @@ func (a *artwork) extractAlbumImage(ctx context.Context, artID model.ArtworkID)
func (a *artwork) extractMediaFileImage(ctx context.Context, artID model.ArtworkID) (reader io.ReadCloser, path string) { func (a *artwork) extractMediaFileImage(ctx context.Context, artID model.ArtworkID) (reader io.ReadCloser, path string) {
mf, err := a.ds.MediaFile(ctx).Get(artID.ID) mf, err := a.ds.MediaFile(ctx).Get(artID.ID)
if errors.Is(err, model.ErrNotFound) { if errors.Is(err, model.ErrNotFound) {
r, path := fromPlaceholder()() r, path, _ := fromPlaceholder()()
return r, path return r, path
} }
if err != nil { if err != nil {
@ -153,41 +153,42 @@ func extractImage(ctx context.Context, artID model.ArtworkID, extractFuncs ...fr
if ctx.Err() != nil { if ctx.Err() != nil {
return nil, "" return nil, ""
} }
r, path := f() r, path, err := f()
if r != nil { if r != nil {
log.Trace(ctx, "Found artwork", "artID", artID, "path", path, "origin", f) log.Trace(ctx, "Found artwork", "artID", artID, "path", path, "origin", f)
return r, path return r, path
} }
log.Trace(ctx, "Tried to extract artwork", "artID", artID, "origin", f, err)
} }
log.Error(ctx, "extractImage should never reach this point!", "artID", artID, "path") log.Error(ctx, "extractImage should never reach this point!", "artID", artID, "path")
return nil, "" return nil, ""
} }
func (a *artwork) fromAlbum(ctx context.Context, id model.ArtworkID) fromFunc { func (a *artwork) fromAlbum(ctx context.Context, id model.ArtworkID) fromFunc {
return func() (io.ReadCloser, string) { return func() (io.ReadCloser, string, error) {
r, path, err := a.get(ctx, id, 0) r, path, err := a.get(ctx, id, 0)
if err != nil { if err != nil {
return nil, "" return nil, "", err
} }
return r, path return r, path, nil
} }
} }
func (a *artwork) fromCoverArtPriority(ctx context.Context, priority string, al model.Album) []fromFunc { func (a *artwork) fromCoverArtPriority(ctx context.Context, priority string, al model.Album) []fromFunc {
var ff []fromFunc var ff []fromFunc
for _, p := range strings.Split(strings.ToLower(priority), ",") { for _, pattern := range strings.Split(strings.ToLower(priority), ",") {
pat := strings.TrimSpace(p) pattern = strings.TrimSpace(pattern)
if pat == "embedded" { if pattern == "embedded" {
ff = append(ff, fromTag(al.EmbedArtPath), fromFFmpegTag(ctx, a.ffmpeg, al.EmbedArtPath)) ff = append(ff, fromTag(al.EmbedArtPath), fromFFmpegTag(ctx, a.ffmpeg, al.EmbedArtPath))
continue continue
} }
ff = append(ff, fromExternalFile(ctx, al.ImageFiles, p)) ff = append(ff, fromExternalFile(ctx, al.ImageFiles, pattern))
} }
return ff return ff
} }
func fromExternalFile(ctx context.Context, files string, pattern string) fromFunc { func fromExternalFile(ctx context.Context, files string, pattern string) fromFunc {
return func() (io.ReadCloser, string) { return func() (io.ReadCloser, string, error) {
for _, file := range filepath.SplitList(files) { for _, file := range filepath.SplitList(files) {
_, name := filepath.Split(file) _, name := filepath.Split(file)
match, err := filepath.Match(pattern, strings.ToLower(name)) match, err := filepath.Match(pattern, strings.ToLower(name))
@ -202,59 +203,59 @@ func fromExternalFile(ctx context.Context, files string, pattern string) fromFun
if err != nil { if err != nil {
continue continue
} }
return f, file return f, file, err
} }
return nil, "" return nil, "", fmt.Errorf("pattern '%s' not matched by files %v", pattern, files)
} }
} }
func fromTag(path string) fromFunc { func fromTag(path string) fromFunc {
return func() (io.ReadCloser, string) { return func() (io.ReadCloser, string, error) {
if path == "" { if path == "" {
return nil, "" return nil, "", nil
} }
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return nil, "" return nil, "", err
} }
defer f.Close() defer f.Close()
m, err := tag.ReadFrom(f) m, err := tag.ReadFrom(f)
if err != nil { if err != nil {
return nil, "" return nil, "", err
} }
picture := m.Picture() picture := m.Picture()
if picture == nil { if picture == nil {
return nil, "" return nil, "", fmt.Errorf("no embedded image found in %s", path)
} }
return io.NopCloser(bytes.NewReader(picture.Data)), path return io.NopCloser(bytes.NewReader(picture.Data)), path, nil
} }
} }
func fromFFmpegTag(ctx context.Context, ffmpeg ffmpeg.FFmpeg, path string) fromFunc { func fromFFmpegTag(ctx context.Context, ffmpeg ffmpeg.FFmpeg, path string) fromFunc {
return func() (io.ReadCloser, string) { return func() (io.ReadCloser, string, error) {
if path == "" { if path == "" {
return nil, "" return nil, "", nil
} }
r, err := ffmpeg.ExtractImage(ctx, path) r, err := ffmpeg.ExtractImage(ctx, path)
if err != nil { if err != nil {
return nil, "" return nil, "", err
} }
defer r.Close() defer r.Close()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
_, err = io.Copy(buf, r) _, err = io.Copy(buf, r)
if err != nil { if err != nil {
return nil, "" return nil, "", err
} }
return io.NopCloser(buf), path return io.NopCloser(buf), path, nil
} }
} }
func fromPlaceholder() fromFunc { func fromPlaceholder() fromFunc {
return func() (io.ReadCloser, string) { return func() (io.ReadCloser, string, error) {
r, _ := resources.FS().Open(consts.PlaceholderAlbumArt) r, _ := resources.FS().Open(consts.PlaceholderAlbumArt)
return r, consts.PlaceholderAlbumArt return r, consts.PlaceholderAlbumArt, nil
} }
} }

View File

@ -27,7 +27,7 @@ var _ = Describe("Artwork", func() {
BeforeEach(func() { BeforeEach(func() {
DeferCleanup(configtest.SetupConfig()) DeferCleanup(configtest.SetupConfig())
conf.Server.ImageCacheSize = "0" // Disable cache conf.Server.ImageCacheSize = "0" // Disable cache
conf.Server.CoverArtPriority = "folder.*,cover.*,embedded,front.*" conf.Server.CoverArtPriority = "folder.*, cover.*, embedded , front.*"
ds = &tests.MockDataStore{MockedTranscoding: &tests.MockTranscodingRepo{}} ds = &tests.MockDataStore{MockedTranscoding: &tests.MockTranscodingRepo{}}
alOnlyEmbed = model.Album{ID: "222", Name: "Only embed", EmbedArtPath: "tests/fixtures/test.mp3"} alOnlyEmbed = model.Album{ID: "222", Name: "Only embed", EmbedArtPath: "tests/fixtures/test.mp3"}
@ -111,9 +111,9 @@ var _ = Describe("Artwork", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal(expected)) Expect(path).To(Equal(expected))
}, },
Entry(nil, "folder.*,cover.*,embedded,front.*", "tests/fixtures/cover.jpg"), Entry(nil, " folder.* , cover.*,embedded,front.*", "tests/fixtures/cover.jpg"),
Entry(nil, "front.*,cover.*,embedded,folder.*", "tests/fixtures/front.png"), Entry(nil, "front.* , cover.*, embedded ,folder.*", "tests/fixtures/front.png"),
Entry(nil, "embedded,front.*,cover.*,folder.*", "tests/fixtures/test.mp3"), Entry(nil, " embedded , front.* , cover.*,folder.*", "tests/fixtures/test.mp3"),
) )
}) })
}) })