feat(playlist): use relative paths in m3u playlists

write playlist item paths relative to the playlist file directory, an resolve relative paths back to absolute on read

closes #537
closes #529
This commit is contained in:
sentriz
2026-03-09 15:26:21 +00:00
parent 65c4674da0
commit 798e7f875a
2 changed files with 9 additions and 3 deletions

No files matched your search

+6
View File
@@ -134,6 +134,9 @@ func (s *Store) Read(relPath string) (*Playlist, error) {
if strings.HasPrefix(line, "#") { if strings.HasPrefix(line, "#") {
continue continue
} }
if !filepath.IsAbs(line) {
line = filepath.Join(filepath.Dir(absPath), line)
}
playlist.Items = append(playlist.Items, line) playlist.Items = append(playlist.Items, line)
} }
@@ -186,6 +189,9 @@ func (s *Store) Write(relPath string, playlist *Playlist) error {
fmt.Fprintln(file, encodeAttr(attrCommment, playlist.Comment)) fmt.Fprintln(file, encodeAttr(attrCommment, playlist.Comment))
fmt.Fprintln(file, encodeAttr(attrIsPublic, fmt.Sprint(playlist.IsPublic))) fmt.Fprintln(file, encodeAttr(attrIsPublic, fmt.Sprint(playlist.IsPublic)))
for _, line := range playlist.Items { for _, line := range playlist.Items {
if rel, err := filepath.Rel(filepath.Dir(absPath), line); err == nil {
line = rel
}
fmt.Fprintln(file, line) fmt.Fprintln(file, line)
} }
+3 -3
View File
@@ -32,9 +32,9 @@ Example comment
It has multiple lines 👍 It has multiple lines 👍
`, `,
Items: []string{ Items: []string{
"item 1.flac", "/music/item 1.flac",
"item 2.flac", "/music/item 2.flac",
"item 3.flac", "/music/item 3.flac",
}, },
IsPublic: true, IsPublic: true,
} }