navidrome/api/get_music_directory.go

142 lines
3.9 KiB
Go
Raw Normal View History

package api
import (
2016-03-03 06:46:23 +01:00
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/domain"
"github.com/deluan/gosonic/utils"
"github.com/karlkfi/inject"
"time"
)
type GetMusicDirectoryController struct {
BaseAPIController
artistRepo domain.ArtistRepository
2016-03-03 05:51:26 +01:00
albumRepo domain.AlbumRepository
mFileRepo domain.MediaFileRepository
}
func (c *GetMusicDirectoryController) Prepare() {
inject.ExtractAssignable(utils.Graph, &c.artistRepo)
inject.ExtractAssignable(utils.Graph, &c.albumRepo)
inject.ExtractAssignable(utils.Graph, &c.mFileRepo)
}
func (c *GetMusicDirectoryController) Get() {
id := c.GetParameter("id", "id parameter required")
2016-03-03 15:50:50 +01:00
response := c.NewEmpty()
switch {
case c.isArtist(id):
a, albums := c.retrieveArtist(id)
response.Directory = c.buildArtistDir(a, albums)
case c.isAlbum(id):
al, tracks := c.retrieveAlbum(id)
response.Directory = c.buildAlbumDir(al, tracks)
default:
beego.Info("Id", id, "not found")
c.SendError(responses.ERROR_DATA_NOT_FOUND, "Directory not found")
}
c.SendResponse(response)
}
2016-03-03 02:50:16 +01:00
2016-03-03 19:17:52 +01:00
func (c *GetMusicDirectoryController) buildArtistDir(a *domain.Artist, albums []domain.Album) *responses.Directory {
2016-03-03 15:50:50 +01:00
dir := &responses.Directory{Id: a.Id, Name: a.Name}
dir.Child = make([]responses.Child, len(albums))
for i, al := range albums {
dir.Child[i].Id = al.Id
dir.Child[i].Title = al.Name
dir.Child[i].IsDir = true
dir.Child[i].Parent = al.ArtistId
2016-03-03 15:50:50 +01:00
dir.Child[i].Album = al.Name
dir.Child[i].Year = al.Year
dir.Child[i].Artist = al.AlbumArtist
2016-03-03 15:50:50 +01:00
dir.Child[i].Genre = al.Genre
2016-03-03 16:34:17 +01:00
dir.Child[i].CoverArt = al.CoverArtId
if al.Starred {
t := time.Now()
dir.Child[i].Starred = &t
}
2016-03-03 15:50:50 +01:00
}
return dir
}
2016-03-03 19:17:52 +01:00
func (c *GetMusicDirectoryController) buildAlbumDir(al *domain.Album, tracks []domain.MediaFile) *responses.Directory {
2016-03-03 15:50:50 +01:00
dir := &responses.Directory{Id: al.Id, Name: al.Name}
dir.Child = make([]responses.Child, len(tracks))
for i, mf := range tracks {
dir.Child[i].Id = mf.Id
dir.Child[i].Title = mf.Title
dir.Child[i].IsDir = false
dir.Child[i].Parent = mf.AlbumId
2016-03-03 15:50:50 +01:00
dir.Child[i].Album = mf.Album
dir.Child[i].Year = mf.Year
dir.Child[i].Artist = mf.Artist
dir.Child[i].Genre = mf.Genre
dir.Child[i].Track = mf.TrackNumber
dir.Child[i].Duration = mf.Duration
dir.Child[i].Size = mf.Size
dir.Child[i].Suffix = mf.Suffix
dir.Child[i].BitRate = mf.BitRate
if mf.Starred {
dir.Child[i].Starred = &mf.UpdatedAt
}
2016-03-03 16:34:17 +01:00
if mf.HasCoverArt {
dir.Child[i].CoverArt = mf.Id
}
dir.Child[i].ContentType = mf.ContentType()
2016-03-03 15:50:50 +01:00
}
return dir
}
func (c *GetMusicDirectoryController) isArtist(id string) bool {
2016-03-03 19:17:52 +01:00
found, err := c.artistRepo.Exists(id)
if err != nil {
2016-03-06 01:40:36 +01:00
beego.Error("Error searching for Artist", id, " - ", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
2016-03-03 15:50:50 +01:00
return found
}
func (c *GetMusicDirectoryController) isAlbum(id string) bool {
found, err := c.albumRepo.Exists(id)
if err != nil {
beego.Error("Error searching for Album:", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
2016-03-03 15:50:50 +01:00
return found
}
2016-03-03 02:50:16 +01:00
2016-03-03 15:50:50 +01:00
func (c *GetMusicDirectoryController) retrieveArtist(id string) (a *domain.Artist, as []domain.Album) {
2016-03-03 19:17:52 +01:00
a, err := c.artistRepo.Get(id)
2016-03-03 15:50:50 +01:00
if err != nil {
2016-03-03 02:50:16 +01:00
beego.Error("Error reading Artist from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
if as, err = c.albumRepo.FindByArtist(id); err != nil {
2016-03-03 15:50:50 +01:00
beego.Error("Error reading Artist's Albums from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
2016-03-03 02:50:16 +01:00
return
}
2016-03-03 15:50:50 +01:00
func (c *GetMusicDirectoryController) retrieveAlbum(id string) (al *domain.Album, mfs []domain.MediaFile) {
al, err := c.albumRepo.Get(id)
if err != nil {
beego.Error("Error reading Album from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
if mfs, err = c.mFileRepo.FindByAlbum(id); err != nil {
2016-03-03 15:50:50 +01:00
beego.Error("Error reading Album's Tracks from DB", err)
c.SendError(responses.ERROR_GENERIC, "Internal Error")
}
return
}