getCoverArt.view working

This commit is contained in:
Deluan 2016-03-03 12:08:44 -05:00
parent 1b945831cc
commit c9455e1955
9 changed files with 148 additions and 2 deletions

71
api/get_cover_art.go Normal file
View 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
View 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)
})
})
}

View file

@ -21,6 +21,7 @@ func mapEndpoints() {
beego.NSRouter("/getMusicFolders.view", &api.GetMusicFoldersController{}, "*:Get"),
beego.NSRouter("/getIndexes.view", &api.GetIndexesController{}, "*:Get"),
beego.NSRouter("/getMusicDirectory.view", &api.GetMusicDirectoryController{}, "*:Get"),
beego.NSRouter("/getCoverArt.view", &api.GetCoverArtController{}, "*:Get"),
)
beego.AddNamespace(ns)

View file

@ -30,5 +30,6 @@ type MediaFile struct {
type MediaFileRepository interface {
BaseRepository
Put(m *MediaFile) error
Get(id string) (*MediaFile, error)
FindByAlbum(albumId string) ([]MediaFile, error)
}

View file

@ -19,6 +19,11 @@ func (r *mediaFileRepository) Put(m *domain.MediaFile) error {
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) {
var mfs = make([]domain.MediaFile, 0)
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]
}
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)

BIN
static/default_cover.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

View file

@ -7,6 +7,7 @@ import (
"fmt"
"github.com/deluan/gosonic/api/responses"
. "github.com/smartystreets/goconvey/convey"
"crypto/md5"
)
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))
}
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 {
var m = make(map[string]interface{})
json.Unmarshal(j, &m)

View file

@ -45,7 +45,11 @@ func (m *MockMediaFile) Get(id string) (*domain.MediaFile, error) {
if m.err {
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) {