quintodrome/engine/browser.go

200 lines
5 KiB
Go
Raw Normal View History

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