Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust artist by tags to hunt for artist cover #180

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions server/ctrlsubsonic/handlers_by_tags.go
Expand Up @@ -44,7 +44,7 @@ func (c *Controller) ServeGetArtists(r *http.Request) *spec.Response {
resp = append(resp, indexMap[key])
}
indexMap[key].Artists = append(indexMap[key].Artists,
spec.NewArtistByTags(artist))
spec.NewArtistByTags(artist, c.DB))
}
sub := spec.NewResponse()
sub.Artists = &spec.Artists{
Expand All @@ -69,7 +69,7 @@ func (c *Controller) ServeGetArtist(r *http.Request) *spec.Response {
}).
First(artist, id.Value)
sub := spec.NewResponse()
sub.Artist = spec.NewArtistByTags(artist)
sub.Artist = spec.NewArtistByTags(artist, c.DB)
sub.Artist.Albums = make([]*spec.Album, len(artist.Albums))
for i, album := range artist.Albums {
sub.Artist.Albums[i] = spec.NewAlbumByTags(album, artist)
Expand Down Expand Up @@ -200,7 +200,7 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response {
return spec.NewError(0, "find artists: %v", err)
}
for _, a := range artists {
results.Artists = append(results.Artists, spec.NewArtistByTags(a))
results.Artists = append(results.Artists, spec.NewArtistByTags(a, c.DB))
}

// search "albums"
Expand Down
25 changes: 25 additions & 0 deletions server/ctrlsubsonic/handlers_raw.go
Expand Up @@ -78,6 +78,8 @@ func coverGetPath(dbc *db.DB, podcastPath string, id specid.ID) (string, error)
switch id.Type {
case specid.Album:
return coverGetPathAlbum(dbc, id.Value)
case specid.Artist:
return coverGetPathArtist(dbc, id.Value)
case specid.Podcast:
return coverGetPathPodcast(dbc, podcastPath, id.Value)
case specid.PodcastEpisode:
Expand Down Expand Up @@ -108,6 +110,29 @@ func coverGetPathAlbum(dbc *db.DB, id int) (string, error) {
), nil
}

func coverGetPathArtist(dbc *db.DB, id int) (string, error) {
folder := &db.Album{}
err := dbc.DB.
Select("parent.*").
Joins("JOIN albums parent ON parent.id=albums.parent_id").
// Where("albums.tag_artist_id=?", id.Value).
Where("albums.tag_artist_id=?", id).
Find(&folder).
Error
if err != nil {
return "", fmt.Errorf("select album: %w", err)
}
if folder.Cover == "" {
return "", errCoverEmpty
}
return path.Join(
folder.RootDir,
folder.LeftPath,
folder.RightPath,
folder.Cover,
), nil
}

func coverGetPathPodcast(dbc *db.DB, podcastPath string, id int) (string, error) {
podcast := &db.Podcast{}
err := dbc.
Expand Down
22 changes: 21 additions & 1 deletion server/ctrlsubsonic/spec/construct_by_tags.go
Expand Up @@ -71,12 +71,32 @@ func NewTrackByTags(t *db.Track, album *db.Album) *TrackChild {
return ret
}

func NewArtistByTags(a *db.Artist) *Artist {
func NewArtistByTags(a *db.Artist, dbc *db.DB) *Artist {
// attempt to fetch a cover if possible
guessedArtistFolder := &db.Album{}
_ = dbc.
Select("parent.*").
Joins("JOIN albums parent ON parent.id=albums.parent_id").
// Where("albums.tag_artist_id=?", id.Value).
Where("albums.tag_artist_id=?", a.ID).
Find(&guessedArtistFolder).
Error

if guessedArtistFolder.Cover != "" {
return &Artist{
ID: a.SID(),
Name: a.Name,
AlbumCount: a.AlbumCount,
CoverID: a.SID(),
}
}

return &Artist{
ID: a.SID(),
Name: a.Name,
AlbumCount: a.AlbumCount,
}

}

func NewGenre(g *db.Genre) *Genre {
Expand Down