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
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
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)
})
})
}