feat: add support for playlist cover art on filesystem
closes #650 Co-authored-by: Matteo <girardi.matteo.dev@gmail.com>
This commit is contained in:
No files matched your search
@@ -53,6 +53,10 @@ func NewStore(basePath string) (*Store, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Store) BasePath() string {
|
||||
return s.basePath
|
||||
}
|
||||
|
||||
// List finds playlist items in s.basePath.
|
||||
// the expected format is <base path>/<user id>/**/<playlist name>.m3u
|
||||
func (s *Store) List() ([]string, error) {
|
||||
|
||||
@@ -39,7 +39,7 @@ func (c *Controller) ServeGetPlaylists(r *http.Request) *spec.Response {
|
||||
continue
|
||||
}
|
||||
playlistID := playlistIDEncode(path)
|
||||
rendered, err := playlistRender(c, params, playlistID, playlist, false)
|
||||
rendered, err := playlistRender(c, params, playlist, playlistID, false)
|
||||
if err != nil {
|
||||
return spec.NewError(0, "error rendering playlist %q: %v", path, err)
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func (c *Controller) ServeGetPlaylists(r *http.Request) *spec.Response {
|
||||
|
||||
func (c *Controller) ServeGetPlaylist(r *http.Request) *spec.Response {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
playlistID, err := params.GetFirst("id", "playlistId")
|
||||
playlistID, err := params.GetFirstID("id", "playlistId")
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide an `id` parameter")
|
||||
}
|
||||
@@ -59,7 +59,7 @@ func (c *Controller) ServeGetPlaylist(r *http.Request) *spec.Response {
|
||||
return spec.NewError(70, "playlist with id %s not found", playlistID)
|
||||
}
|
||||
sub := spec.NewResponse()
|
||||
rendered, err := playlistRender(c, params, playlistID, playlist, true)
|
||||
rendered, err := playlistRender(c, params, playlist, playlistID, true)
|
||||
if err != nil {
|
||||
return spec.NewError(0, "error rendering playlist: %v", err)
|
||||
}
|
||||
@@ -71,7 +71,10 @@ func (c *Controller) ServeCreateOrUpdatePlaylist(r *http.Request) *spec.Response
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
|
||||
playlistID := params.GetFirstOr( /* default */ "", "id", "playlistId")
|
||||
playlistID, err := params.GetFirstID("id", "playlistId")
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide an `id` or `playlistId` parameter")
|
||||
}
|
||||
playlistPath := playlistIDDecode(playlistID)
|
||||
|
||||
var playlist playlistp.Playlist
|
||||
@@ -112,7 +115,7 @@ func (c *Controller) ServeCreateOrUpdatePlaylist(r *http.Request) *spec.Response
|
||||
}
|
||||
|
||||
sub := spec.NewResponse()
|
||||
rendered, err := playlistRender(c, params, playlistID, &playlist, true)
|
||||
rendered, err := playlistRender(c, params, &playlist, playlistID, true)
|
||||
if err != nil {
|
||||
return spec.NewError(0, "error rendering playlist: %v", err)
|
||||
}
|
||||
@@ -124,7 +127,10 @@ func (c *Controller) ServeUpdatePlaylist(r *http.Request) *spec.Response {
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
|
||||
playlistID := params.GetFirstOr( /* default */ "", "id", "playlistId")
|
||||
playlistID, err := params.GetFirstID("id", "playlistId")
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide an `id` or `playlistId` parameter")
|
||||
}
|
||||
playlistPath := playlistIDDecode(playlistID)
|
||||
playlist, err := c.playlistStore.Read(playlistPath)
|
||||
if err != nil {
|
||||
@@ -173,23 +179,29 @@ func (c *Controller) ServeUpdatePlaylist(r *http.Request) *spec.Response {
|
||||
|
||||
func (c *Controller) ServeDeletePlaylist(r *http.Request) *spec.Response {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
playlistID := params.GetFirstOr( /* default */ "", "id", "playlistId")
|
||||
playlistID, err := params.GetFirstID("id", "playlistId")
|
||||
if err != nil {
|
||||
return spec.NewError(10, "please provide an `id` or `playlistId` parameter")
|
||||
}
|
||||
if err := c.playlistStore.Delete(playlistIDDecode(playlistID)); err != nil {
|
||||
return spec.NewError(0, "delete playlist: %v", err)
|
||||
}
|
||||
return spec.NewResponse()
|
||||
}
|
||||
|
||||
func playlistIDEncode(path string) string {
|
||||
return base64.URLEncoding.EncodeToString([]byte(path))
|
||||
func playlistIDEncode(path string) specid.ID {
|
||||
return specid.ID{
|
||||
Type: specid.Playlist,
|
||||
StringValue: base64.URLEncoding.EncodeToString([]byte(path)),
|
||||
}
|
||||
}
|
||||
|
||||
func playlistIDDecode(id string) string {
|
||||
path, _ := base64.URLEncoding.DecodeString(id)
|
||||
func playlistIDDecode(id specid.ID) string {
|
||||
path, _ := base64.URLEncoding.DecodeString(id.StringValue)
|
||||
return string(path)
|
||||
}
|
||||
|
||||
func playlistRender(c *Controller, params params.Params, playlistID string, playlist *playlistp.Playlist, withItems bool) (*spec.Playlist, error) {
|
||||
func playlistRender(c *Controller, params params.Params, playlist *playlistp.Playlist, playlistID specid.ID, withItems bool) (*spec.Playlist, error) {
|
||||
user := &db.User{}
|
||||
if err := c.dbc.Where("id=?", playlist.UserID).Find(user).Error; err != nil {
|
||||
return nil, fmt.Errorf("find user by id: %w", err)
|
||||
|
||||
@@ -11,13 +11,16 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/jinzhu/gorm"
|
||||
"go.senan.xyz/wrtag/coverparse"
|
||||
|
||||
"go.senan.xyz/gonic/db"
|
||||
"go.senan.xyz/gonic/infocache/artistinfocache"
|
||||
"go.senan.xyz/gonic/playlist"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
|
||||
@@ -77,7 +80,7 @@ func (c *Controller) ServeGetCoverArt(w http.ResponseWriter, r *http.Request) *s
|
||||
return nil
|
||||
}
|
||||
|
||||
reader, err := coverFor(c.dbc, c.artistInfoCache, c.tagReader, id)
|
||||
reader, err := coverFor(c.dbc, c.artistInfoCache, c.playlistStore, c.tagReader, id)
|
||||
if err != nil {
|
||||
return spec.NewError(10, "couldn't find cover %q: %v", id, err)
|
||||
}
|
||||
@@ -131,7 +134,7 @@ var (
|
||||
)
|
||||
|
||||
// TODO: can we use specidpaths.Locate here?
|
||||
func coverFor(dbc *db.DB, artistInfoCache *artistinfocache.ArtistInfoCache, tagReader tags.Reader, id specid.ID) (io.ReadCloser, error) {
|
||||
func coverFor(dbc *db.DB, artistInfoCache *artistinfocache.ArtistInfoCache, playlistStore *playlist.Store, tagReader tags.Reader, id specid.ID) (io.ReadCloser, error) {
|
||||
switch id.Type {
|
||||
case specid.Album:
|
||||
return coverForAlbum(dbc, id.Value)
|
||||
@@ -143,6 +146,8 @@ func coverFor(dbc *db.DB, artistInfoCache *artistinfocache.ArtistInfoCache, tagR
|
||||
return coverForPodcastEpisode(dbc, id.Value)
|
||||
case specid.Track:
|
||||
return coverForTrack(dbc, tagReader, id.Value)
|
||||
case specid.Playlist:
|
||||
return coverForPlaylist(playlistStore, id)
|
||||
default:
|
||||
return nil, errCoverNotFound
|
||||
}
|
||||
@@ -230,6 +235,35 @@ func coverForTrack(dbc *db.DB, tagReader tags.Reader, id int) (io.ReadCloser, er
|
||||
return io.NopCloser(bytes.NewReader(cover)), nil
|
||||
}
|
||||
|
||||
func coverForPlaylist(playlistStore *playlist.Store, id specid.ID) (*os.File, error) {
|
||||
playlistPath := playlistIDDecode(id)
|
||||
playlistDir := filepath.Join(playlistStore.BasePath(), filepath.Dir(playlistPath))
|
||||
playlistBase := strings.TrimSuffix(filepath.Base(playlistPath), filepath.Ext(playlistPath))
|
||||
|
||||
entries, err := os.ReadDir(playlistDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read playlist dir: %w", err)
|
||||
}
|
||||
|
||||
var cover string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := entry.Name()
|
||||
nameBase := strings.TrimSuffix(name, filepath.Ext(name))
|
||||
if nameBase == playlistBase && coverparse.IsCover(name) {
|
||||
cover = coverparse.BestBetween(cover, name)
|
||||
}
|
||||
}
|
||||
|
||||
if cover == "" {
|
||||
return nil, errCoverEmpty
|
||||
}
|
||||
|
||||
return os.Open(filepath.Join(playlistDir, cover))
|
||||
}
|
||||
|
||||
func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.Response {
|
||||
params := r.Context().Value(CtxParams).(params.Params)
|
||||
user := r.Context().Value(CtxUser).(*db.User)
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
package ctrlsubsonic
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.senan.xyz/gonic/playlist"
|
||||
)
|
||||
|
||||
func TestCoverForPlaylist(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("cover found jpg", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
store, err := playlist.NewStore(tmp)
|
||||
require.NoError(t, err)
|
||||
|
||||
playlistDir := filepath.Join(tmp, "1")
|
||||
require.NoError(t, os.MkdirAll(playlistDir, 0o755))
|
||||
require.NoError(t, touch(filepath.Join(playlistDir, "test-playlist.jpg")))
|
||||
|
||||
playlistID := playlistIDEncode("1/test-playlist.m3u")
|
||||
file, err := coverForPlaylist(store, playlistID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, file)
|
||||
require.NoError(t, file.Close())
|
||||
})
|
||||
|
||||
t.Run("cover found png", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
store, err := playlist.NewStore(tmp)
|
||||
require.NoError(t, err)
|
||||
|
||||
playlistDir := filepath.Join(tmp, "1")
|
||||
require.NoError(t, os.MkdirAll(playlistDir, 0o755))
|
||||
require.NoError(t, touch(filepath.Join(playlistDir, "test-playlist.png")))
|
||||
|
||||
playlistID := playlistIDEncode("1/test-playlist.m3u")
|
||||
file, err := coverForPlaylist(store, playlistID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, file)
|
||||
require.NoError(t, file.Close())
|
||||
})
|
||||
|
||||
t.Run("cover not found", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
store, err := playlist.NewStore(tmp)
|
||||
require.NoError(t, err)
|
||||
|
||||
playlistDir := filepath.Join(tmp, "1")
|
||||
require.NoError(t, os.MkdirAll(playlistDir, 0o755))
|
||||
|
||||
playlistID := playlistIDEncode("1/test-playlist.m3u")
|
||||
file, err := coverForPlaylist(store, playlistID)
|
||||
require.ErrorIs(t, err, errCoverEmpty)
|
||||
require.Nil(t, file)
|
||||
})
|
||||
|
||||
t.Run("nested playlist path", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
store, err := playlist.NewStore(tmp)
|
||||
require.NoError(t, err)
|
||||
|
||||
playlistDir := filepath.Join(tmp, "1", "subfolder")
|
||||
require.NoError(t, os.MkdirAll(playlistDir, 0o755))
|
||||
require.NoError(t, touch(filepath.Join(playlistDir, "my-nested-playlist.jpg")))
|
||||
|
||||
playlistID := playlistIDEncode("1/subfolder/my-nested-playlist.m3u")
|
||||
file, err := coverForPlaylist(store, playlistID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, file)
|
||||
require.NoError(t, file.Close())
|
||||
})
|
||||
|
||||
t.Run("playlist directory missing", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
store, err := playlist.NewStore(tmp)
|
||||
require.NoError(t, err)
|
||||
|
||||
playlistID := playlistIDEncode("1/test-playlist.m3u")
|
||||
file, err := coverForPlaylist(store, playlistID)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, file)
|
||||
})
|
||||
|
||||
t.Run("different playlists different covers", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
store, err := playlist.NewStore(tmp)
|
||||
require.NoError(t, err)
|
||||
|
||||
playlistDir := filepath.Join(tmp, "1")
|
||||
require.NoError(t, os.MkdirAll(playlistDir, 0o755))
|
||||
require.NoError(t, touch(filepath.Join(playlistDir, "playlist-a.jpg")))
|
||||
require.NoError(t, touch(filepath.Join(playlistDir, "playlist-b.png")))
|
||||
|
||||
fileA, err := coverForPlaylist(store, playlistIDEncode("1/playlist-a.m3u"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "playlist-a.jpg", filepath.Base(fileA.Name()))
|
||||
require.NoError(t, fileA.Close())
|
||||
|
||||
fileB, err := coverForPlaylist(store, playlistIDEncode("1/playlist-b.m3u"))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "playlist-b.png", filepath.Base(fileB.Name()))
|
||||
require.NoError(t, fileB.Close())
|
||||
})
|
||||
}
|
||||
|
||||
func touch(path string) error {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return f.Close()
|
||||
}
|
||||
@@ -320,7 +320,7 @@ type Playlists struct {
|
||||
}
|
||||
|
||||
type Playlist struct {
|
||||
ID string `xml:"id,attr" json:"id"`
|
||||
ID specid.ID `xml:"id,attr" json:"id"`
|
||||
Name string `xml:"name,attr" json:"name"`
|
||||
Comment string `xml:"comment,attr" json:"comment"`
|
||||
Owner string `xml:"owner,attr" json:"owner"`
|
||||
|
||||
@@ -27,13 +27,15 @@ const (
|
||||
Podcast IDT = "pd"
|
||||
PodcastEpisode IDT = "pe"
|
||||
InternetRadioStation IDT = "ir"
|
||||
Playlist IDT = "pl"
|
||||
separator = "-"
|
||||
)
|
||||
|
||||
//nolint:musttag
|
||||
type ID struct {
|
||||
Type IDT
|
||||
Value int
|
||||
Type IDT
|
||||
Value int
|
||||
StringValue string
|
||||
}
|
||||
|
||||
func New(in string) (ID, error) {
|
||||
@@ -41,10 +43,17 @@ func New(in string) (ID, error) {
|
||||
if !ok {
|
||||
return ID{}, ErrBadSeparator
|
||||
}
|
||||
|
||||
switch IDT(partType) {
|
||||
case Playlist:
|
||||
return ID{Type: Playlist, StringValue: partValue}, nil
|
||||
}
|
||||
|
||||
val, err := strconv.Atoi(partValue)
|
||||
if err != nil {
|
||||
return ID{}, fmt.Errorf("%q: %w", partValue, ErrNotAnInt)
|
||||
}
|
||||
|
||||
switch IDT(partType) {
|
||||
case Artist:
|
||||
return ID{Type: Artist, Value: val}, nil
|
||||
@@ -64,10 +73,13 @@ func New(in string) (ID, error) {
|
||||
}
|
||||
|
||||
func (i ID) String() string {
|
||||
if i.Value == 0 {
|
||||
return "-1"
|
||||
switch {
|
||||
case i.Value != 0:
|
||||
return fmt.Sprintf("%s%s%d", i.Type, separator, i.Value)
|
||||
case i.StringValue != "":
|
||||
return fmt.Sprintf("%s%s%s", i.Type, separator, i.StringValue)
|
||||
}
|
||||
return fmt.Sprintf("%s%s%d", i.Type, separator, i.Value)
|
||||
return "-1"
|
||||
}
|
||||
|
||||
func (i ID) MarshalJSON() ([]byte, error) {
|
||||
|
||||
Reference in New Issue
Block a user