mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-03 17:07:49 +01:00
677 lines
18 KiB
Go
677 lines
18 KiB
Go
package subsonic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"slices"
|
|
"strconv"
|
|
"time"
|
|
"unicode"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/core/publicurl"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/server/filter"
|
|
"github.com/navidrome/navidrome/server/subsonic/responses"
|
|
"github.com/navidrome/navidrome/utils/req"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
"github.com/navidrome/navidrome/utils/str"
|
|
)
|
|
|
|
func (api *Router) GetMusicFolders(r *http.Request) (*responses.Subsonic, error) {
|
|
libraries := getUserAccessibleLibraries(r.Context())
|
|
|
|
folders := make([]responses.MusicFolder, len(libraries))
|
|
for i, f := range libraries {
|
|
folders[i].Id = int32(f.ID)
|
|
folders[i].Name = f.Name
|
|
}
|
|
response := newResponse()
|
|
response.MusicFolders = &responses.MusicFolders{Folders: folders}
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) getArtist(r *http.Request, libIds []int, ifModifiedSince time.Time) (model.ArtistIndexes, int64, error) {
|
|
ctx := r.Context()
|
|
|
|
lastScanStr, err := api.ds.Property().DefaultGet(ctx, consts.LastScanStartTimeKey, "")
|
|
if err != nil {
|
|
log.Error(ctx, "Error retrieving last scan start time", err)
|
|
return nil, 0, err
|
|
}
|
|
lastScan := time.Now()
|
|
if lastScanStr != "" {
|
|
lastScan, err = time.Parse(time.RFC3339, lastScanStr)
|
|
}
|
|
|
|
var indexes model.ArtistIndexes
|
|
if lastScan.After(ifModifiedSince) {
|
|
indexes, err = api.ds.Artist().GetIndex(ctx, false, libIds, model.RoleAlbumArtist)
|
|
if err != nil {
|
|
log.Error(ctx, "Error retrieving Indexes", err)
|
|
return nil, 0, err
|
|
}
|
|
if len(indexes) == 0 {
|
|
log.Debug(ctx, "No artists found in library", "libId", libIds)
|
|
return nil, 0, newError(responses.ErrorDataNotFound, "Library not found or empty")
|
|
}
|
|
}
|
|
|
|
return indexes, lastScan.UnixMilli(), err
|
|
}
|
|
|
|
func (api *Router) getArtistIndexID3(r *http.Request, libIds []int, ifModifiedSince time.Time) (*responses.Artists, error) {
|
|
indexes, modified, err := api.getArtist(r, libIds, ifModifiedSince)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res := &responses.Artists{
|
|
IgnoredArticles: conf.Server.IgnoredArticles,
|
|
LastModified: modified,
|
|
}
|
|
|
|
res.Index = make([]responses.IndexID3, len(indexes))
|
|
for i, idx := range indexes {
|
|
res.Index[i].Name = idx.ID
|
|
res.Index[i].Artists = slice.MapWithArg(idx.Artists, r, toArtistID3)
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func folderIndexKey(name string) string {
|
|
s := str.SanitizeFieldForSortingNoArticle(name)
|
|
if len(s) == 0 {
|
|
return "#"
|
|
}
|
|
r := []rune(s)[0]
|
|
if r >= 'a' && r <= 'z' {
|
|
return string(unicode.ToUpper(r))
|
|
}
|
|
if r >= 'A' && r <= 'Z' {
|
|
return string(r)
|
|
}
|
|
return "#"
|
|
}
|
|
|
|
func (api *Router) GetIndexes(r *http.Request) (*responses.Subsonic, error) {
|
|
ctx := r.Context()
|
|
p := req.Params(r)
|
|
musicFolderIds, err := selectedMusicFolderIds(r, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ifModifiedSince := p.TimeOr("ifModifiedSince", time.Time{})
|
|
|
|
lastScanStr, err := api.ds.Property().DefaultGet(ctx, consts.LastScanStartTimeKey, "")
|
|
if err != nil {
|
|
log.Error(ctx, "Error retrieving last scan start time", err)
|
|
return nil, err
|
|
}
|
|
lastScan := time.Now()
|
|
if lastScanStr != "" {
|
|
if t, err := time.Parse(time.RFC3339, lastScanStr); err == nil {
|
|
lastScan = t
|
|
}
|
|
}
|
|
|
|
if !lastScan.After(ifModifiedSince) {
|
|
return newResponse(), nil
|
|
}
|
|
|
|
res := &responses.Indexes{
|
|
IgnoredArticles: conf.Server.IgnoredArticles,
|
|
LastModified: lastScan.UnixMilli(),
|
|
}
|
|
|
|
folders, err := api.ds.Folder().GetRootSubfoldersWithAudio(ctx, musicFolderIds...)
|
|
if err != nil {
|
|
log.Error(ctx, "Error retrieving root subfolders", err)
|
|
return nil, err
|
|
}
|
|
|
|
var folderIDs []string
|
|
for _, f := range folders {
|
|
folderIDs = append(folderIDs, f.ID)
|
|
}
|
|
coverArtMap, _ := api.ds.Folder().GetCoverArtForFolders(ctx, folderIDs...)
|
|
|
|
indexMap := make(map[string][]responses.Artist)
|
|
for _, f := range folders {
|
|
key := folderIndexKey(f.Name)
|
|
artist := responses.Artist{
|
|
Id: f.ID,
|
|
Name: f.Name,
|
|
}
|
|
if art, ok := coverArtMap[f.ID]; ok && art != "" {
|
|
artist.CoverArt = art
|
|
}
|
|
indexMap[key] = append(indexMap[key], artist)
|
|
}
|
|
keys := make([]string, 0, len(indexMap))
|
|
for k := range indexMap {
|
|
keys = append(keys, k)
|
|
}
|
|
slices.Sort(keys)
|
|
for _, k := range keys {
|
|
res.Index = append(res.Index, responses.Index{
|
|
Name: k,
|
|
Artists: indexMap[k],
|
|
})
|
|
}
|
|
|
|
accessibleLibs := getUserAccessibleLibraries(ctx)
|
|
libMap := make(map[int]model.Library, len(accessibleLibs))
|
|
for _, lib := range accessibleLibs {
|
|
libMap[lib.ID] = lib
|
|
}
|
|
|
|
for _, libID := range musicFolderIds {
|
|
lib, ok := libMap[libID]
|
|
if !ok {
|
|
continue
|
|
}
|
|
rootFolder, err := api.ds.Folder().GetByPath(ctx, lib, ".")
|
|
if err != nil {
|
|
if !errors.Is(err, model.ErrNotFound) {
|
|
log.Error(ctx, "Error retrieving root folder", "libraryId", lib.ID, err)
|
|
}
|
|
continue
|
|
}
|
|
if rootFolder == nil || rootFolder.Missing {
|
|
continue
|
|
}
|
|
mfs, err := api.ds.MediaFile().GetAll(ctx, model.QueryOptions{
|
|
Filters: squirrel.And{
|
|
squirrel.Eq{"media_file.folder_id": rootFolder.ID},
|
|
squirrel.Eq{"media_file.missing": false},
|
|
},
|
|
Sort: "disc_number, track_number, title",
|
|
})
|
|
if err != nil {
|
|
log.Error(ctx, "Error retrieving loose tracks", "folderId", rootFolder.ID, err)
|
|
return nil, err
|
|
}
|
|
for _, mf := range mfs {
|
|
res.Child = append(res.Child, childFromMediaFile(ctx, mf))
|
|
}
|
|
}
|
|
|
|
response := newResponse()
|
|
response.Indexes = res
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetArtists(r *http.Request) (*responses.Subsonic, error) {
|
|
musicFolderIds, _ := selectedMusicFolderIds(r, false)
|
|
|
|
res, err := api.getArtistIndexID3(r, musicFolderIds, time.Time{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.Artist = res
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetMusicDirectory(r *http.Request) (*responses.Subsonic, error) {
|
|
p := req.Params(r)
|
|
id, _ := p.String("id")
|
|
ctx := r.Context()
|
|
|
|
// 1. Check if id is a model.Folder
|
|
folder, err := api.ds.Folder().Get(ctx, id)
|
|
if err == nil && folder != nil {
|
|
if folder.Missing {
|
|
return nil, newError(responses.ErrorDataNotFound, "Directory not found")
|
|
}
|
|
dir, err := api.buildFolderDirectory(ctx, folder)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return nil, err
|
|
}
|
|
if dir.Name == "." {
|
|
for _, lib := range getUserAccessibleLibraries(ctx) {
|
|
if lib.ID == folder.LibraryID {
|
|
dir.Name = lib.Name
|
|
break
|
|
}
|
|
}
|
|
if dir.Name == "." {
|
|
if lib, err := api.ds.Library().Get(ctx, folder.LibraryID); err == nil && lib != nil {
|
|
dir.Name = lib.Name
|
|
}
|
|
}
|
|
}
|
|
response := newResponse()
|
|
response.Directory = dir
|
|
return response, nil
|
|
}
|
|
|
|
// 2. Check if id is an accessible Library.ID
|
|
if libID, err := strconv.Atoi(id); err == nil {
|
|
accessibleLibs := getUserAccessibleLibraries(ctx)
|
|
for _, lib := range accessibleLibs {
|
|
if lib.ID == libID {
|
|
rootFolder, err := api.ds.Folder().GetByPath(ctx, lib, ".")
|
|
if err != nil || rootFolder == nil || rootFolder.Missing {
|
|
return nil, newError(responses.ErrorDataNotFound, "Directory not found")
|
|
}
|
|
dir, err := api.buildFolderDirectory(ctx, rootFolder)
|
|
if err != nil {
|
|
log.Error(err)
|
|
return nil, err
|
|
}
|
|
dir.Name = lib.Name
|
|
response := newResponse()
|
|
response.Directory = dir
|
|
return response, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Fallback to existing model.GetEntityByID (*model.Artist and *model.Album)
|
|
entity, err := model.GetEntityByID(ctx, api.ds, id)
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
log.Error(r, "Requested ID not found ", "id", id)
|
|
return nil, newError(responses.ErrorDataNotFound, "Directory not found")
|
|
}
|
|
if err != nil {
|
|
log.Error(err)
|
|
return nil, err
|
|
}
|
|
|
|
var dir *responses.Directory
|
|
|
|
switch v := entity.(type) {
|
|
case *model.Artist:
|
|
dir, err = api.buildArtistDirectory(ctx, v)
|
|
case *model.Album:
|
|
dir, err = api.buildAlbumDirectory(ctx, v)
|
|
default:
|
|
log.Error(r, "Requested ID of invalid type", "id", id, "entity", v)
|
|
return nil, newError(responses.ErrorDataNotFound, "Directory not found")
|
|
}
|
|
|
|
if err != nil {
|
|
log.Error(err)
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.Directory = dir
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetArtist(r *http.Request) (*responses.Subsonic, error) {
|
|
p := req.Params(r)
|
|
id, _ := p.String("id")
|
|
ctx := r.Context()
|
|
|
|
artist, err := api.ds.Artist().Get(ctx, id)
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
log.Error(ctx, "Requested ArtistID not found ", "id", id)
|
|
return nil, newError(responses.ErrorDataNotFound, "Artist not found")
|
|
}
|
|
if err != nil {
|
|
log.Error(ctx, "Error retrieving artist", "id", id, err)
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.ArtistWithAlbumsID3, err = api.buildArtist(r, artist)
|
|
if err != nil {
|
|
log.Error(ctx, "Error retrieving albums by artist", "id", artist.ID, "name", artist.Name, err)
|
|
}
|
|
return response, err
|
|
}
|
|
|
|
func (api *Router) GetAlbum(r *http.Request) (*responses.Subsonic, error) {
|
|
p := req.Params(r)
|
|
id, _ := p.String("id")
|
|
|
|
ctx := r.Context()
|
|
|
|
album, err := api.ds.Album().Get(ctx, id)
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
log.Error(ctx, "Requested AlbumID not found ", "id", id)
|
|
return nil, newError(responses.ErrorDataNotFound, "Album not found")
|
|
}
|
|
if err != nil {
|
|
log.Error(ctx, "Error retrieving album", "id", id, err)
|
|
return nil, err
|
|
}
|
|
|
|
mfs, err := api.ds.MediaFile().GetAll(ctx, filter.SongsByAlbum(id))
|
|
if err != nil {
|
|
log.Error(ctx, "Error retrieving tracks from album", "id", id, "name", album.Name, err)
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.AlbumWithSongsID3 = api.buildAlbum(ctx, album, mfs)
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetAlbumInfo(r *http.Request) (*responses.Subsonic, error) {
|
|
p := req.Params(r)
|
|
id, err := p.String("id")
|
|
ctx := r.Context()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
album, err := api.provider.UpdateAlbumInfo(ctx, id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.AlbumInfo = &responses.AlbumInfo{}
|
|
response.AlbumInfo.Notes = album.Description
|
|
if !album.ImageAbsent {
|
|
response.AlbumInfo.SmallImageUrl = publicurl.ImageURL(r.Context(), album.CoverArtID(), 300)
|
|
response.AlbumInfo.MediumImageUrl = publicurl.ImageURL(r.Context(), album.CoverArtID(), 600)
|
|
response.AlbumInfo.LargeImageUrl = publicurl.ImageURL(r.Context(), album.CoverArtID(), 1200)
|
|
}
|
|
|
|
response.AlbumInfo.LastFmUrl = album.ExternalUrl
|
|
response.AlbumInfo.MusicBrainzID = album.MbzAlbumID
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetSong(r *http.Request) (*responses.Subsonic, error) {
|
|
p := req.Params(r)
|
|
id, _ := p.String("id")
|
|
ctx := r.Context()
|
|
|
|
mf, err := api.ds.MediaFile().Get(ctx, id)
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
log.Error(r, "Requested MediaFileID not found ", "id", id)
|
|
return nil, newError(responses.ErrorDataNotFound, "Song not found")
|
|
}
|
|
if err != nil {
|
|
log.Error(r, "Error retrieving MediaFile", "id", id, err)
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.Song = new(childFromMediaFile(ctx, *mf))
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetGenres(r *http.Request) (*responses.Subsonic, error) {
|
|
ctx := r.Context()
|
|
genres, err := api.ds.Genre().GetAll(ctx, model.QueryOptions{Sort: "song_count, album_count, name desc", Order: "desc"})
|
|
if err != nil {
|
|
log.Error(r, err)
|
|
return nil, err
|
|
}
|
|
for i, g := range genres {
|
|
if g.Name == "" {
|
|
genres[i].Name = "<Empty>"
|
|
}
|
|
}
|
|
|
|
response := newResponse()
|
|
response.Genres = toGenres(genres)
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) getArtistInfo(r *http.Request) (*responses.ArtistInfoBase, *model.Artists, error) {
|
|
ctx := r.Context()
|
|
p := req.Params(r)
|
|
id, err := p.String("id")
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
count := p.IntOr("count", 20)
|
|
includeNotPresent := p.BoolOr("includeNotPresent", false)
|
|
|
|
artist, err := api.provider.UpdateArtistInfo(ctx, id, count, includeNotPresent)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
base := responses.ArtistInfoBase{}
|
|
base.Biography = artist.Biography
|
|
if !artist.ImageAbsent {
|
|
base.SmallImageUrl = publicurl.ImageURL(r.Context(), artist.CoverArtID(), 300)
|
|
base.MediumImageUrl = publicurl.ImageURL(r.Context(), artist.CoverArtID(), 600)
|
|
base.LargeImageUrl = publicurl.ImageURL(r.Context(), artist.CoverArtID(), 1200)
|
|
}
|
|
base.LastFmUrl = artist.ExternalUrl
|
|
base.MusicBrainzID = artist.MbzArtistID
|
|
|
|
return &base, &artist.SimilarArtists, nil
|
|
}
|
|
|
|
func (api *Router) GetArtistInfo(r *http.Request) (*responses.Subsonic, error) {
|
|
base, similarArtists, err := api.getArtistInfo(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.ArtistInfo = &responses.ArtistInfo{}
|
|
response.ArtistInfo.ArtistInfoBase = *base
|
|
|
|
for _, s := range *similarArtists {
|
|
similar := toArtist(r, s)
|
|
if s.ID == "" {
|
|
similar.Id = "-1"
|
|
}
|
|
response.ArtistInfo.SimilarArtist = append(response.ArtistInfo.SimilarArtist, similar)
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetArtistInfo2(r *http.Request) (*responses.Subsonic, error) {
|
|
base, similarArtists, err := api.getArtistInfo(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.ArtistInfo2 = &responses.ArtistInfo2{}
|
|
response.ArtistInfo2.ArtistInfoBase = *base
|
|
|
|
for _, s := range *similarArtists {
|
|
similar := toArtistID3(r, s)
|
|
if s.ID == "" {
|
|
similar.Id = "-1"
|
|
}
|
|
response.ArtistInfo2.SimilarArtist = append(response.ArtistInfo2.SimilarArtist, similar)
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetSimilarSongs(r *http.Request) (*responses.Subsonic, error) {
|
|
ctx := r.Context()
|
|
p := req.Params(r)
|
|
id, err := p.String("id")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
count := p.IntOr("count", 50)
|
|
|
|
songs, err := api.provider.SimilarSongs(ctx, id, count)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.SimilarSongs = &responses.SimilarSongs{
|
|
Song: slice.MapWithArg(songs, ctx, childFromMediaFile),
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetSimilarSongs2(r *http.Request) (*responses.Subsonic, error) {
|
|
res, err := api.GetSimilarSongs(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.SimilarSongs2 = &responses.SimilarSongs2{
|
|
Song: res.SimilarSongs.Song,
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) GetTopSongs(r *http.Request) (*responses.Subsonic, error) {
|
|
ctx := r.Context()
|
|
p := req.Params(r)
|
|
id, idErr := p.String("id")
|
|
artist, err := p.String("artist")
|
|
if err != nil && idErr != nil {
|
|
return nil, err
|
|
}
|
|
count := p.IntOr("count", 50)
|
|
|
|
songs, err := api.provider.TopSongs(ctx, artist, id, count)
|
|
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
|
return nil, err
|
|
}
|
|
|
|
response := newResponse()
|
|
response.TopSongs = &responses.TopSongs{
|
|
Song: slice.MapWithArg(songs, ctx, childFromMediaFile),
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func (api *Router) buildArtistDirectory(ctx context.Context, artist *model.Artist) (*responses.Directory, error) {
|
|
dir := &responses.Directory{}
|
|
dir.Id = artist.ID
|
|
dir.Name = artist.Name
|
|
dir.PlayCount = artist.PlayCount
|
|
if artist.PlayCount > 0 {
|
|
dir.Played = artist.PlayDate
|
|
}
|
|
dir.AlbumCount = getArtistAlbumCount(artist)
|
|
dir.UserRating = int32(artist.Rating)
|
|
if conf.Server.Subsonic.EnableAverageRating {
|
|
dir.AverageRating = artist.AverageRating
|
|
}
|
|
if artist.Starred {
|
|
dir.Starred = artist.StarredAt
|
|
}
|
|
|
|
albums, err := api.ds.Album().GetAll(ctx, filter.AlbumsByArtistID(artist.ID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dir.Child = slice.MapWithArg(albums, ctx, childFromAlbum)
|
|
return dir, nil
|
|
}
|
|
|
|
func (api *Router) buildArtist(r *http.Request, artist *model.Artist) (*responses.ArtistWithAlbumsID3, error) {
|
|
ctx := r.Context()
|
|
a := &responses.ArtistWithAlbumsID3{}
|
|
a.ArtistID3 = toArtistID3(r, *artist)
|
|
|
|
albums, err := api.ds.Album().GetAll(ctx, filter.AlbumsByArtistID(artist.ID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
a.Album = slice.MapWithArg(albums, ctx, buildAlbumID3)
|
|
return a, nil
|
|
}
|
|
|
|
func (api *Router) buildAlbumDirectory(ctx context.Context, album *model.Album) (*responses.Directory, error) {
|
|
dir := &responses.Directory{}
|
|
dir.Id = album.ID
|
|
dir.Name = album.FullName()
|
|
dir.Parent = album.AlbumArtistID
|
|
dir.PlayCount = album.PlayCount
|
|
if album.PlayCount > 0 {
|
|
dir.Played = album.PlayDate
|
|
}
|
|
dir.UserRating = int32(album.Rating)
|
|
if conf.Server.Subsonic.EnableAverageRating {
|
|
dir.AverageRating = album.AverageRating
|
|
}
|
|
dir.SongCount = int32(album.SongCount)
|
|
dir.CoverArt = coverArtOrEmpty(album.CoverArtID(), album.ImageAbsent)
|
|
if album.Starred {
|
|
dir.Starred = album.StarredAt
|
|
}
|
|
|
|
mfs, err := api.ds.MediaFile().GetAll(ctx, filter.SongsByAlbum(album.ID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dir.Child = slice.MapWithArg(mfs, ctx, childFromMediaFile)
|
|
return dir, nil
|
|
}
|
|
|
|
func (api *Router) buildAlbum(ctx context.Context, album *model.Album, mfs model.MediaFiles) *responses.AlbumWithSongsID3 {
|
|
dir := &responses.AlbumWithSongsID3{}
|
|
dir.AlbumID3 = buildAlbumID3(ctx, *album)
|
|
dir.Song = slice.MapWithArg(mfs, ctx, childFromMediaFile)
|
|
return dir
|
|
}
|
|
|
|
func (api *Router) buildFolderDirectory(ctx context.Context, folder *model.Folder) (*responses.Directory, error) {
|
|
dir := &responses.Directory{}
|
|
dir.Id = folder.ID
|
|
dir.Name = folder.Name
|
|
if folder.ParentID != "" {
|
|
dir.Parent = folder.ParentID
|
|
}
|
|
|
|
subfolders, err := api.ds.Folder().GetSubfoldersWithAudio(ctx, folder.ID, folder.LibraryID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
songs, err := api.ds.MediaFile().GetAll(ctx, model.QueryOptions{
|
|
Filters: squirrel.And{
|
|
squirrel.Eq{"media_file.folder_id": folder.ID},
|
|
squirrel.Eq{"media_file.missing": false},
|
|
},
|
|
Sort: "disc_number, track_number, title",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(songs) > 0 {
|
|
if album, err := api.ds.Album().Get(ctx, songs[0].AlbumID); err == nil && album != nil {
|
|
dir.CoverArt = coverArtOrEmpty(album.CoverArtID(), album.ImageAbsent)
|
|
}
|
|
} else {
|
|
folderCovers, _ := api.ds.Folder().GetCoverArtForFolders(ctx, folder.ID)
|
|
if art, ok := folderCovers[folder.ID]; ok {
|
|
dir.CoverArt = art
|
|
}
|
|
}
|
|
|
|
var subfolderIDs []string
|
|
for _, sf := range subfolders {
|
|
subfolderIDs = append(subfolderIDs, sf.ID)
|
|
}
|
|
coverArtMap, _ := api.ds.Folder().GetCoverArtForFolders(ctx, subfolderIDs...)
|
|
|
|
for _, sf := range subfolders {
|
|
dir.Child = append(dir.Child, childFromFolder(ctx, sf, coverArtMap[sf.ID]))
|
|
}
|
|
for _, song := range songs {
|
|
dir.Child = append(dir.Child, childFromMediaFile(ctx, song))
|
|
}
|
|
|
|
return dir, nil
|
|
}
|