2020-07-10 08:45:58 -08:00
|
|
|
package core
|
2020-04-05 16:31:05 -08:00
|
|
|
|
|
|
|
|
import (
|
2020-04-26 08:06:38 -08:00
|
|
|
"context"
|
2020-04-05 16:31:05 -08:00
|
|
|
"image"
|
2021-07-20 16:00:58 -08:00
|
|
|
"os"
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-07-24 09:30:27 -08:00
|
|
|
"github.com/navidrome/navidrome/conf"
|
2020-04-05 16:31:05 -08:00
|
|
|
"github.com/navidrome/navidrome/log"
|
|
|
|
|
"github.com/navidrome/navidrome/model"
|
2020-10-27 07:01:40 -08:00
|
|
|
"github.com/navidrome/navidrome/tests"
|
2020-04-05 16:31:05 -08:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
)
|
|
|
|
|
|
2020-07-31 05:31:19 -08:00
|
|
|
var _ = Describe("Artwork", func() {
|
|
|
|
|
var artwork Artwork
|
2020-04-05 16:31:05 -08:00
|
|
|
var ds model.DataStore
|
2020-04-26 08:06:38 -08:00
|
|
|
ctx := log.NewContext(context.TODO())
|
2020-04-05 16:31:05 -08:00
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2021-06-08 12:30:19 -08:00
|
|
|
ds = &tests.MockDataStore{MockedTranscoding: &tests.MockTranscodingRepo{}}
|
|
|
|
|
ds.Album(ctx).(*tests.MockAlbumRepo).SetData(model.Albums{
|
2020-10-27 06:48:37 -08:00
|
|
|
{ID: "222", CoverArtId: "123", CoverArtPath: "tests/fixtures/test.mp3"},
|
|
|
|
|
{ID: "333", CoverArtId: ""},
|
|
|
|
|
{ID: "444", CoverArtId: "444", CoverArtPath: "tests/fixtures/cover.jpg"},
|
|
|
|
|
})
|
2021-06-08 12:30:19 -08:00
|
|
|
ds.MediaFile(ctx).(*tests.MockMediaFileRepo).SetData(model.MediaFiles{
|
2020-10-27 06:48:37 -08:00
|
|
|
{ID: "123", AlbumID: "222", Path: "tests/fixtures/test.mp3", HasCoverArt: true},
|
|
|
|
|
{ID: "456", AlbumID: "222", Path: "tests/fixtures/test.ogg", HasCoverArt: false},
|
|
|
|
|
})
|
2020-04-05 16:31:05 -08:00
|
|
|
})
|
|
|
|
|
|
2020-04-14 15:15:47 -08:00
|
|
|
Context("Cache is configured", func() {
|
|
|
|
|
BeforeEach(func() {
|
2021-07-20 16:00:58 -08:00
|
|
|
conf.Server.DataFolder, _ = os.MkdirTemp("", "file_caches")
|
2020-07-24 09:30:27 -08:00
|
|
|
conf.Server.ImageCacheSize = "100MB"
|
2020-10-27 13:02:20 -08:00
|
|
|
cache := GetImageCache()
|
2020-10-30 20:35:20 -08:00
|
|
|
Eventually(func() bool { return cache.Ready(context.TODO()) }).Should(BeTrue())
|
2020-07-31 05:31:19 -08:00
|
|
|
artwork = NewArtwork(ds, cache)
|
2020-07-24 09:30:27 -08:00
|
|
|
})
|
|
|
|
|
|
2020-07-31 05:31:19 -08:00
|
|
|
It("retrieves the external artwork art for an album", func() {
|
2020-11-17 09:30:37 -09:00
|
|
|
r, err := artwork.Get(ctx, "al-444", 0)
|
|
|
|
|
Expect(err).To(BeNil())
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-11-17 09:30:37 -09:00
|
|
|
_, format, err := image.Decode(r)
|
2020-04-14 15:15:47 -08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(format).To(Equal("jpeg"))
|
2020-11-17 09:30:37 -09:00
|
|
|
Expect(r.Close()).To(BeNil())
|
2020-04-14 15:15:47 -08:00
|
|
|
})
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-07-31 05:31:19 -08:00
|
|
|
It("retrieves the embedded artwork art for an album", func() {
|
2020-11-17 09:30:37 -09:00
|
|
|
r, err := artwork.Get(ctx, "al-222", 0)
|
|
|
|
|
Expect(err).To(BeNil())
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-11-17 09:30:37 -09:00
|
|
|
_, format, err := image.Decode(r)
|
2020-04-14 15:15:47 -08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(format).To(Equal("jpeg"))
|
2020-11-17 09:30:37 -09:00
|
|
|
Expect(r.Close()).To(BeNil())
|
2020-04-14 15:15:47 -08:00
|
|
|
})
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-07-31 05:31:19 -08:00
|
|
|
It("returns the default artwork if album does not have artwork", func() {
|
2020-11-17 09:30:37 -09:00
|
|
|
r, err := artwork.Get(ctx, "al-333", 0)
|
|
|
|
|
Expect(err).To(BeNil())
|
2020-04-14 15:15:47 -08:00
|
|
|
|
2020-11-17 09:30:37 -09:00
|
|
|
_, format, err := image.Decode(r)
|
2020-04-14 15:15:47 -08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(format).To(Equal("png"))
|
2020-11-17 09:30:37 -09:00
|
|
|
Expect(r.Close()).To(BeNil())
|
2020-04-14 15:15:47 -08:00
|
|
|
})
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-07-31 05:31:19 -08:00
|
|
|
It("returns the default artwork if album is not found", func() {
|
2020-11-17 09:30:37 -09:00
|
|
|
r, err := artwork.Get(ctx, "al-0101", 0)
|
|
|
|
|
Expect(err).To(BeNil())
|
2020-06-21 09:31:14 -08:00
|
|
|
|
2020-11-17 09:30:37 -09:00
|
|
|
_, format, err := image.Decode(r)
|
2020-04-14 15:15:47 -08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(format).To(Equal("png"))
|
2020-11-17 09:30:37 -09:00
|
|
|
Expect(r.Close()).To(BeNil())
|
2020-04-14 15:15:47 -08:00
|
|
|
})
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-07-31 05:31:19 -08:00
|
|
|
It("retrieves the original artwork art from a media_file", func() {
|
2020-11-17 09:30:37 -09:00
|
|
|
r, err := artwork.Get(ctx, "123", 0)
|
|
|
|
|
Expect(err).To(BeNil())
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-11-17 09:30:37 -09:00
|
|
|
img, format, err := image.Decode(r)
|
2020-04-14 15:15:47 -08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(format).To(Equal("jpeg"))
|
|
|
|
|
Expect(img.Bounds().Size().X).To(Equal(600))
|
|
|
|
|
Expect(img.Bounds().Size().Y).To(Equal(600))
|
2020-11-17 09:30:37 -09:00
|
|
|
Expect(r.Close()).To(BeNil())
|
2020-04-14 15:15:47 -08:00
|
|
|
})
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-07-31 05:31:19 -08:00
|
|
|
It("retrieves the album artwork art if media_file does not have one", func() {
|
2020-11-17 09:30:37 -09:00
|
|
|
r, err := artwork.Get(ctx, "456", 0)
|
|
|
|
|
Expect(err).To(BeNil())
|
2020-08-13 19:56:06 -08:00
|
|
|
|
2020-11-17 09:30:37 -09:00
|
|
|
_, format, err := image.Decode(r)
|
2020-08-13 19:56:06 -08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(format).To(Equal("jpeg"))
|
2020-11-17 09:30:37 -09:00
|
|
|
Expect(r.Close()).To(BeNil())
|
2020-08-13 19:56:06 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("retrieves the album artwork by album id", func() {
|
2020-11-17 09:30:37 -09:00
|
|
|
r, err := artwork.Get(ctx, "222", 0)
|
|
|
|
|
Expect(err).To(BeNil())
|
2020-06-27 09:11:51 -08:00
|
|
|
|
2020-11-17 09:30:37 -09:00
|
|
|
_, format, err := image.Decode(r)
|
2020-06-27 09:11:51 -08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(format).To(Equal("jpeg"))
|
2020-11-17 09:30:37 -09:00
|
|
|
Expect(r.Close()).To(BeNil())
|
2020-06-27 09:11:51 -08:00
|
|
|
})
|
|
|
|
|
|
2020-07-31 05:31:19 -08:00
|
|
|
It("resized artwork art as requested", func() {
|
2020-11-17 09:30:37 -09:00
|
|
|
r, err := artwork.Get(ctx, "123", 200)
|
|
|
|
|
Expect(err).To(BeNil())
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-11-17 09:30:37 -09:00
|
|
|
img, format, err := image.Decode(r)
|
2020-04-14 15:15:47 -08:00
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
Expect(format).To(Equal("jpeg"))
|
|
|
|
|
Expect(img.Bounds().Size().X).To(Equal(200))
|
|
|
|
|
Expect(img.Bounds().Size().Y).To(Equal(200))
|
2020-11-17 09:30:37 -09:00
|
|
|
Expect(r.Close()).To(BeNil())
|
2020-04-14 15:15:47 -08:00
|
|
|
})
|
2020-04-05 16:31:05 -08:00
|
|
|
|
2020-04-14 15:15:47 -08:00
|
|
|
Context("Errors", func() {
|
|
|
|
|
It("returns err if gets error from album table", func() {
|
2021-06-08 12:30:19 -08:00
|
|
|
ds.Album(ctx).(*tests.MockAlbumRepo).SetError(true)
|
2020-11-17 09:30:37 -09:00
|
|
|
_, err := artwork.Get(ctx, "al-222", 0)
|
|
|
|
|
Expect(err).To(MatchError("Error!"))
|
2020-04-14 15:15:47 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
It("returns err if gets error from media_file table", func() {
|
2021-06-08 12:30:19 -08:00
|
|
|
ds.MediaFile(ctx).(*tests.MockMediaFileRepo).SetError(true)
|
2020-11-17 09:30:37 -09:00
|
|
|
_, err := artwork.Get(ctx, "123", 0)
|
|
|
|
|
Expect(err).To(MatchError("Error!"))
|
2020-04-14 15:15:47 -08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-04-05 16:31:05 -08:00
|
|
|
})
|