Support m3u playlist files to define chapter order
The chapter order on the tonie is already fully API-controlled via SortChaptersOfTonie/PATCH. This adds an explicit way to define that order via an .m3u/.m3u8 file placed in the tonie folder, instead of relying on alphabetical filenames: - internal/syncer/playlist.go: finds a single playlist file in the tonie folder, parses its entries (ignoring blanks/#-comments), and reorders local tracks accordingly. Tracks not referenced by the playlist are appended afterwards so nothing is silently dropped; playlist entries without a matching local file are ignored. Multiple playlist files in the same folder is an error (ambiguous order). - ListLocalTracks now prefers this playlist-defined order, falling back to alphabetical filename order when no playlist is present. - Tests covering playlist ordering, unreferenced-track handling, missing entries, and the multiple-playlists error case. - README documents the .m3u/.m3u8 convention. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -56,7 +56,12 @@ func TitleFromFilename(name string) string {
|
||||
return title
|
||||
}
|
||||
|
||||
// ListLocalTracks returns audio files in folder, sorted by filename (defines chapter order).
|
||||
// ListLocalTracks returns the audio files in folder that should be synced,
|
||||
// in chapter order.
|
||||
//
|
||||
// If the folder contains a single .m3u/.m3u8 playlist file, its entry order
|
||||
// defines the chapter order (see orderTracksByPlaylist); otherwise tracks
|
||||
// are sorted alphabetically by filename.
|
||||
func ListLocalTracks(folder string) ([]Track, error) {
|
||||
entries, err := os.ReadDir(folder)
|
||||
if err != nil {
|
||||
@@ -78,6 +83,18 @@ func ListLocalTracks(folder string) ([]Track, error) {
|
||||
})
|
||||
}
|
||||
sort.Slice(tracks, func(i, j int) bool { return tracks[i].Path < tracks[j].Path })
|
||||
|
||||
playlistPath, err := findPlaylistFile(folder, entries)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if playlistPath != "" {
|
||||
tracks, err = orderTracksByPlaylist(folder, playlistPath, tracks)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return tracks, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user