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

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
}