first commit
This commit is contained in:
33 files changed
+5235
No files matched your search
@@ -0,0 +1,165 @@
|
||||
// Package m3u writes extended m3u playlists in replace or append mode.
|
||||
package m3u
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"applesync/internal/model"
|
||||
"applesync/internal/state"
|
||||
)
|
||||
|
||||
// Entry is one playlist track mapped to a downloaded file.
|
||||
type Entry struct {
|
||||
File *model.FileEntry
|
||||
Track *model.Track
|
||||
}
|
||||
|
||||
// Write writes entries to path. In replace mode the file is rebuilt; in append
|
||||
// mode existing path lines are kept and only new files are appended. Paths are
|
||||
// rendered relative to the m3u file or as absolute paths.
|
||||
func Write(path string, entries []Entry, mode, pathStyle string, extended bool) (int, error) {
|
||||
mode = strings.ToLower(strings.TrimSpace(mode))
|
||||
if mode == "" {
|
||||
mode = "replace"
|
||||
}
|
||||
pathStyle = strings.ToLower(strings.TrimSpace(pathStyle))
|
||||
if pathStyle == "" {
|
||||
pathStyle = "relative"
|
||||
}
|
||||
|
||||
switch mode {
|
||||
case "replace":
|
||||
var b strings.Builder
|
||||
b.WriteString("#EXTM3U\n")
|
||||
written := 0
|
||||
for _, entry := range entries {
|
||||
line, err := renderPath(path, entry.File.Path, pathStyle)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if extended {
|
||||
b.WriteString(extinf(entry.Track))
|
||||
}
|
||||
b.WriteString(line)
|
||||
b.WriteString("\n")
|
||||
written++
|
||||
}
|
||||
if err := state.WriteFileAtomic(path, []byte(b.String())); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
_ = os.Chmod(path, 0o644)
|
||||
return written, nil
|
||||
|
||||
case "append":
|
||||
existing, err := os.ReadFile(path)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return 0, err
|
||||
}
|
||||
existingSet := existingPaths(string(existing), filepath.Dir(path))
|
||||
var b strings.Builder
|
||||
b.Write(existing)
|
||||
if len(existing) > 0 && !strings.HasSuffix(string(existing), "\n") {
|
||||
b.WriteString("\n")
|
||||
}
|
||||
if len(existing) == 0 {
|
||||
b.WriteString("#EXTM3U\n")
|
||||
}
|
||||
written := 0
|
||||
for _, entry := range entries {
|
||||
identity := identityPath(entry.File.Path, filepath.Dir(path))
|
||||
if existingSet[identity] {
|
||||
continue
|
||||
}
|
||||
line, err := renderPath(path, entry.File.Path, pathStyle)
|
||||
if err != nil {
|
||||
return written, err
|
||||
}
|
||||
if extended {
|
||||
b.WriteString(extinf(entry.Track))
|
||||
}
|
||||
b.WriteString(line)
|
||||
b.WriteString("\n")
|
||||
existingSet[identity] = true
|
||||
written++
|
||||
}
|
||||
if written == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
if err := state.WriteFileAtomic(path, []byte(b.String())); err != nil {
|
||||
return written, err
|
||||
}
|
||||
_ = os.Chmod(path, 0o644)
|
||||
return written, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("unknown m3u mode %q", mode)
|
||||
}
|
||||
}
|
||||
|
||||
func extinf(track *model.Track) string {
|
||||
if track == nil {
|
||||
return "#EXTINF:-1,\n"
|
||||
}
|
||||
seconds := track.DurationMS / 1000
|
||||
if seconds < 0 {
|
||||
seconds = 0
|
||||
}
|
||||
label := strings.TrimSpace(track.ArtistName + " - " + track.Title)
|
||||
if label == "" {
|
||||
label = track.Title
|
||||
}
|
||||
return fmt.Sprintf("#EXTINF:%d,%s\n", seconds, sanitize(label))
|
||||
}
|
||||
|
||||
func sanitize(value string) string {
|
||||
value = strings.ReplaceAll(value, "\r", " ")
|
||||
value = strings.ReplaceAll(value, "\n", " ")
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
func renderPath(m3uPath, filePath, style string) (string, error) {
|
||||
abs, err := filepath.Abs(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if style == "absolute" {
|
||||
return filepath.ToSlash(abs), nil
|
||||
}
|
||||
dir, err := filepath.Abs(filepath.Dir(m3uPath))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rel, err := filepath.Rel(dir, abs)
|
||||
if err != nil {
|
||||
return filepath.ToSlash(abs), nil
|
||||
}
|
||||
return filepath.ToSlash(rel), nil
|
||||
}
|
||||
|
||||
func identityPath(filePath, m3uDir string) string {
|
||||
if strings.Contains(filePath, "://") {
|
||||
return filePath
|
||||
}
|
||||
if !filepath.IsAbs(filePath) {
|
||||
filePath = filepath.Join(m3uDir, filePath)
|
||||
}
|
||||
abs, err := filepath.Abs(filePath)
|
||||
if err != nil {
|
||||
return filepath.Clean(filePath)
|
||||
}
|
||||
return filepath.Clean(abs)
|
||||
}
|
||||
|
||||
func existingPaths(content, m3uDir string) map[string]bool {
|
||||
out := map[string]bool{}
|
||||
for _, line := range strings.Split(content, "\n") {
|
||||
line = strings.TrimSpace(strings.TrimSuffix(line, "\r"))
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
out[identityPath(line, m3uDir)] = true
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package m3u
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"applesync/internal/model"
|
||||
)
|
||||
|
||||
func entry(path, artist, title string, ms int) Entry {
|
||||
return Entry{
|
||||
File: &model.FileEntry{Path: path},
|
||||
Track: &model.Track{ArtistName: artist, Title: title, DurationMS: ms},
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplaceRelative(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
music := filepath.Join(root, "music", "Album")
|
||||
if err := os.MkdirAll(music, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
file := filepath.Join(music, "01. Artist - One.flac")
|
||||
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m3uPath := filepath.Join(root, "playlists", "list.m3u")
|
||||
if _, err := Write(m3uPath, []Entry{entry(file, "Artist", "One", 215000)}, "replace", "relative", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := os.ReadFile(m3uPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "#EXTM3U\n#EXTINF:215,Artist - One\n../music/Album/01. Artist - One.flac\n"
|
||||
if string(data) != want {
|
||||
t.Fatalf("got:\n%s\nwant:\n%s", data, want)
|
||||
}
|
||||
info, err := os.Stat(m3uPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Mode().Perm() != 0o644 {
|
||||
t.Fatalf("mode = %v, want 0644", info.Mode().Perm())
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplaceAbsolute(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
file := filepath.Join(root, "a.flac")
|
||||
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m3uPath := filepath.Join(root, "list.m3u")
|
||||
if _, err := Write(m3uPath, []Entry{entry(file, "A", "B", 1000)}, "replace", "absolute", false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, _ := os.ReadFile(m3uPath)
|
||||
if !strings.Contains(string(data), "#EXTM3U\n") || !strings.Contains(string(data), file) {
|
||||
t.Fatalf("unexpected m3u:\n%s", data)
|
||||
}
|
||||
if strings.Contains(string(data), "#EXTINF") {
|
||||
t.Fatalf("plain m3u must not contain EXTINF:\n%s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendDeduplicates(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
one := filepath.Join(root, "one.flac")
|
||||
two := filepath.Join(root, "two.flac")
|
||||
_ = os.WriteFile(one, []byte("1"), 0o644)
|
||||
_ = os.WriteFile(two, []byte("2"), 0o644)
|
||||
m3uPath := filepath.Join(root, "list.m3u")
|
||||
|
||||
if _, err := Write(m3uPath, []Entry{entry(one, "A", "One", 1000)}, "replace", "relative", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
written, err := Write(m3uPath, []Entry{
|
||||
entry(one, "A", "One", 1000),
|
||||
entry(two, "B", "Two", 2000),
|
||||
}, "append", "relative", true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if written != 1 {
|
||||
t.Fatalf("written = %d, want 1", written)
|
||||
}
|
||||
data, _ := os.ReadFile(m3uPath)
|
||||
if strings.Count(string(data), "one.flac") != 1 {
|
||||
t.Fatalf("duplicate one.flac:\n%s", data)
|
||||
}
|
||||
if !strings.Contains(string(data), "two.flac") {
|
||||
t.Fatalf("two.flac missing:\n%s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendMissingFile(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
file := filepath.Join(root, "one.flac")
|
||||
_ = os.WriteFile(file, []byte("1"), 0o644)
|
||||
m3uPath := filepath.Join(root, "new.m3u")
|
||||
if _, err := Write(m3uPath, []Entry{entry(file, "A", "One", 1000)}, "append", "relative", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, _ := os.ReadFile(m3uPath)
|
||||
if !strings.HasPrefix(string(data), "#EXTM3U\n") {
|
||||
t.Fatalf("missing header:\n%s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendDedupesExistingPlainPaths(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
file := filepath.Join(root, "one.flac")
|
||||
_ = os.WriteFile(file, []byte("1"), 0o644)
|
||||
m3uPath := filepath.Join(root, "list.m3u")
|
||||
if err := os.WriteFile(m3uPath, []byte("one.flac\r\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
written, err := Write(m3uPath, []Entry{entry(file, "A", "One", 1000)}, "append", "relative", true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if written != 0 {
|
||||
t.Fatalf("written = %d, want 0", written)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user