mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 21:17:37 +03:00
getCoverArt.view working
This commit is contained in:
parent
1b945831cc
commit
c9455e1955
9 changed files with 148 additions and 2 deletions
71
api/get_cover_art.go
Normal file
71
api/get_cover_art.go
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/deluan/gosonic/domain"
|
||||||
|
"github.com/karlkfi/inject"
|
||||||
|
"github.com/deluan/gosonic/utils"
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
"github.com/deluan/gosonic/api/responses"
|
||||||
|
"io/ioutil"
|
||||||
|
"github.com/dhowden/tag"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GetCoverArtController struct {
|
||||||
|
BaseAPIController
|
||||||
|
repo domain.MediaFileRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *GetCoverArtController) Prepare() {
|
||||||
|
inject.ExtractAssignable(utils.Graph, &c.repo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *GetCoverArtController) Get() {
|
||||||
|
id := c.Input().Get("id")
|
||||||
|
if id == "" {
|
||||||
|
c.SendError(responses.ERROR_MISSING_PARAMETER, "id parameter required")
|
||||||
|
}
|
||||||
|
|
||||||
|
mf, err := c.repo.Get(id)
|
||||||
|
if err != nil {
|
||||||
|
beego.Error("Error reading mediafile", id, "from the database", ":", err)
|
||||||
|
c.SendError(responses.ERROR_GENERIC, "Internal error")
|
||||||
|
}
|
||||||
|
|
||||||
|
var img []byte
|
||||||
|
|
||||||
|
if (mf.HasCoverArt) {
|
||||||
|
img, err = readFromTag(mf.Path)
|
||||||
|
beego.Debug("Serving cover art from", mf.Path)
|
||||||
|
} else {
|
||||||
|
img, err = ioutil.ReadFile("static/default_cover.jpg")
|
||||||
|
beego.Debug("Serving default cover art")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
beego.Error("Could not retrieve cover art", id, ":", err)
|
||||||
|
c.SendError(responses.ERROR_DATA_NOT_FOUND, "cover art not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
c.Ctx.Output.ContentType("image/jpg")
|
||||||
|
c.Ctx.Output.Body(img)
|
||||||
|
}
|
||||||
|
|
||||||
|
func readFromTag(path string) ([]byte, error) {
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
beego.Warn("Error opening file", path, "-", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
m, err := tag.ReadFrom(f)
|
||||||
|
if err != nil {
|
||||||
|
beego.Warn("Error reading tag from file", path, "-", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.Picture().Data, nil
|
||||||
|
}
|
||||||
|
|
58
api/get_cover_art_test.go
Normal file
58
api/get_cover_art_test.go
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
package api_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/deluan/gosonic/api/responses"
|
||||||
|
"github.com/deluan/gosonic/domain"
|
||||||
|
. "github.com/deluan/gosonic/tests"
|
||||||
|
"github.com/deluan/gosonic/tests/mocks"
|
||||||
|
"github.com/deluan/gosonic/utils"
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getCoverArt(params ...string) (*http.Request, *httptest.ResponseRecorder) {
|
||||||
|
url := AddParams("/rest/getCoverArt.view", params...)
|
||||||
|
r, _ := http.NewRequest("GET", url, nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
beego.BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
|
beego.Debug("testing TestGetCoverArtDirectory", fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%#v", r.URL, w.Code, w.HeaderMap))
|
||||||
|
return r, w
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetCoverArt(t *testing.T) {
|
||||||
|
Init(t, false)
|
||||||
|
|
||||||
|
mockMediaFileRepo := mocks.CreateMockMediaFileRepo()
|
||||||
|
utils.DefineSingleton(new(domain.MediaFileRepository), func() domain.MediaFileRepository {
|
||||||
|
return mockMediaFileRepo
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Subject: GetCoverArt Endpoint", t, func() {
|
||||||
|
Convey("Should fail if missing Id parameter", func() {
|
||||||
|
_, w := getCoverArt()
|
||||||
|
|
||||||
|
So(w.Body, ShouldReceiveError, responses.ERROR_MISSING_PARAMETER)
|
||||||
|
})
|
||||||
|
Convey("When id is not found", func() {
|
||||||
|
mockMediaFileRepo.SetData(`[]`, 1)
|
||||||
|
_, w := getCoverArt("id=NOT_FOUND")
|
||||||
|
|
||||||
|
So(w.Body.Bytes(), ShouldMatchMD5, "963552b04e87a5a55e993f98a0fbdf82")
|
||||||
|
})
|
||||||
|
Convey("When id is found", func() {
|
||||||
|
mockMediaFileRepo.SetData(`[{"Id":"2","HasCoverArt":true,"Path":"tests/fixtures/01 Invisible (RED) Edit Version.mp3"}]`, 1)
|
||||||
|
_, w := getCoverArt("id=2")
|
||||||
|
|
||||||
|
So(w.Body.Bytes(), ShouldMatchMD5, "e859a71cd1b1aaeb1ad437d85b306668")
|
||||||
|
})
|
||||||
|
Reset(func() {
|
||||||
|
mockMediaFileRepo.SetData("[]", 0)
|
||||||
|
mockMediaFileRepo.SetError(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
|
@ -21,6 +21,7 @@ func mapEndpoints() {
|
||||||
beego.NSRouter("/getMusicFolders.view", &api.GetMusicFoldersController{}, "*:Get"),
|
beego.NSRouter("/getMusicFolders.view", &api.GetMusicFoldersController{}, "*:Get"),
|
||||||
beego.NSRouter("/getIndexes.view", &api.GetIndexesController{}, "*:Get"),
|
beego.NSRouter("/getIndexes.view", &api.GetIndexesController{}, "*:Get"),
|
||||||
beego.NSRouter("/getMusicDirectory.view", &api.GetMusicDirectoryController{}, "*:Get"),
|
beego.NSRouter("/getMusicDirectory.view", &api.GetMusicDirectoryController{}, "*:Get"),
|
||||||
|
beego.NSRouter("/getCoverArt.view", &api.GetCoverArtController{}, "*:Get"),
|
||||||
)
|
)
|
||||||
beego.AddNamespace(ns)
|
beego.AddNamespace(ns)
|
||||||
|
|
||||||
|
|
|
@ -30,5 +30,6 @@ type MediaFile struct {
|
||||||
type MediaFileRepository interface {
|
type MediaFileRepository interface {
|
||||||
BaseRepository
|
BaseRepository
|
||||||
Put(m *MediaFile) error
|
Put(m *MediaFile) error
|
||||||
|
Get(id string) (*MediaFile, error)
|
||||||
FindByAlbum(albumId string) ([]MediaFile, error)
|
FindByAlbum(albumId string) ([]MediaFile, error)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,11 @@ func (r *mediaFileRepository) Put(m *domain.MediaFile) error {
|
||||||
return r.saveOrUpdate(m.Id, m)
|
return r.saveOrUpdate(m.Id, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *mediaFileRepository) Get(id string) (*domain.MediaFile, error) {
|
||||||
|
m, err := r.readEntity(id)
|
||||||
|
return m.(*domain.MediaFile), err
|
||||||
|
}
|
||||||
|
|
||||||
func (r *mediaFileRepository) FindByAlbum(albumId string) ([]domain.MediaFile, error) {
|
func (r *mediaFileRepository) FindByAlbum(albumId string) ([]domain.MediaFile, error) {
|
||||||
var mfs = make([]domain.MediaFile, 0)
|
var mfs = make([]domain.MediaFile, 0)
|
||||||
err := r.loadChildren("album", albumId, &mfs, "", false)
|
err := r.loadChildren("album", albumId, &mfs, "", false)
|
||||||
|
@ -35,7 +40,7 @@ func (a byTrackNumber) Swap(i, j int) {
|
||||||
a[i], a[j] = a[j], a[i]
|
a[i], a[j] = a[j], a[i]
|
||||||
}
|
}
|
||||||
func (a byTrackNumber) Less(i, j int) bool {
|
func (a byTrackNumber) Less(i, j int) bool {
|
||||||
return (a[i].DiscNumber*1000 + a[i].TrackNumber) < (a[j].DiscNumber*1000 + a[j].TrackNumber)
|
return (a[i].DiscNumber * 1000 + a[i].TrackNumber) < (a[j].DiscNumber * 1000 + a[j].TrackNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ domain.MediaFileRepository = (*mediaFileRepository)(nil)
|
var _ domain.MediaFileRepository = (*mediaFileRepository)(nil)
|
BIN
static/default_cover.jpg
Normal file
BIN
static/default_cover.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
BIN
tests/fixtures/01 Invisible (RED) Edit Version.mp3
vendored
Normal file
BIN
tests/fixtures/01 Invisible (RED) Edit Version.mp3
vendored
Normal file
Binary file not shown.
|
@ -7,6 +7,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/deluan/gosonic/api/responses"
|
"github.com/deluan/gosonic/api/responses"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
"crypto/md5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ShouldMatchXML(actual interface{}, expected ...interface{}) string {
|
func ShouldMatchXML(actual interface{}, expected ...interface{}) string {
|
||||||
|
@ -43,6 +44,11 @@ func ShouldReceiveError(actual interface{}, expected ...interface{}) string {
|
||||||
return ShouldEqual(v.Error.Code, expected[0].(int))
|
return ShouldEqual(v.Error.Code, expected[0].(int))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ShouldMatchMD5(actual interface{}, expected ...interface{}) string {
|
||||||
|
a := fmt.Sprintf("%x", md5.Sum(actual.([]byte)))
|
||||||
|
return ShouldEqual(a, expected[0].(string))
|
||||||
|
}
|
||||||
|
|
||||||
func UnindentJSON(j []byte) string {
|
func UnindentJSON(j []byte) string {
|
||||||
var m = make(map[string]interface{})
|
var m = make(map[string]interface{})
|
||||||
json.Unmarshal(j, &m)
|
json.Unmarshal(j, &m)
|
||||||
|
|
|
@ -45,7 +45,11 @@ func (m *MockMediaFile) Get(id string) (*domain.MediaFile, error) {
|
||||||
if m.err {
|
if m.err {
|
||||||
return nil, errors.New("Error!")
|
return nil, errors.New("Error!")
|
||||||
}
|
}
|
||||||
return m.data[id], nil
|
mf := m.data[id]
|
||||||
|
if mf == nil {
|
||||||
|
mf = &domain.MediaFile{}
|
||||||
|
}
|
||||||
|
return mf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockMediaFile) FindByAlbum(artistId string) ([]domain.MediaFile, error) {
|
func (m *MockMediaFile) FindByAlbum(artistId string) ([]domain.MediaFile, error) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue