From 703875b89586dd5d6e972d5ec905c197394770d6 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 27 Jun 2020 13:11:51 -0400 Subject: [PATCH] Fallback to album art if mediaFile does not have cover art --- engine/cover.go | 40 ++++++++++++++++------------------------ engine/cover_test.go | 38 +++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/engine/cover.go b/engine/cover.go index 7712ee804..055147495 100644 --- a/engine/cover.go +++ b/engine/cover.go @@ -41,7 +41,6 @@ type cover struct { } func (c *cover) Get(ctx context.Context, id string, size int, out io.Writer) error { - id = strings.TrimPrefix(id, "al-") path, lastUpdate, err := c.getCoverPath(ctx, id) if err != nil && err != model.ErrNotFound { return err @@ -88,12 +87,9 @@ func (c *cover) Get(ctx context.Context, id string, size int, out io.Writer) err } func (c *cover) getCoverPath(ctx context.Context, id string) (path string, lastUpdated time.Time, err error) { - var found bool - if found, err = c.ds.Album(ctx).Exists(id); err != nil { - return - } - var coverPath string - if found { + // If id is an album cover ID + if strings.HasPrefix(id, "al-") { + id = strings.TrimPrefix(id, "al-") var al *model.Album al, err = c.ds.Album(ctx).Get(id) if err != nil { @@ -101,26 +97,22 @@ func (c *cover) getCoverPath(ctx context.Context, id string) (path string, lastU } if al.CoverArtId == "" { err = model.ErrNotFound - return } - id = al.CoverArtId - coverPath = al.CoverArtPath - } - var mf *model.MediaFile - mf, err = c.ds.MediaFile(ctx).Get(id) - if err == nil && mf.HasCoverArt { - return mf.Path, mf.UpdatedAt, nil - } else if err != nil && coverPath != "" { - info, err := os.Stat(coverPath) - if err != nil { - return "", time.Time{}, model.ErrNotFound - } - return coverPath, info.ModTime(), nil - } else if err != nil { - return + return al.CoverArtPath, al.UpdatedAt, err } - return "", time.Time{}, model.ErrNotFound + // if id is a mediafile cover id + var mf *model.MediaFile + mf, err = c.ds.MediaFile(ctx).Get(id) + if err != nil { + return + } + if mf.HasCoverArt { + return mf.Path, mf.UpdatedAt, nil + } + + // if the mediafile does not have a coverArt, fallback to the album cover + return c.getCoverPath(ctx, "al-"+mf.AlbumID) } func imageCacheKey(path string, size int, lastUpdate time.Time) string { diff --git a/engine/cover_test.go b/engine/cover_test.go index 5a4944454..9bd7f934d 100644 --- a/engine/cover_test.go +++ b/engine/cover_test.go @@ -19,8 +19,8 @@ var _ = Describe("Cover", func() { BeforeEach(func() { ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}} - ds.Album(ctx).(*persistence.MockAlbum).SetData(`[{"id": "222", "coverArtId": "123"}, {"id": "333", "coverArtId": ""}, {"id": "444", "coverArtId": "444", "coverArtPath": "tests/fixtures/cover.jpg"}]`) - ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "path": "tests/fixtures/test.mp3", "hasCoverArt": true, "updatedAt":"2020-04-02T21:29:31.6377Z"}]`) + ds.Album(ctx).(*persistence.MockAlbum).SetData(`[{"id": "222", "coverArtId": "123", "coverArtPath":"tests/fixtures/test.mp3"}, {"id": "333", "coverArtId": ""}, {"id": "444", "coverArtId": "444", "coverArtPath": "tests/fixtures/cover.jpg"}]`) + ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "albumId": "222", "path": "tests/fixtures/test.mp3", "hasCoverArt": true, "updatedAt":"2020-04-02T21:29:31.6377Z"},{"id": "456", "albumId": "222", "path": "tests/fixtures/test.ogg", "hasCoverArt": false, "updatedAt":"2020-04-02T21:29:31.6377Z"}]`) }) Context("Cache is configured", func() { @@ -28,17 +28,17 @@ var _ = Describe("Cover", func() { cover = NewCover(ds, testCache) }) - It("retrieves the original cover art from an album", func() { + It("retrieves the external cover art for an album", func() { buf := new(bytes.Buffer) - Expect(cover.Get(ctx, "222", 0, buf)).To(BeNil()) + Expect(cover.Get(ctx, "al-444", 0, buf)).To(BeNil()) _, format, err := image.Decode(bytes.NewReader(buf.Bytes())) Expect(err).To(BeNil()) Expect(format).To(Equal("jpeg")) }) - It("accepts albumIds with 'al-' prefix", func() { + It("retrieves the embedded cover art for an album", func() { buf := new(bytes.Buffer) Expect(cover.Get(ctx, "al-222", 0, buf)).To(BeNil()) @@ -51,27 +51,17 @@ var _ = Describe("Cover", func() { It("returns the default cover if album does not have cover", func() { buf := new(bytes.Buffer) - Expect(cover.Get(ctx, "333", 0, buf)).To(BeNil()) + Expect(cover.Get(ctx, "al-333", 0, buf)).To(BeNil()) _, format, err := image.Decode(bytes.NewReader(buf.Bytes())) Expect(err).To(BeNil()) Expect(format).To(Equal("png")) }) - It("returns the external cover for the album", func() { - buf := new(bytes.Buffer) - - Expect(cover.Get(ctx, "444", 0, buf)).To(BeNil()) - - _, format, err := image.Decode(bytes.NewReader(buf.Bytes())) - Expect(err).To(BeNil()) - Expect(format).To(Equal("jpeg")) - }) - It("returns the default cover if album is not found", func() { buf := new(bytes.Buffer) - Expect(cover.Get(ctx, "0101", 0, buf)).To(BeNil()) + Expect(cover.Get(ctx, "al-0101", 0, buf)).To(BeNil()) _, format, err := image.Decode(bytes.NewReader(buf.Bytes())) Expect(err).To(BeNil()) @@ -90,6 +80,16 @@ var _ = Describe("Cover", func() { Expect(img.Bounds().Size().Y).To(Equal(600)) }) + It("retrieves the album cover art if media_file does not have one", func() { + buf := new(bytes.Buffer) + + Expect(cover.Get(ctx, "456", 0, buf)).To(BeNil()) + + _, format, err := image.Decode(bytes.NewReader(buf.Bytes())) + Expect(err).To(BeNil()) + Expect(format).To(Equal("jpeg")) + }) + It("resized cover art as requested", func() { buf := new(bytes.Buffer) @@ -107,7 +107,7 @@ var _ = Describe("Cover", func() { ds.Album(ctx).(*persistence.MockAlbum).SetError(true) buf := new(bytes.Buffer) - Expect(cover.Get(ctx, "222", 0, buf)).To(MatchError("Error!")) + Expect(cover.Get(ctx, "al-222", 0, buf)).To(MatchError("Error!")) }) It("returns err if gets error from media_file table", func() { @@ -126,7 +126,7 @@ var _ = Describe("Cover", func() { It("retrieves the original cover art from an album", func() { buf := new(bytes.Buffer) - Expect(cover.Get(ctx, "222", 0, buf)).To(BeNil()) + Expect(cover.Get(ctx, "al-222", 0, buf)).To(BeNil()) _, format, err := image.Decode(bytes.NewReader(buf.Bytes())) Expect(err).To(BeNil())