2016-03-03 02:00:55 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-03-09 16:22:10 +01:00
|
|
|
"fmt"
|
2016-03-12 02:49:01 +01:00
|
|
|
"time"
|
|
|
|
|
2016-03-03 06:46:23 +01:00
|
|
|
"github.com/astaxie/beego"
|
2016-03-03 02:00:55 +01:00
|
|
|
"github.com/deluan/gosonic/api/responses"
|
2016-03-23 23:15:14 +01:00
|
|
|
"github.com/deluan/gosonic/domain"
|
2016-03-08 14:48:47 +01:00
|
|
|
"github.com/deluan/gosonic/engine"
|
2016-03-03 02:00:55 +01:00
|
|
|
"github.com/deluan/gosonic/utils"
|
|
|
|
)
|
|
|
|
|
2016-03-09 16:22:10 +01:00
|
|
|
type BrowsingController struct {
|
2016-03-03 02:00:55 +01:00
|
|
|
BaseAPIController
|
2016-03-08 14:48:47 +01:00
|
|
|
browser engine.Browser
|
2016-03-03 02:00:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 16:22:10 +01:00
|
|
|
func (c *BrowsingController) Prepare() {
|
2016-03-11 21:16:17 +01:00
|
|
|
utils.ResolveDependencies(&c.browser)
|
2016-03-03 02:00:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 19:44:21 +01:00
|
|
|
func (c *BrowsingController) GetMusicFolders() {
|
2016-03-09 16:22:10 +01:00
|
|
|
mediaFolderList, _ := c.browser.MediaFolders()
|
2016-03-20 18:14:04 +01:00
|
|
|
folders := make([]responses.MusicFolder, len(mediaFolderList))
|
|
|
|
for i, f := range mediaFolderList {
|
2016-03-09 16:22:10 +01:00
|
|
|
folders[i].Id = f.Id
|
|
|
|
folders[i].Name = f.Name
|
|
|
|
}
|
|
|
|
response := c.NewEmpty()
|
|
|
|
response.MusicFolders = &responses.MusicFolders{Folders: folders}
|
|
|
|
c.SendResponse(response)
|
|
|
|
}
|
|
|
|
|
2016-03-28 02:35:10 +02:00
|
|
|
func (c *BrowsingController) getArtistIndex(ifModifiedSince time.Time) responses.Indexes {
|
2016-03-09 16:22:10 +01:00
|
|
|
indexes, lastModified, err := c.browser.Indexes(ifModifiedSince)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Error retrieving Indexes:", err)
|
2016-03-23 17:35:10 +01:00
|
|
|
c.SendError(responses.ErrorGeneric, "Internal Error")
|
2016-03-09 16:22:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res := responses.Indexes{
|
|
|
|
IgnoredArticles: beego.AppConfig.String("ignoredArticles"),
|
|
|
|
LastModified: fmt.Sprint(utils.ToMillis(lastModified)),
|
|
|
|
}
|
|
|
|
|
2016-03-20 18:08:24 +01:00
|
|
|
res.Index = make([]responses.Index, len(indexes))
|
|
|
|
for i, idx := range indexes {
|
2016-03-09 16:22:10 +01:00
|
|
|
res.Index[i].Name = idx.Id
|
|
|
|
res.Index[i].Artists = make([]responses.Artist, len(idx.Artists))
|
|
|
|
for j, a := range idx.Artists {
|
|
|
|
res.Index[i].Artists[j].Id = a.ArtistId
|
|
|
|
res.Index[i].Artists[j].Name = a.Artist
|
2016-03-28 02:13:00 +02:00
|
|
|
res.Index[i].Artists[j].AlbumCount = a.AlbumCount
|
2016-03-09 16:22:10 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-28 02:35:10 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BrowsingController) GetIndexes() {
|
|
|
|
ifModifiedSince := c.ParamTime("ifModifiedSince", time.Time{})
|
|
|
|
|
|
|
|
res := c.getArtistIndex(ifModifiedSince)
|
2016-03-09 16:22:10 +01:00
|
|
|
|
|
|
|
response := c.NewEmpty()
|
|
|
|
response.Indexes = &res
|
|
|
|
c.SendResponse(response)
|
|
|
|
}
|
|
|
|
|
2016-03-28 02:35:10 +02:00
|
|
|
func (c *BrowsingController) GetArtists() {
|
|
|
|
res := c.getArtistIndex(time.Time{})
|
|
|
|
|
|
|
|
response := c.NewEmpty()
|
|
|
|
response.Artist = &res
|
|
|
|
c.SendResponse(response)
|
|
|
|
}
|
|
|
|
|
2016-03-24 19:44:21 +01:00
|
|
|
func (c *BrowsingController) GetMusicDirectory() {
|
2016-03-07 20:13:01 +01:00
|
|
|
id := c.RequiredParamString("id", "id parameter required")
|
2016-03-03 02:00:55 +01:00
|
|
|
|
2016-03-08 14:48:47 +01:00
|
|
|
dir, err := c.browser.Directory(id)
|
2016-03-03 15:50:50 +01:00
|
|
|
switch {
|
2016-03-23 23:15:14 +01:00
|
|
|
case err == domain.ErrNotFound:
|
2016-03-10 00:46:53 +01:00
|
|
|
beego.Error("Requested Id", id, "not found:", err)
|
2016-03-23 17:35:10 +01:00
|
|
|
c.SendError(responses.ErrorDataNotFound, "Directory not found")
|
2016-03-08 14:48:47 +01:00
|
|
|
case err != nil:
|
|
|
|
beego.Error(err)
|
2016-03-23 17:35:10 +01:00
|
|
|
c.SendError(responses.ErrorGeneric, "Internal Error")
|
2016-03-03 02:00:55 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 05:04:22 +01:00
|
|
|
response := c.NewEmpty()
|
2016-03-08 14:48:47 +01:00
|
|
|
response.Directory = c.buildDirectory(dir)
|
2016-03-25 05:04:22 +01:00
|
|
|
c.SendResponse(response)
|
|
|
|
}
|
2016-03-08 14:48:47 +01:00
|
|
|
|
2016-03-28 03:27:45 +02:00
|
|
|
func (c *BrowsingController) GetArtist() {
|
|
|
|
id := c.RequiredParamString("id", "id parameter required")
|
|
|
|
|
|
|
|
dir, err := c.browser.Artist(id)
|
|
|
|
switch {
|
|
|
|
case err == domain.ErrNotFound:
|
|
|
|
beego.Error("Requested ArtistId", id, "not found:", err)
|
|
|
|
c.SendError(responses.ErrorDataNotFound, "Artist not found")
|
|
|
|
case err != nil:
|
|
|
|
beego.Error(err)
|
|
|
|
c.SendError(responses.ErrorGeneric, "Internal Error")
|
|
|
|
}
|
|
|
|
|
|
|
|
response := c.NewEmpty()
|
|
|
|
response.ArtistWithAlbumsID3 = c.buildArtist(dir)
|
|
|
|
c.SendResponse(response)
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:16:03 +02:00
|
|
|
func (c *BrowsingController) GetAlbum() {
|
|
|
|
id := c.RequiredParamString("id", "id parameter required")
|
|
|
|
|
|
|
|
dir, err := c.browser.Album(id)
|
|
|
|
switch {
|
|
|
|
case err == domain.ErrNotFound:
|
|
|
|
beego.Error("Requested AlbumId", id, "not found:", err)
|
|
|
|
c.SendError(responses.ErrorDataNotFound, "Album not found")
|
|
|
|
case err != nil:
|
|
|
|
beego.Error(err)
|
|
|
|
c.SendError(responses.ErrorGeneric, "Internal Error")
|
|
|
|
}
|
|
|
|
|
|
|
|
response := c.NewEmpty()
|
|
|
|
response.AlbumWithSongsID3 = c.buildAlbum(dir)
|
|
|
|
c.SendResponse(response)
|
|
|
|
}
|
|
|
|
|
2016-03-25 05:04:22 +01:00
|
|
|
func (c *BrowsingController) GetSong() {
|
|
|
|
id := c.RequiredParamString("id", "id parameter required")
|
|
|
|
|
|
|
|
song, err := c.browser.GetSong(id)
|
|
|
|
switch {
|
|
|
|
case err == domain.ErrNotFound:
|
|
|
|
beego.Error("Requested Id", id, "not found:", err)
|
|
|
|
c.SendError(responses.ErrorDataNotFound, "Directory not found")
|
|
|
|
case err != nil:
|
|
|
|
beego.Error(err)
|
|
|
|
c.SendError(responses.ErrorGeneric, "Internal Error")
|
|
|
|
}
|
|
|
|
|
|
|
|
response := c.NewEmpty()
|
|
|
|
child := c.ToChild(*song)
|
|
|
|
response.Song = &child
|
2016-03-03 02:00:55 +01:00
|
|
|
c.SendResponse(response)
|
|
|
|
}
|
2016-03-03 02:50:16 +01:00
|
|
|
|
2016-03-09 16:22:10 +01:00
|
|
|
func (c *BrowsingController) buildDirectory(d *engine.DirectoryInfo) *responses.Directory {
|
2016-03-21 14:35:18 +01:00
|
|
|
dir := &responses.Directory{
|
2016-03-21 15:24:40 +01:00
|
|
|
Id: d.Id,
|
|
|
|
Name: d.Name,
|
|
|
|
Parent: d.Parent,
|
|
|
|
PlayCount: d.PlayCount,
|
2016-03-28 03:27:45 +02:00
|
|
|
AlbumCount: d.AlbumCount,
|
2016-03-21 15:24:40 +01:00
|
|
|
UserRating: d.UserRating,
|
2016-03-21 14:35:18 +01:00
|
|
|
}
|
|
|
|
if !d.Starred.IsZero() {
|
|
|
|
dir.Starred = &d.Starred
|
|
|
|
}
|
2016-03-08 14:48:47 +01:00
|
|
|
|
2016-03-21 16:57:04 +01:00
|
|
|
dir.Child = c.ToChildren(d.Entries)
|
2016-03-03 15:50:50 +01:00
|
|
|
return dir
|
|
|
|
}
|
2016-03-28 03:27:45 +02:00
|
|
|
|
|
|
|
func (c *BrowsingController) buildArtist(d *engine.DirectoryInfo) *responses.ArtistWithAlbumsID3 {
|
2016-03-28 15:16:03 +02:00
|
|
|
dir := &responses.ArtistWithAlbumsID3{}
|
|
|
|
dir.Id = d.Id
|
|
|
|
dir.Name = d.Name
|
|
|
|
dir.AlbumCount = d.AlbumCount
|
|
|
|
dir.CoverArt = d.CoverArt
|
2016-03-28 03:27:45 +02:00
|
|
|
if !d.Starred.IsZero() {
|
|
|
|
dir.Starred = &d.Starred
|
|
|
|
}
|
|
|
|
|
|
|
|
dir.Album = c.ToAlbums(d.Entries)
|
|
|
|
return dir
|
|
|
|
}
|
2016-03-28 15:16:03 +02:00
|
|
|
|
|
|
|
func (c *BrowsingController) buildAlbum(d *engine.DirectoryInfo) *responses.AlbumWithSongsID3 {
|
|
|
|
dir := &responses.AlbumWithSongsID3{}
|
|
|
|
dir.Id = d.Id
|
|
|
|
dir.Name = d.Name
|
|
|
|
dir.Artist = d.Artist
|
|
|
|
dir.ArtistId = d.ArtistId
|
|
|
|
dir.CoverArt = d.CoverArt
|
|
|
|
dir.SongCount = d.SongCount
|
|
|
|
dir.Duration = d.Duration
|
|
|
|
dir.PlayCount = d.PlayCount
|
|
|
|
dir.Year = d.Year
|
|
|
|
dir.Genre = d.Genre
|
|
|
|
if !d.Created.IsZero() {
|
|
|
|
dir.Created = &d.Created
|
|
|
|
}
|
|
|
|
if !d.Starred.IsZero() {
|
|
|
|
dir.Starred = &d.Starred
|
|
|
|
}
|
|
|
|
|
|
|
|
dir.Song = c.ToChildren(d.Entries)
|
|
|
|
return dir
|
|
|
|
}
|