2016-03-07 16:57:32 +01:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2020-01-09 02:45:07 +01:00
|
|
|
"context"
|
2016-03-07 16:57:32 +01:00
|
|
|
"fmt"
|
2020-01-15 23:49:09 +01:00
|
|
|
"sort"
|
2016-03-08 14:48:47 +01:00
|
|
|
"strconv"
|
2020-01-15 23:49:09 +01:00
|
|
|
"strings"
|
2016-03-08 14:48:47 +01:00
|
|
|
"time"
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
"github.com/cloudsonic/sonic-server/log"
|
2020-01-15 04:22:34 +01:00
|
|
|
"github.com/cloudsonic/sonic-server/model"
|
2017-04-01 15:47:14 +02:00
|
|
|
"github.com/cloudsonic/sonic-server/utils"
|
2016-03-08 14:48:47 +01:00
|
|
|
)
|
|
|
|
|
2016-03-07 16:57:32 +01:00
|
|
|
type Browser interface {
|
2020-01-15 04:22:34 +01:00
|
|
|
MediaFolders() (model.MediaFolders, error)
|
|
|
|
Indexes(ifModifiedSince time.Time) (model.ArtistIndexes, time.Time, error)
|
2020-01-09 02:45:07 +01:00
|
|
|
Directory(ctx context.Context, id string) (*DirectoryInfo, error)
|
|
|
|
Artist(ctx context.Context, id string) (*DirectoryInfo, error)
|
|
|
|
Album(ctx context.Context, id string) (*DirectoryInfo, error)
|
2016-03-25 05:04:22 +01:00
|
|
|
GetSong(id string) (*Entry, error)
|
2020-01-15 23:49:09 +01:00
|
|
|
GetGenres() (model.Genres, error)
|
2016-03-07 16:57:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 02:46:19 +01:00
|
|
|
func NewBrowser(pr model.PropertyRepository, fr model.MediaFolderRepository,
|
2020-01-15 23:49:09 +01:00
|
|
|
ar model.ArtistRepository, alr model.AlbumRepository, mr model.MediaFileRepository, gr model.GenreRepository) Browser {
|
2020-01-18 02:46:19 +01:00
|
|
|
return &browser{pr, fr, ar, alr, mr, gr}
|
2016-03-07 16:57:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type browser struct {
|
2020-01-15 04:22:34 +01:00
|
|
|
propRepo model.PropertyRepository
|
|
|
|
folderRepo model.MediaFolderRepository
|
|
|
|
artistRepo model.ArtistRepository
|
|
|
|
albumRepo model.AlbumRepository
|
|
|
|
mfileRepo model.MediaFileRepository
|
2020-01-15 23:49:09 +01:00
|
|
|
genreRepo model.GenreRepository
|
2016-03-07 16:57:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 04:22:34 +01:00
|
|
|
func (b *browser) MediaFolders() (model.MediaFolders, error) {
|
2016-03-07 16:57:32 +01:00
|
|
|
return b.folderRepo.GetAll()
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:22:34 +01:00
|
|
|
func (b *browser) Indexes(ifModifiedSince time.Time) (model.ArtistIndexes, time.Time, error) {
|
|
|
|
l, err := b.propRepo.DefaultGet(model.PropLastScan, "-1")
|
2016-03-07 16:57:32 +01:00
|
|
|
ms, _ := strconv.ParseInt(l, 10, 64)
|
|
|
|
lastModified := utils.ToTime(ms)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-03-27 04:43:13 +02:00
|
|
|
return nil, time.Time{}, fmt.Errorf("error retrieving LastScan property: %v", err)
|
2016-03-07 16:57:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if lastModified.After(ifModifiedSince) {
|
2020-01-18 02:46:19 +01:00
|
|
|
indexes, err := b.artistRepo.GetIndex()
|
2016-03-07 16:57:32 +01:00
|
|
|
return indexes, lastModified, err
|
|
|
|
}
|
|
|
|
|
2016-03-20 18:08:24 +01:00
|
|
|
return nil, lastModified, nil
|
2016-03-07 16:57:32 +01:00
|
|
|
}
|
2016-03-08 14:48:47 +01:00
|
|
|
|
|
|
|
type DirectoryInfo struct {
|
2016-03-21 15:24:40 +01:00
|
|
|
Id string
|
|
|
|
Name string
|
|
|
|
Entries Entries
|
|
|
|
Parent string
|
|
|
|
Starred time.Time
|
|
|
|
PlayCount int32
|
|
|
|
UserRating int
|
2016-03-28 03:27:45 +02:00
|
|
|
AlbumCount int
|
|
|
|
CoverArt string
|
2016-03-28 15:16:03 +02:00
|
|
|
Artist string
|
|
|
|
ArtistId string
|
|
|
|
SongCount int
|
|
|
|
Duration int
|
|
|
|
Created time.Time
|
|
|
|
Year int
|
|
|
|
Genre string
|
2016-03-28 03:27:45 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
func (b *browser) Artist(ctx context.Context, id string) (*DirectoryInfo, error) {
|
2016-03-28 03:27:45 +02:00
|
|
|
a, albums, err := b.retrieveArtist(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-09 02:45:07 +01:00
|
|
|
log.Debug(ctx, "Found Artist", "id", id, "name", a.Name)
|
2016-03-28 03:27:45 +02:00
|
|
|
return b.buildArtistDir(a, albums), nil
|
2016-03-08 14:48:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
func (b *browser) Album(ctx context.Context, id string) (*DirectoryInfo, error) {
|
2016-03-28 15:16:03 +02:00
|
|
|
al, tracks, err := b.retrieveAlbum(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-09 02:45:07 +01:00
|
|
|
log.Debug(ctx, "Found Album", "id", id, "name", al.Name)
|
2016-03-28 15:16:03 +02:00
|
|
|
return b.buildAlbumDir(al, tracks), nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
func (b *browser) Directory(ctx context.Context, id string) (*DirectoryInfo, error) {
|
2016-03-08 14:48:47 +01:00
|
|
|
switch {
|
2020-01-09 02:45:07 +01:00
|
|
|
case b.isArtist(ctx, id):
|
|
|
|
return b.Artist(ctx, id)
|
|
|
|
case b.isAlbum(ctx, id):
|
|
|
|
return b.Album(ctx, id)
|
2016-03-08 14:48:47 +01:00
|
|
|
default:
|
2020-01-09 02:45:07 +01:00
|
|
|
log.Debug(ctx, "Directory not found", "id", id)
|
2020-01-15 04:22:34 +01:00
|
|
|
return nil, model.ErrNotFound
|
2016-03-08 14:48:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 05:04:22 +01:00
|
|
|
func (b *browser) GetSong(id string) (*Entry, error) {
|
|
|
|
mf, err := b.mfileRepo.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := FromMediaFile(mf)
|
|
|
|
return &entry, nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 23:49:09 +01:00
|
|
|
func (b *browser) GetGenres() (model.Genres, error) {
|
|
|
|
genres, err := b.genreRepo.GetAll()
|
|
|
|
for i, g := range genres {
|
|
|
|
if strings.TrimSpace(g.Name) == "" {
|
|
|
|
genres[i].Name = "<Empty>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(genres, func(i, j int) bool {
|
|
|
|
return genres[i].Name < genres[j].Name
|
|
|
|
})
|
|
|
|
return genres, err
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:22:34 +01:00
|
|
|
func (b *browser) buildArtistDir(a *model.Artist, albums model.Albums) *DirectoryInfo {
|
2016-03-28 03:27:45 +02:00
|
|
|
dir := &DirectoryInfo{
|
2020-01-10 05:33:01 +01:00
|
|
|
Id: a.ID,
|
2016-03-28 03:27:45 +02:00
|
|
|
Name: a.Name,
|
|
|
|
AlbumCount: a.AlbumCount,
|
|
|
|
}
|
2016-03-08 14:48:47 +01:00
|
|
|
|
2016-03-20 18:14:04 +01:00
|
|
|
dir.Entries = make(Entries, len(albums))
|
|
|
|
for i, al := range albums {
|
2016-03-11 15:10:40 +01:00
|
|
|
dir.Entries[i] = FromAlbum(&al)
|
2016-03-21 14:35:18 +01:00
|
|
|
dir.PlayCount += int32(al.PlayCount)
|
2016-03-08 14:48:47 +01:00
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:22:34 +01:00
|
|
|
func (b *browser) buildAlbumDir(al *model.Album, tracks model.MediaFiles) *DirectoryInfo {
|
2016-03-21 14:35:18 +01:00
|
|
|
dir := &DirectoryInfo{
|
2020-01-10 05:33:01 +01:00
|
|
|
Id: al.ID,
|
2016-03-21 15:24:40 +01:00
|
|
|
Name: al.Name,
|
2020-01-10 05:33:01 +01:00
|
|
|
Parent: al.ArtistID,
|
2016-03-21 15:24:40 +01:00
|
|
|
PlayCount: int32(al.PlayCount),
|
|
|
|
UserRating: al.Rating,
|
2016-03-23 00:00:18 +01:00
|
|
|
Starred: al.StarredAt,
|
2016-03-28 15:16:03 +02:00
|
|
|
Artist: al.Artist,
|
2020-01-10 05:33:01 +01:00
|
|
|
ArtistId: al.ArtistID,
|
2016-03-28 15:16:03 +02:00
|
|
|
SongCount: al.SongCount,
|
|
|
|
Duration: al.Duration,
|
|
|
|
Created: al.CreatedAt,
|
|
|
|
Year: al.Year,
|
|
|
|
Genre: al.Genre,
|
2016-03-30 16:01:37 +02:00
|
|
|
CoverArt: al.CoverArtId,
|
2016-03-21 14:35:18 +01:00
|
|
|
}
|
2016-03-08 14:48:47 +01:00
|
|
|
|
2016-03-20 18:14:04 +01:00
|
|
|
dir.Entries = make(Entries, len(tracks))
|
|
|
|
for i, mf := range tracks {
|
2016-03-11 15:10:40 +01:00
|
|
|
dir.Entries[i] = FromMediaFile(&mf)
|
2016-03-08 14:48:47 +01:00
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
func (b *browser) isArtist(ctx context.Context, id string) bool {
|
2016-03-22 04:11:57 +01:00
|
|
|
found, err := b.artistRepo.Exists(id)
|
2016-03-08 14:48:47 +01:00
|
|
|
if err != nil {
|
2020-01-09 02:45:07 +01:00
|
|
|
log.Debug(ctx, "Error searching for Artist", "id", id, err)
|
2016-03-08 14:48:47 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
func (b *browser) isAlbum(ctx context.Context, id string) bool {
|
2016-03-22 04:11:57 +01:00
|
|
|
found, err := b.albumRepo.Exists(id)
|
2016-03-08 14:48:47 +01:00
|
|
|
if err != nil {
|
2020-01-09 02:45:07 +01:00
|
|
|
log.Debug(ctx, "Error searching for Album", "id", id, err)
|
2016-03-08 14:48:47 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:22:34 +01:00
|
|
|
func (b *browser) retrieveArtist(id string) (a *model.Artist, as model.Albums, err error) {
|
2016-03-22 04:11:57 +01:00
|
|
|
a, err = b.artistRepo.Get(id)
|
2016-03-08 14:48:47 +01:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error reading Artist %s from DB: %v", id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-22 04:11:57 +01:00
|
|
|
if as, err = b.albumRepo.FindByArtist(id); err != nil {
|
2016-03-08 14:48:47 +01:00
|
|
|
err = fmt.Errorf("Error reading %s's albums from DB: %v", a.Name, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-15 04:22:34 +01:00
|
|
|
func (b *browser) retrieveAlbum(id string) (al *model.Album, mfs model.MediaFiles, err error) {
|
2016-03-22 04:11:57 +01:00
|
|
|
al, err = b.albumRepo.Get(id)
|
2016-03-08 14:48:47 +01:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error reading Album %s from DB: %v", id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-22 04:11:57 +01:00
|
|
|
if mfs, err = b.mfileRepo.FindByAlbum(id); err != nil {
|
2016-03-08 14:48:47 +01:00
|
|
|
err = fmt.Errorf("Error reading %s's tracks from DB: %v", al.Name, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|