From fd8d6adf3409a4322e9b25096cf6a0c10e4fb83c Mon Sep 17 00:00:00 2001 From: 0chroma <21014+0chroma@users.noreply.github.com> Date: Sat, 7 Sep 2024 02:26:33 -0700 Subject: [PATCH 1/6] add playlist prefix argument --- cmd/gonic/gonic.go | 8 +++++++- db/migrations.go | 3 ++- playlist/playlist.go | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index 4548904c..37b54d59 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -65,6 +65,7 @@ func main() { flag.Var(&confMusicPaths, "music-path", "path to music") confPlaylistsPath := flag.String("playlists-path", "", "path to your list of new or existing m3u playlists that gonic can manage") + confPlaylistsPrefix := flag.String("playlists-prefix", "", "path prefix used inside of m3u playlists gonic generates") confDBPath := flag.String("db-path", "gonic.db", "path to database (optional)") @@ -127,6 +128,10 @@ func main() { log.Fatalf("checking playlist directory: %v", err) } + if *confPlaylistsPrefix == "" { + confPlaylistsPrefix = confPlaylistsPath + } + cacheDirAudio := path.Join(*confCachePath, "audio") cacheDirCovers := path.Join(*confCachePath, "covers") if err := os.MkdirAll(cacheDirAudio, os.ModePerm); err != nil { @@ -147,6 +152,7 @@ func main() { DBPath: *confDBPath, OriginalMusicPath: confMusicPaths[0].path, PlaylistsPath: *confPlaylistsPath, + PlaylistsPrefix: *confPlaylistsPrefix, PodcastsPath: *confPodcastPath, }) if err != nil { @@ -215,7 +221,7 @@ func main() { listenbrainzClient := listenbrainz.NewClient() lastfmClient := lastfm.NewClient(lastfmClientKeySecretFunc) - playlistStore, err := playlist.NewStore(*confPlaylistsPath) + playlistStore, err := playlist.NewStore(*confPlaylistsPath, *confPlaylistsPrefix) if err != nil { log.Panicf("error creating playlists store: %v", err) } diff --git a/db/migrations.go b/db/migrations.go index 447bfdf6..493e8c83 100644 --- a/db/migrations.go +++ b/db/migrations.go @@ -23,6 +23,7 @@ type MigrationContext struct { DBPath string OriginalMusicPath string PlaylistsPath string + PlaylistsPrefix string PodcastsPath string } @@ -507,7 +508,7 @@ func migratePlaylistsToM3U(tx *gorm.DB, ctx MigrationContext) error { return "" } - store, err := playlist.NewStore(ctx.PlaylistsPath) + store, err := playlist.NewStore(ctx.PlaylistsPath, ctx.PlaylistsPrefix) if err != nil { return fmt.Errorf("create playlists store: %w", err) } diff --git a/playlist/playlist.go b/playlist/playlist.go index e51dbc16..564f1780 100644 --- a/playlist/playlist.go +++ b/playlist/playlist.go @@ -40,7 +40,7 @@ type Store struct { mu sync.Mutex } -func NewStore(basePath string) (*Store, error) { +func NewStore(basePath string, prefix string) (*Store, error) { if basePath == "" { return nil, ErrInvalidBasePath } From 34f953089c6802bde9f0aa937d571870c1dfde0d Mon Sep 17 00:00:00 2001 From: 0chroma <21014+0chroma@users.noreply.github.com> Date: Sat, 7 Sep 2024 04:31:17 -0700 Subject: [PATCH 2/6] since multiple music-paths can exist, make it relative to playlist file instead --- cmd/gonic/gonic.go | 10 +++------- playlist/playlist.go | 8 +++++++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index 37b54d59..26085c17 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -65,7 +65,7 @@ func main() { flag.Var(&confMusicPaths, "music-path", "path to music") confPlaylistsPath := flag.String("playlists-path", "", "path to your list of new or existing m3u playlists that gonic can manage") - confPlaylistsPrefix := flag.String("playlists-prefix", "", "path prefix used inside of m3u playlists gonic generates") + confPlaylistsRelative := flag.Bool("playlists-relative", false, "make song file paths relative in the m3u playlists gonic generates") confDBPath := flag.String("db-path", "gonic.db", "path to database (optional)") @@ -128,10 +128,6 @@ func main() { log.Fatalf("checking playlist directory: %v", err) } - if *confPlaylistsPrefix == "" { - confPlaylistsPrefix = confPlaylistsPath - } - cacheDirAudio := path.Join(*confCachePath, "audio") cacheDirCovers := path.Join(*confCachePath, "covers") if err := os.MkdirAll(cacheDirAudio, os.ModePerm); err != nil { @@ -152,7 +148,7 @@ func main() { DBPath: *confDBPath, OriginalMusicPath: confMusicPaths[0].path, PlaylistsPath: *confPlaylistsPath, - PlaylistsPrefix: *confPlaylistsPrefix, + PlaylistsRelative: confPlaylistsRelative, PodcastsPath: *confPodcastPath, }) if err != nil { @@ -221,7 +217,7 @@ func main() { listenbrainzClient := listenbrainz.NewClient() lastfmClient := lastfm.NewClient(lastfmClientKeySecretFunc) - playlistStore, err := playlist.NewStore(*confPlaylistsPath, *confPlaylistsPrefix) + playlistStore, err := playlist.NewStore(*confPlaylistsPath, *confPlaylistsRelative) if err != nil { log.Panicf("error creating playlists store: %v", err) } diff --git a/playlist/playlist.go b/playlist/playlist.go index 564f1780..1a245cea 100644 --- a/playlist/playlist.go +++ b/playlist/playlist.go @@ -37,10 +37,11 @@ type Playlist struct { type Store struct { basePath string + relative bool mu sync.Mutex } -func NewStore(basePath string, prefix string) (*Store, error) { +func NewStore(basePath string, relative bool) (*Store, error) { if basePath == "" { return nil, ErrInvalidBasePath } @@ -50,6 +51,7 @@ func NewStore(basePath string, prefix string) (*Store, error) { return &Store{ basePath: basePath, + relative: relative, }, nil } @@ -126,6 +128,8 @@ func (s *Store) Read(relPath string) (*Playlist, error) { if strings.HasPrefix(line, "#") { continue } + //transform to absolute path + //TODO maybe don't need? playlist.Items = append(playlist.Items, line) } @@ -178,6 +182,8 @@ func (s *Store) Write(relPath string, playlist *Playlist) error { fmt.Fprintln(file, encodeAttr(attrCommment, playlist.Comment)) fmt.Fprintln(file, encodeAttr(attrIsPublic, fmt.Sprint(playlist.IsPublic))) for _, line := range playlist.Items { + //transform to path relative to basePath + //TODO fmt.Fprintln(file, line) } From 43162378aadd8bc24997be3883b7fc802426f8bb Mon Sep 17 00:00:00 2001 From: 0chroma <21014+0chroma@users.noreply.github.com> Date: Sat, 7 Sep 2024 05:04:21 -0700 Subject: [PATCH 3/6] finish relative path feature --- cmd/gonic/gonic.go | 2 +- db/migrations.go | 4 ++-- playlist/playlist.go | 16 +++++++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index 26085c17..176200c2 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -148,7 +148,7 @@ func main() { DBPath: *confDBPath, OriginalMusicPath: confMusicPaths[0].path, PlaylistsPath: *confPlaylistsPath, - PlaylistsRelative: confPlaylistsRelative, + PlaylistsRelative: *confPlaylistsRelative, PodcastsPath: *confPodcastPath, }) if err != nil { diff --git a/db/migrations.go b/db/migrations.go index 493e8c83..5eeb8e28 100644 --- a/db/migrations.go +++ b/db/migrations.go @@ -23,7 +23,7 @@ type MigrationContext struct { DBPath string OriginalMusicPath string PlaylistsPath string - PlaylistsPrefix string + PlaylistsRelative bool PodcastsPath string } @@ -508,7 +508,7 @@ func migratePlaylistsToM3U(tx *gorm.DB, ctx MigrationContext) error { return "" } - store, err := playlist.NewStore(ctx.PlaylistsPath, ctx.PlaylistsPrefix) + store, err := playlist.NewStore(ctx.PlaylistsPath, ctx.PlaylistsRelative) if err != nil { return fmt.Errorf("create playlists store: %w", err) } diff --git a/playlist/playlist.go b/playlist/playlist.go index 1a245cea..9f48f94f 100644 --- a/playlist/playlist.go +++ b/playlist/playlist.go @@ -128,8 +128,11 @@ func (s *Store) Read(relPath string) (*Playlist, error) { if strings.HasPrefix(line, "#") { continue } - //transform to absolute path - //TODO maybe don't need? + + //file path might be relative if using playlists-relative + if !filepath.IsAbs(line) { + line = filepath.Join(filepath.Dir(absPath), line) + } playlist.Items = append(playlist.Items, line) } @@ -183,10 +186,13 @@ func (s *Store) Write(relPath string, playlist *Playlist) error { fmt.Fprintln(file, encodeAttr(attrIsPublic, fmt.Sprint(playlist.IsPublic))) for _, line := range playlist.Items { //transform to path relative to basePath - //TODO - fmt.Fprintln(file, line) + relativePath, err := filepath.Rel(filepath.Dir(absPath), line) + if s.relative && err != nil { + fmt.Fprintln(file, relativePath) + } else { + fmt.Fprintln(file, line) + } } - return nil } From c77ca294ec0f80ca56917c025c5774c51c0c0d89 Mon Sep 17 00:00:00 2001 From: 0chroma <21014+0chroma@users.noreply.github.com> Date: Sat, 7 Sep 2024 05:24:06 -0700 Subject: [PATCH 4/6] finalize feature and fix bug --- playlist/playlist.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/playlist/playlist.go b/playlist/playlist.go index 9f48f94f..dc08572e 100644 --- a/playlist/playlist.go +++ b/playlist/playlist.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io/fs" + "log" "os" "path/filepath" "strconv" @@ -129,10 +130,11 @@ func (s *Store) Read(relPath string) (*Playlist, error) { continue } - //file path might be relative if using playlists-relative + //file path might be relative if using -playlists-relative if !filepath.IsAbs(line) { line = filepath.Join(filepath.Dir(absPath), line) } + playlist.Items = append(playlist.Items, line) } @@ -185,13 +187,16 @@ func (s *Store) Write(relPath string, playlist *Playlist) error { fmt.Fprintln(file, encodeAttr(attrCommment, playlist.Comment)) fmt.Fprintln(file, encodeAttr(attrIsPublic, fmt.Sprint(playlist.IsPublic))) for _, line := range playlist.Items { - //transform to path relative to basePath - relativePath, err := filepath.Rel(filepath.Dir(absPath), line) - if s.relative && err != nil { - fmt.Fprintln(file, relativePath) - } else { - fmt.Fprintln(file, line) + if s.relative { + //transform to path relative to playlist's dir + relativePath, err := filepath.Rel(filepath.Dir(absPath), line) + if err == nil { + line = relativePath + } else { + log.Printf("Warning: could not make playlist entry path's relative - %v\n", err) + } } + fmt.Fprintln(file, line) } return nil } From ae53b52c91c3e05159fc2e05f9a466f0d08ae556 Mon Sep 17 00:00:00 2001 From: 0chroma <21014+0chroma@users.noreply.github.com> Date: Sat, 7 Sep 2024 05:34:46 -0700 Subject: [PATCH 5/6] fix tests/lint --- playlist/playlist.go | 4 ++-- playlist/playlist_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/playlist/playlist.go b/playlist/playlist.go index dc08572e..9ff13af7 100644 --- a/playlist/playlist.go +++ b/playlist/playlist.go @@ -130,7 +130,7 @@ func (s *Store) Read(relPath string) (*Playlist, error) { continue } - //file path might be relative if using -playlists-relative + // file path might be relative if using -playlists-relative if !filepath.IsAbs(line) { line = filepath.Join(filepath.Dir(absPath), line) } @@ -188,7 +188,7 @@ func (s *Store) Write(relPath string, playlist *Playlist) error { fmt.Fprintln(file, encodeAttr(attrIsPublic, fmt.Sprint(playlist.IsPublic))) for _, line := range playlist.Items { if s.relative { - //transform to path relative to playlist's dir + // transform to path relative to playlist's dir relativePath, err := filepath.Rel(filepath.Dir(absPath), line) if err == nil { line = relativePath diff --git a/playlist/playlist_test.go b/playlist/playlist_test.go index 10b62344..d06144b2 100644 --- a/playlist/playlist_test.go +++ b/playlist/playlist_test.go @@ -11,7 +11,7 @@ func TestPlaylist(t *testing.T) { t.Parallel() tmp := t.TempDir() - store, err := playlist.NewStore(tmp) + store, err := playlist.NewStore(tmp, false) require.NoError(t, err) playlistIDs, err := store.List() From 505f1c58a243ec50395e9fe296468e404240d3c7 Mon Sep 17 00:00:00 2001 From: 0chroma <21014+0chroma@users.noreply.github.com> Date: Sat, 7 Sep 2024 05:47:04 -0700 Subject: [PATCH 6/6] fix test --- playlist/playlist_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/playlist/playlist_test.go b/playlist/playlist_test.go index d06144b2..e0b6cb53 100644 --- a/playlist/playlist_test.go +++ b/playlist/playlist_test.go @@ -32,9 +32,9 @@ Example comment It has multiple lines 👍 `, Items: []string{ - "item 1.flac", - "item 2.flac", - "item 3.flac", + "/item 1.flac", + "/item 2.flac", + "/item 3.flac", }, IsPublic: true, }