feat(subsonic): support embedded cover art

closes #11
closes #556
This commit is contained in:
sentriz
2025-09-29 18:24:37 +01:00
parent 2fcba12650
commit ec7cffdd57
6 changed files with 109 additions and 29 deletions

No files matched your search

+28 -2
View File
@@ -21,6 +21,7 @@ import (
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
"go.senan.xyz/gonic/server/ctrlsubsonic/specidpaths"
"go.senan.xyz/gonic/tags"
"go.senan.xyz/gonic/transcode"
)
@@ -49,7 +50,7 @@ func (c *Controller) ServeGetCoverArt(w http.ResponseWriter, r *http.Request) *s
_, err = os.Stat(cachePath)
switch {
case os.IsNotExist(err):
reader, err := coverFor(c.dbc, c.artistInfoCache, id)
reader, err := coverFor(c.dbc, c.artistInfoCache, c.tagReader, id)
if err != nil {
return spec.NewError(10, "couldn't find cover %q: %v", id, err)
}
@@ -76,7 +77,7 @@ var (
)
// TODO: can we use specidpaths.Locate here?
func coverFor(dbc *db.DB, artistInfoCache *artistinfocache.ArtistInfoCache, id specid.ID) (io.ReadCloser, error) {
func coverFor(dbc *db.DB, artistInfoCache *artistinfocache.ArtistInfoCache, tagReader tags.Reader, id specid.ID) (io.ReadCloser, error) {
switch id.Type {
case specid.Album:
return coverForAlbum(dbc, id.Value)
@@ -86,6 +87,8 @@ func coverFor(dbc *db.DB, artistInfoCache *artistinfocache.ArtistInfoCache, id s
return coverForPodcast(dbc, id.Value)
case specid.PodcastEpisode:
return coverForPodcastEpisode(dbc, id.Value)
case specid.Track:
return coverForTrack(dbc, tagReader, id.Value)
default:
return nil, errCoverNotFound
}
@@ -150,6 +153,29 @@ func coverForPodcastEpisode(dbc *db.DB, id int) (*os.File, error) {
return os.Open(filepath.Join(pe.Podcast.RootDir, pe.Podcast.Image))
}
func coverForTrack(dbc *db.DB, tagReader tags.Reader, id int) (io.ReadCloser, error) {
var tr db.Track
err := dbc.
Preload("Album").
First(&tr, id).
Error
if err != nil {
return nil, fmt.Errorf("select track: %w", err)
}
absPath := tr.AbsPath()
cover, err := tagReader.ReadCover(absPath)
if err != nil {
return nil, fmt.Errorf("read cover: %w", err)
}
if len(cover) == 0 {
return nil, errCoverEmpty
}
return io.NopCloser(bytes.NewReader(cover)), nil
}
func coverScaleAndSave(reader io.Reader, cachePath string, size int) error {
src, err := imaging.Decode(reader)
if err != nil {