Skip to content

Commit 798e7f8

Browse files
committed
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
1 parent 65c4674 commit 798e7f8

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

playlist/playlist.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func (s *Store) Read(relPath string) (*Playlist, error) {
134134
if strings.HasPrefix(line, "#") {
135135
continue
136136
}
137+
if !filepath.IsAbs(line) {
138+
line = filepath.Join(filepath.Dir(absPath), line)
139+
}
137140
playlist.Items = append(playlist.Items, line)
138141
}
139142

@@ -186,6 +189,9 @@ func (s *Store) Write(relPath string, playlist *Playlist) error {
186189
fmt.Fprintln(file, encodeAttr(attrCommment, playlist.Comment))
187190
fmt.Fprintln(file, encodeAttr(attrIsPublic, fmt.Sprint(playlist.IsPublic)))
188191
for _, line := range playlist.Items {
192+
if rel, err := filepath.Rel(filepath.Dir(absPath), line); err == nil {
193+
line = rel
194+
}
189195
fmt.Fprintln(file, line)
190196
}
191197

playlist/playlist_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ Example comment
3232
It has multiple lines 👍
3333
`,
3434
Items: []string{
35-
"item 1.flac",
36-
"item 2.flac",
37-
"item 3.flac",
35+
"/music/item 1.flac",
36+
"/music/item 2.flac",
37+
"/music/item 3.flac",
3838
},
3939
IsPublic: true,
4040
}

0 commit comments

Comments
 (0)