navidrome/engine/browser.go

216 lines
5.3 KiB
Go
Raw Normal View History

package engine
import (
2020-01-09 02:45:07 +01:00
"context"
"fmt"
2020-01-15 23:49:09 +01:00
"sort"
"strconv"
2020-01-15 23:49:09 +01:00
"strings"
"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"
)
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)
}
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 {
return &browser{pr, fr, ar, alr, mr, gr}
}
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
}
2020-01-15 04:22:34 +01:00
func (b *browser) MediaFolders() (model.MediaFolders, error) {
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")
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)
}
if lastModified.After(ifModifiedSince) {
indexes, err := b.artistRepo.GetIndex()
return indexes, lastModified, err
}
return nil, lastModified, nil
}
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
}
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) {
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)
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-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{
Id: a.ID,
2016-03-28 03:27:45 +02:00
Name: a.Name,
AlbumCount: a.AlbumCount,
}
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)
}
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{
Id: al.ID,
2016-03-21 15:24:40 +01:00
Name: al.Name,
Parent: al.ArtistID,
2016-03-21 15:24:40 +01:00
PlayCount: int32(al.PlayCount),
UserRating: al.Rating,
Starred: al.StarredAt,
2016-03-28 15:16:03 +02:00
Artist: al.Artist,
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,
CoverArt: al.CoverArtId,
2016-03-21 14:35:18 +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)
}
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)
if err != nil {
2020-01-09 02:45:07 +01:00
log.Debug(ctx, "Error searching for Artist", "id", id, err)
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)
if err != nil {
2020-01-09 02:45:07 +01:00
log.Debug(ctx, "Error searching for Album", "id", id, err)
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)
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 {
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)
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 {
err = fmt.Errorf("Error reading %s's tracks from DB: %v", al.Name, err)
}
return
}