Refactor getAlbum

This commit is contained in:
Deluan
2020-08-13 18:50:28 -04:00
committed by Deluan Quintão
parent e344f616b3
commit 15c8f4c0ef
2 changed files with 40 additions and 21 deletions
+28 -20
View File
@@ -147,18 +147,26 @@ func (c *BrowsingController) GetArtist(w http.ResponseWriter, r *http.Request) (
func (c *BrowsingController) GetAlbum(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) { func (c *BrowsingController) GetAlbum(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id := utils.ParamString(r, "id") id := utils.ParamString(r, "id")
dir, err := c.browser.Album(r.Context(), id) ctx := r.Context()
album, err := c.ds.Album(ctx).Get(id)
switch { switch {
case err == model.ErrNotFound: case err == model.ErrNotFound:
log.Error(r, "Requested ID not found ", "id", id) log.Error(ctx, "Requested AlbumID not found ", "id", id)
return nil, NewError(responses.ErrorDataNotFound, "Album not found") return nil, NewError(responses.ErrorDataNotFound, "Album not found")
case err != nil: case err != nil:
log.Error(r, err) log.Error(ctx, "Error retrieving album", "id", id, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error")
}
mfs, err := c.ds.MediaFile(ctx).FindByAlbum(id)
if err != nil {
log.Error(ctx, "Error retrieving tracks from album", "id", id, "name", album.Name, err)
return nil, NewError(responses.ErrorGeneric, "Internal Error") return nil, NewError(responses.ErrorGeneric, "Internal Error")
} }
response := NewResponse() response := NewResponse()
response.AlbumWithSongsID3 = c.buildAlbum(r.Context(), dir) response.AlbumWithSongsID3 = c.buildAlbum(ctx, album, mfs)
return response, nil return response, nil
} }
@@ -255,25 +263,25 @@ func (c *BrowsingController) buildArtist(ctx context.Context, artist *model.Arti
return dir return dir
} }
func (c *BrowsingController) buildAlbum(ctx context.Context, d *engine.DirectoryInfo) *responses.AlbumWithSongsID3 { func (c *BrowsingController) buildAlbum(ctx context.Context, album *model.Album, mfs model.MediaFiles) *responses.AlbumWithSongsID3 {
dir := &responses.AlbumWithSongsID3{} dir := &responses.AlbumWithSongsID3{}
dir.Id = d.Id dir.Id = album.ID
dir.Name = d.Name dir.Name = album.Name
dir.Artist = d.Artist dir.Artist = album.AlbumArtist
dir.ArtistId = d.ArtistId dir.ArtistId = album.AlbumArtistID
dir.CoverArt = d.CoverArt dir.CoverArt = album.CoverArtId
dir.SongCount = d.SongCount dir.SongCount = album.SongCount
dir.Duration = d.Duration dir.Duration = int(album.Duration)
dir.PlayCount = d.PlayCount dir.PlayCount = album.PlayCount
dir.Year = d.Year dir.Year = album.MaxYear
dir.Genre = d.Genre dir.Genre = album.Genre
if !d.Created.IsZero() { if !album.CreatedAt.IsZero() {
dir.Created = &d.Created dir.Created = &album.CreatedAt
} }
if !d.Starred.IsZero() { if album.Starred {
dir.Starred = &d.Starred dir.Starred = &album.StarredAt
} }
dir.Song = ToChildren(ctx, d.Entries) dir.Song = ChildrenFromMediaFiles(ctx, mfs)
return dir return dir
} }
+12 -1
View File
@@ -187,7 +187,7 @@ func ChildFromMediaFile(ctx context.Context, mf model.MediaFile) responses.Child
child.CoverArt = "al-" + mf.AlbumID child.CoverArt = "al-" + mf.AlbumID
} }
child.ContentType = mf.ContentType() child.ContentType = mf.ContentType()
child.Path = mf.Path child.Path = fmt.Sprintf("%s/%s/%s.%s", realArtistName(mf), mf.Album, mf.Title, mf.Suffix)
child.DiscNumber = mf.DiscNumber child.DiscNumber = mf.DiscNumber
child.Created = &mf.CreatedAt child.Created = &mf.CreatedAt
child.AlbumId = mf.AlbumID child.AlbumId = mf.AlbumID
@@ -208,6 +208,17 @@ func ChildFromMediaFile(ctx context.Context, mf model.MediaFile) responses.Child
return child return child
} }
func realArtistName(mf model.MediaFile) string {
switch {
case mf.Compilation:
return consts.VariousArtists
case mf.AlbumArtist != "":
return mf.AlbumArtist
}
return mf.Artist
}
func ChildrenFromMediaFiles(ctx context.Context, mfs model.MediaFiles) []responses.Child { func ChildrenFromMediaFiles(ctx context.Context, mfs model.MediaFiles) []responses.Child {
children := make([]responses.Child, len(mfs)) children := make([]responses.Child, len(mfs))
for i, mf := range mfs { for i, mf := range mfs {