mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 13:07:36 +03:00
Allowing album cover ids with prefix (al-)
This commit is contained in:
parent
9fd530b78a
commit
a7ccd76d54
5 changed files with 43 additions and 7 deletions
|
@ -143,6 +143,7 @@ func (b *browser) buildAlbumDir(al *domain.Album, tracks domain.MediaFiles) *Dir
|
|||
Created: al.CreatedAt,
|
||||
Year: al.Year,
|
||||
Genre: al.Genre,
|
||||
CoverArt: al.CoverArtId,
|
||||
}
|
||||
|
||||
dir.Entries = make(Entries, len(tracks))
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
_ "image/png"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"github.com/dhowden/tag"
|
||||
|
@ -20,22 +21,44 @@ type Cover interface {
|
|||
|
||||
type cover struct {
|
||||
mfileRepo domain.MediaFileRepository
|
||||
albumRepo domain.AlbumRepository
|
||||
}
|
||||
|
||||
func NewCover(mr domain.MediaFileRepository) Cover {
|
||||
return &cover{mr}
|
||||
func NewCover(mr domain.MediaFileRepository, alr domain.AlbumRepository) Cover {
|
||||
return &cover{mr, alr}
|
||||
}
|
||||
|
||||
func (c *cover) getCoverPath(id string) (string, error) {
|
||||
switch {
|
||||
case strings.HasPrefix(id, "al-"):
|
||||
id = id[3:]
|
||||
al, err := c.albumRepo.Get(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return al.CoverArtPath, nil
|
||||
default:
|
||||
mf, err := c.mfileRepo.Get(id)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if mf.HasCoverArt {
|
||||
return mf.Path, nil
|
||||
}
|
||||
}
|
||||
return "", domain.ErrNotFound
|
||||
}
|
||||
|
||||
func (c *cover) Get(id string, size int, out io.Writer) error {
|
||||
mf, err := c.mfileRepo.Get(id)
|
||||
path, err := c.getCoverPath(id)
|
||||
if err != nil && err != domain.ErrNotFound {
|
||||
return err
|
||||
}
|
||||
|
||||
var reader io.Reader
|
||||
|
||||
if err == nil && mf.HasCoverArt {
|
||||
reader, err = readFromTag(mf.Path)
|
||||
if err != domain.ErrNotFound {
|
||||
reader, err = readFromTag(path)
|
||||
} else {
|
||||
var f *os.File
|
||||
f, err = os.Open("static/default_cover.jpg")
|
||||
|
|
|
@ -17,8 +17,9 @@ func TestCover(t *testing.T) {
|
|||
Init(t, false)
|
||||
|
||||
mockMediaFileRepo := persistence.CreateMockMediaFileRepo()
|
||||
mockAlbumRepo := persistence.CreateMockAlbumRepo()
|
||||
|
||||
cover := engine.NewCover(mockMediaFileRepo)
|
||||
cover := engine.NewCover(mockMediaFileRepo, mockAlbumRepo)
|
||||
out := new(bytes.Buffer)
|
||||
|
||||
Convey("Subject: GetCoverArt Endpoint", t, func() {
|
||||
|
@ -70,6 +71,16 @@ func TestCover(t *testing.T) {
|
|||
So(img.Bounds().Max.Y, ShouldEqual, 100)
|
||||
})
|
||||
})
|
||||
Convey("When id is for an album", func() {
|
||||
mockAlbumRepo.SetData(`[{"Id":"1","CoverArtPath":"tests/fixtures/01 Invisible (RED) Edit Version.mp3"}]`, 1)
|
||||
err := cover.Get("al-1", 0, out)
|
||||
|
||||
Convey("Then it should return the cover for the album", func() {
|
||||
So(err, ShouldBeNil)
|
||||
So(out.Bytes(), ShouldMatchMD5, "e859a71cd1b1aaeb1ad437d85b306668")
|
||||
})
|
||||
})
|
||||
|
||||
Reset(func() {
|
||||
mockMediaFileRepo.SetData("[]", 0)
|
||||
mockMediaFileRepo.SetError(false)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue