Skip trying to read cover art from mediafile if it does not have one

This commit is contained in:
Deluan 2022-12-20 21:44:11 -05:00 committed by Deluan Quintão
parent 2923f01cd9
commit bce7b163ba
3 changed files with 21 additions and 11 deletions

View File

@ -113,11 +113,15 @@ func (a *artwork) extractMediaFileImage(ctx context.Context, artID model.Artwork
return nil, "" return nil, ""
} }
return extractImage(ctx, artID, var ff []fromFunc
fromTag(mf.Path), if mf.HasCoverArt {
fromFFmpegTag(ctx, a.ffmpeg, mf.Path), ff = []fromFunc{
a.fromAlbum(ctx, mf.AlbumCoverArtID()), fromTag(mf.Path),
) fromFFmpegTag(ctx, a.ffmpeg, mf.Path),
}
}
ff = append(ff, a.fromAlbum(ctx, mf.AlbumCoverArtID()))
return extractImage(ctx, artID, ff...)
} }
func (a *artwork) resizedFromOriginal(ctx context.Context, artID model.ArtworkID, size int) (io.ReadCloser, string, error) { func (a *artwork) resizedFromOriginal(ctx context.Context, artID model.ArtworkID, size int) (io.ReadCloser, string, error) {
@ -225,7 +229,13 @@ func fromFFmpegTag(ctx context.Context, ffmpeg ffmpeg.FFmpeg, path string) fromF
if err != nil { if err != nil {
return nil, "" return nil, ""
} }
return r, path defer r.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, r)
if err != nil {
return nil, ""
}
return io.NopCloser(buf), path
} }
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"image" "image"
"io"
"github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest" "github.com/navidrome/navidrome/conf/configtest"
@ -40,7 +41,7 @@ var _ = Describe("Artwork", func() {
conf.Server.ImageCacheSize = "0" // Disable cache conf.Server.ImageCacheSize = "0" // Disable cache
cache := GetImageCache() cache := GetImageCache()
ffmpeg = tests.NewMockFFmpeg("") ffmpeg = tests.NewMockFFmpeg("content from ffmpeg")
aw = NewArtwork(ds, cache, ffmpeg).(*artwork) aw = NewArtwork(ds, cache, ffmpeg).(*artwork)
}) })
@ -129,8 +130,9 @@ var _ = Describe("Artwork", func() {
Expect(path).To(Equal("tests/fixtures/test.mp3")) Expect(path).To(Equal("tests/fixtures/test.mp3"))
}) })
It("returns embed cover if successfully extracted by ffmpeg", func() { It("returns embed cover if successfully extracted by ffmpeg", func() {
_, path, err := aw.get(context.Background(), mfCorruptedCover.CoverArtID(), 0) r, path, err := aw.get(context.Background(), mfCorruptedCover.CoverArtID(), 0)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(io.ReadAll(r)).To(Equal([]byte("content from ffmpeg")))
Expect(path).To(Equal("tests/fixtures/test.ogg")) Expect(path).To(Equal("tests/fixtures/test.ogg"))
}) })
It("returns album cover if cannot read embed artwork", func() { It("returns album cover if cannot read embed artwork", func() {

View File

@ -6,8 +6,6 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/resources"
"github.com/navidrome/navidrome/utils" "github.com/navidrome/navidrome/utils"
) )
@ -33,7 +31,7 @@ func (ff *MockFFmpeg) ExtractImage(ctx context.Context, path string) (io.ReadClo
if ff.Error != nil { if ff.Error != nil {
return nil, ff.Error return nil, ff.Error
} }
return resources.FS().Open(consts.PlaceholderAlbumArt) return ff, nil
} }
func (ff *MockFFmpeg) Read(p []byte) (n int, err error) { func (ff *MockFFmpeg) Read(p []byte) (n int, err error) {