Fallback to album art if mediaFile does not have cover art
This commit is contained in:
+16
-24
@@ -41,7 +41,6 @@ type cover struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *cover) Get(ctx context.Context, id string, size int, out io.Writer) error {
|
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)
|
path, lastUpdate, err := c.getCoverPath(ctx, id)
|
||||||
if err != nil && err != model.ErrNotFound {
|
if err != nil && err != model.ErrNotFound {
|
||||||
return err
|
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) {
|
func (c *cover) getCoverPath(ctx context.Context, id string) (path string, lastUpdated time.Time, err error) {
|
||||||
var found bool
|
// If id is an album cover ID
|
||||||
if found, err = c.ds.Album(ctx).Exists(id); err != nil {
|
if strings.HasPrefix(id, "al-") {
|
||||||
return
|
id = strings.TrimPrefix(id, "al-")
|
||||||
}
|
|
||||||
var coverPath string
|
|
||||||
if found {
|
|
||||||
var al *model.Album
|
var al *model.Album
|
||||||
al, err = c.ds.Album(ctx).Get(id)
|
al, err = c.ds.Album(ctx).Get(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -101,26 +97,22 @@ func (c *cover) getCoverPath(ctx context.Context, id string) (path string, lastU
|
|||||||
}
|
}
|
||||||
if al.CoverArtId == "" {
|
if al.CoverArtId == "" {
|
||||||
err = model.ErrNotFound
|
err = model.ErrNotFound
|
||||||
return
|
|
||||||
}
|
}
|
||||||
id = al.CoverArtId
|
return al.CoverArtPath, al.UpdatedAt, err
|
||||||
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 "", 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 {
|
func imageCacheKey(path string, size int, lastUpdate time.Time) string {
|
||||||
|
|||||||
+19
-19
@@ -19,8 +19,8 @@ var _ = Describe("Cover", func() {
|
|||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}}
|
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.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", "path": "tests/fixtures/test.mp3", "hasCoverArt": true, "updatedAt":"2020-04-02T21:29:31.6377Z"}]`)
|
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() {
|
Context("Cache is configured", func() {
|
||||||
@@ -28,17 +28,17 @@ var _ = Describe("Cover", func() {
|
|||||||
cover = NewCover(ds, testCache)
|
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)
|
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()))
|
_, format, err := image.Decode(bytes.NewReader(buf.Bytes()))
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
Expect(format).To(Equal("jpeg"))
|
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)
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
Expect(cover.Get(ctx, "al-222", 0, buf)).To(BeNil())
|
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() {
|
It("returns the default cover if album does not have cover", func() {
|
||||||
buf := new(bytes.Buffer)
|
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()))
|
_, format, err := image.Decode(bytes.NewReader(buf.Bytes()))
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
Expect(format).To(Equal("png"))
|
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() {
|
It("returns the default cover if album is not found", func() {
|
||||||
buf := new(bytes.Buffer)
|
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()))
|
_, format, err := image.Decode(bytes.NewReader(buf.Bytes()))
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
@@ -90,6 +80,16 @@ var _ = Describe("Cover", func() {
|
|||||||
Expect(img.Bounds().Size().Y).To(Equal(600))
|
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() {
|
It("resized cover art as requested", func() {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
@@ -107,7 +107,7 @@ var _ = Describe("Cover", func() {
|
|||||||
ds.Album(ctx).(*persistence.MockAlbum).SetError(true)
|
ds.Album(ctx).(*persistence.MockAlbum).SetError(true)
|
||||||
buf := new(bytes.Buffer)
|
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() {
|
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() {
|
It("retrieves the original cover art from an album", func() {
|
||||||
buf := new(bytes.Buffer)
|
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()))
|
_, format, err := image.Decode(bytes.NewReader(buf.Bytes()))
|
||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
|
|||||||
Reference in New Issue
Block a user