Add command line M3U exporter. Closes #1914

This commit is contained in:
Deluan
2022-12-21 14:37:08 -05:00
parent 5d8318f7b3
commit 28389fb05e
12 changed files with 188 additions and 63 deletions
+17
View File
@@ -11,6 +11,7 @@ import (
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request"
)
var (
@@ -65,3 +66,19 @@ func Validate(tokenStr string) (map[string]interface{}, error) {
}
return token.AsMap(context.Background())
}
func WithAdminUser(ctx context.Context, ds model.DataStore) context.Context {
u, err := ds.User(ctx).FindFirstAdmin()
if err != nil {
c, err := ds.User(ctx).CountAll()
if c == 0 && err == nil {
log.Debug(ctx, "Scanner: No admin user yet!", err)
} else {
log.Error(ctx, "Scanner: No admin user found!", err)
}
u = &model.User{}
}
ctx = request.WithUsername(ctx, u.UserName)
return request.WithUser(ctx, *u)
}
+6 -11
View File
@@ -22,7 +22,7 @@ import (
type Playlists interface {
ImportFile(ctx context.Context, dir string, fname string) (*model.Playlist, error)
Update(ctx context.Context, playlistId string, name *string, comment *string, public *bool, idsToAdd []string, idxToRemove []int) error
Update(ctx context.Context, playlistID string, name *string, comment *string, public *bool, idsToAdd []string, idxToRemove []int) error
}
type playlists struct {
@@ -33,11 +33,6 @@ func NewPlaylists(ds model.DataStore) Playlists {
return &playlists{ds: ds}
}
func IsPlaylist(filePath string) bool {
extension := strings.ToLower(filepath.Ext(filePath))
return extension == ".m3u" || extension == ".m3u8" || extension == ".nsp"
}
func (s *playlists) ImportFile(ctx context.Context, dir string, fname string) (*model.Playlist, error) {
pls, err := s.parsePlaylist(ctx, fname, dir)
if err != nil {
@@ -194,7 +189,7 @@ func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
return 0, nil, nil
}
func (s *playlists) Update(ctx context.Context, playlistId string,
func (s *playlists) Update(ctx context.Context, playlistID string,
name *string, comment *string, public *bool,
idsToAdd []string, idxToRemove []int) error {
needsInfoUpdate := name != nil || comment != nil || public != nil
@@ -205,18 +200,18 @@ func (s *playlists) Update(ctx context.Context, playlistId string,
var err error
repo := tx.Playlist(ctx)
if needsTrackRefresh {
pls, err = repo.GetWithTracks(playlistId)
pls, err = repo.GetWithTracks(playlistID)
pls.RemoveTracks(idxToRemove)
pls.AddTracks(idsToAdd)
} else {
if len(idsToAdd) > 0 {
_, err = repo.Tracks(playlistId).Add(idsToAdd)
_, err = repo.Tracks(playlistID).Add(idsToAdd)
if err != nil {
return err
}
}
if needsInfoUpdate {
pls, err = repo.Get(playlistId)
pls, err = repo.Get(playlistID)
}
}
if err != nil {
@@ -237,7 +232,7 @@ func (s *playlists) Update(ctx context.Context, playlistId string,
}
// Special case: The playlist is now empty
if len(idxToRemove) > 0 && len(pls.Tracks) == 0 {
if err = repo.Tracks(playlistId).DeleteAll(); err != nil {
if err = repo.Tracks(playlistID).DeleteAll(); err != nil {
return err
}
}
-15
View File
@@ -2,7 +2,6 @@ package core
import (
"context"
"path/filepath"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/tests"
@@ -10,20 +9,6 @@ import (
. "github.com/onsi/gomega"
)
var _ = Describe("IsPlaylist", func() {
It("returns true for a M3U file", func() {
Expect(IsPlaylist(filepath.Join("path", "to", "test.m3u"))).To(BeTrue())
})
It("returns true for a M3U8 file", func() {
Expect(IsPlaylist(filepath.Join("path", "to", "test.m3u8"))).To(BeTrue())
})
It("returns false for a non-playlist file", func() {
Expect(IsPlaylist("testm3u")).To(BeFalse())
})
})
var _ = Describe("Playlists", func() {
var ds model.DataStore
var ps Playlists