Refactored coverart logic to engine layer

This commit is contained in:
Deluan 2016-03-08 23:05:54 -05:00
parent 193f7fe166
commit 00b4c44010
3 changed files with 72 additions and 44 deletions

View file

@ -3,64 +3,32 @@ package api
import ( import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses" "github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/domain" "github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils" "github.com/deluan/gosonic/utils"
"github.com/dhowden/tag"
"github.com/karlkfi/inject" "github.com/karlkfi/inject"
"io/ioutil"
"os"
) )
type GetCoverArtController struct { type GetCoverArtController struct {
BaseAPIController BaseAPIController
repo domain.MediaFileRepository cover engine.Cover
} }
func (c *GetCoverArtController) Prepare() { func (c *GetCoverArtController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.repo) inject.ExtractAssignable(utils.Graph, &c.cover)
} }
// TODO accept size parameter // TODO accept size parameter
func (c *GetCoverArtController) Get() { func (c *GetCoverArtController) Get() {
id := c.RequiredParamString("id", "id parameter required") id := c.RequiredParamString("id", "id parameter required")
mf, err := c.repo.Get(id) err := c.cover.GetCover(id, 0, c.Ctx.ResponseWriter)
if err != nil {
beego.Error("Error reading mediafile", id, "from the database", ":", err) switch {
c.SendError(responses.ERROR_GENERIC, "Internal error") case err == engine.DataNotFound:
beego.Error(err, "Id:", id)
c.SendError(responses.ERROR_DATA_NOT_FOUND, "Directory not found")
case err != nil:
beego.Error(err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
} }
var img []byte
if mf != nil && 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.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
} }

View file

@ -19,4 +19,5 @@ func init() {
utils.DefineSingleton(new(engine.PropertyRepository), persistence.NewPropertyRepository) utils.DefineSingleton(new(engine.PropertyRepository), persistence.NewPropertyRepository)
utils.DefineSingleton(new(engine.Browser), engine.NewBrowser) utils.DefineSingleton(new(engine.Browser), engine.NewBrowser)
utils.DefineSingleton(new(engine.ListGenerator), engine.NewListGenerator) utils.DefineSingleton(new(engine.ListGenerator), engine.NewListGenerator)
utils.DefineSingleton(new(engine.Cover), engine.NewCover)
} }

59
engine/cover.go Normal file
View file

@ -0,0 +1,59 @@
package engine
import (
"io"
"io/ioutil"
"os"
"github.com/deluan/gosonic/domain"
"github.com/dhowden/tag"
)
type Cover interface {
GetCover(id string, size int, out io.Writer) error
}
type cover struct {
mfileRepo domain.MediaFileRepository
}
func NewCover(mr domain.MediaFileRepository) Cover {
return cover{mr}
}
func (c cover) GetCover(id string, size int, out io.Writer) error {
mf, err := c.mfileRepo.Get(id)
if err != nil {
return err
}
var img []byte
if mf != nil && mf.HasCoverArt {
img, err = readFromTag(mf.Path)
} else {
img, err = ioutil.ReadFile("static/default_cover.jpg")
}
if err != nil {
return DataNotFound
}
_, err = out.Write(img)
return err
}
func readFromTag(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
m, err := tag.ReadFrom(f)
if err != nil {
return nil, err
}
return m.Picture().Data, nil
}