feat: render local artist images with no foreign key

This commit is contained in:
sentriz
2022-02-09 17:55:19 +00:00
committed by Senan Kelly
parent a0b9934d08
commit a74b5a261c
10 changed files with 160 additions and 49 deletions

View File

@@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/jinzhu/gorm"
@@ -248,12 +250,7 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
apiKey, _ := c.DB.GetSetting("lastfm_api_key")
if apiKey == "" {
sub := spec.NewResponse()
sub.ArtistInfoTwo = &spec.ArtistInfo{}
return sub
}
artist := &db.Artist{}
err = c.DB.
Where("id=?", id.Value).
@@ -262,26 +259,41 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
if errors.Is(err, gorm.ErrRecordNotFound) {
return spec.NewError(70, "artist with id `%s` not found", id)
}
sub := spec.NewResponse()
sub.ArtistInfoTwo = &spec.ArtistInfo{}
if artist.Cover != "" {
sub.ArtistInfoTwo.SmallImageURL = c.genArtistCoverURL(r, artist, 64)
sub.ArtistInfoTwo.MediumImageURL = c.genArtistCoverURL(r, artist, 126)
sub.ArtistInfoTwo.LargeImageURL = c.genArtistCoverURL(r, artist, 256)
}
apiKey, _ := c.DB.GetSetting("lastfm_api_key")
if apiKey == "" {
return sub
}
info, err := lastfm.ArtistGetInfo(apiKey, artist)
if err != nil {
return spec.NewError(0, "fetching artist info: %v", err)
}
sub := spec.NewResponse()
sub.ArtistInfoTwo = &spec.ArtistInfo{
Biography: info.Bio.Summary,
MusicBrainzID: info.MBID,
LastFMURL: info.URL,
}
for _, image := range info.Image {
switch image.Size {
case "small":
sub.ArtistInfoTwo.SmallImageURL = image.Text
case "medium":
sub.ArtistInfoTwo.MediumImageURL = image.Text
case "large":
sub.ArtistInfoTwo.LargeImageURL = image.Text
sub.ArtistInfoTwo.Biography = info.Bio.Summary
sub.ArtistInfoTwo.MusicBrainzID = info.MBID
sub.ArtistInfoTwo.LastFMURL = info.URL
if artist.Cover == "" {
for _, image := range info.Image {
switch image.Size {
case "small":
sub.ArtistInfoTwo.SmallImageURL = image.Text
case "medium":
sub.ArtistInfoTwo.MediumImageURL = image.Text
case "large":
sub.ArtistInfoTwo.LargeImageURL = image.Text
}
}
}
count := params.GetOrInt("count", 20)
inclNotPresent := params.GetOrBool("includeNotPresent", false)
for i, similarInfo := range info.Similar.Artists {
@@ -310,6 +322,7 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
sub.ArtistInfoTwo.SimilarArtist = append(
sub.ArtistInfoTwo.SimilarArtist, similar)
}
return sub
}
@@ -371,3 +384,15 @@ func (c *Controller) ServeGetStarredTwo(r *http.Request) *spec.Response {
}
return sub
}
func (c *Controller) genArtistCoverURL(r *http.Request, artist *db.Artist, size int) string {
coverURL, _ := url.Parse(c.BaseURL(r))
coverURL.Path = c.Path("/rest/getCoverArt")
query := r.URL.Query()
query.Set("id", artist.SID().String())
query.Set("size", strconv.Itoa(size))
coverURL.RawQuery = query.Encode()
return coverURL.String()
}

View File

@@ -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:
@@ -90,7 +92,6 @@ func coverGetPath(dbc *db.DB, podcastPath string, id specid.ID) (string, error)
func coverGetPathAlbum(dbc *db.DB, id int) (string, error) {
folder := &db.Album{}
err := dbc.DB.
Preload("Parent").
Select("id, root_dir, left_path, right_path, cover").
First(folder, id).
Error
@@ -108,6 +109,28 @@ 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.Debug().
Select("parent.id, parent.root_dir, parent.left_path, parent.right_path, parent.cover").
Joins("JOIN albums parent ON parent.id=albums.parent_id").
Where("albums.tag_artist_id=?", id).
Find(folder).
Error
if err != nil {
return "", fmt.Errorf("select guessed artist folder: %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.

View File

@@ -76,11 +76,15 @@ func NewArtistByFolder(f *db.Album) *Artist {
// an album is also a folder. so we're constructing an artist
// from an "album" where
// maybe TODO: rename the Album model to Folder
return &Artist{
a := &Artist{
ID: f.SID(),
Name: f.RightPath,
AlbumCount: f.ChildCount,
}
if f.Cover != "" {
a.CoverID = f.SID()
}
return a
}
func NewDirectoryByFolder(f *db.Album, children []*TrackChild) *Directory {

View File

@@ -72,11 +72,15 @@ func NewTrackByTags(t *db.Track, album *db.Album) *TrackChild {
}
func NewArtistByTags(a *db.Artist) *Artist {
return &Artist{
r := &Artist{
ID: a.SID(),
Name: a.Name,
AlbumCount: a.AlbumCount,
}
if a.Cover != "" {
r.CoverID = a.SID()
}
return r
}
func NewGenre(g *db.Genre) *Genre {