- feat(storage): libraries table (name, path); audio_files points at a library
by id instead of repeating the root path; migration 0005 for both drivers
turns every existing root into a library and keeps file ids, tracks and the
missing flag
- feat(library): Libraries service validates names and paths: absolute, an
existing directory, never the filesystem root, never inside or around
another library (checked on real paths, so symlinks cannot hide an overlap)
- feat(api): admin-only GET/POST /libraries, PATCH/DELETE /libraries/{id} and
POST /libraries/{id}/scan; deleting a library drops its index entries and
keeps tracks and listening history
- feat(library): the scanner and the scan manager work on libraries; scanning
with no libraries answers 409 no_libraries
- feat(streaming): the library directory comes from the database instead of
a configured list of roots
- breaking(config): library.roots is removed; an old config fails with a hint
to use POST /api/v1/libraries
- test: CRUD and duplicates on both databases, path change keeps files,
upgrade of a database written before libraries existed
272 lines
7.7 KiB
Go
272 lines
7.7 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// memLibraries is an in-memory LibraryStore that enforces unique names and paths.
|
|
type memLibraries struct {
|
|
mu sync.Mutex
|
|
next int64
|
|
libs []Library
|
|
}
|
|
|
|
func (m *memLibraries) ListLibraries(context.Context) ([]Library, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return append([]Library(nil), m.libs...), nil
|
|
}
|
|
|
|
func (m *memLibraries) GetLibrary(_ context.Context, id int64) (Library, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
for _, l := range m.libs {
|
|
if l.ID == id {
|
|
return l, nil
|
|
}
|
|
}
|
|
return Library{}, ErrLibraryNotFound
|
|
}
|
|
|
|
func (m *memLibraries) clash(name, path string, skip int64) bool {
|
|
for _, l := range m.libs {
|
|
if l.ID != skip && (strings.EqualFold(l.Name, name) || l.Path == path) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (m *memLibraries) CreateLibrary(_ context.Context, name, path string) (Library, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.clash(name, path, 0) {
|
|
return Library{}, ErrLibraryExists
|
|
}
|
|
m.next++
|
|
l := Library{ID: m.next, Name: name, Path: path, CreatedAt: time.Now()}
|
|
m.libs = append(m.libs, l)
|
|
return l, nil
|
|
}
|
|
|
|
func (m *memLibraries) UpdateLibrary(_ context.Context, id int64, name, path string) (Library, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
for i, l := range m.libs {
|
|
if l.ID == id {
|
|
if m.clash(name, path, id) {
|
|
return Library{}, ErrLibraryExists
|
|
}
|
|
m.libs[i].Name, m.libs[i].Path = name, path
|
|
return m.libs[i], nil
|
|
}
|
|
}
|
|
return Library{}, ErrLibraryNotFound
|
|
}
|
|
|
|
func (m *memLibraries) DeleteLibrary(_ context.Context, id int64) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
for i, l := range m.libs {
|
|
if l.ID == id {
|
|
m.libs = append(m.libs[:i], m.libs[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
return ErrLibraryNotFound
|
|
}
|
|
|
|
func newLibraries() (*Libraries, *memLibraries) {
|
|
st := &memLibraries{}
|
|
return NewLibraries(st), st
|
|
}
|
|
|
|
func mkdir(t *testing.T, path string) string {
|
|
t.Helper()
|
|
if err := os.MkdirAll(path, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestCreateLibrary(t *testing.T) {
|
|
s, _ := newLibraries()
|
|
dir := t.TempDir()
|
|
|
|
lib, err := s.Create(context.Background(), " Jazz ", dir+"/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if lib.Name != "Jazz" || lib.Path != dir || lib.ID == 0 {
|
|
t.Errorf("library = %+v, want trimmed name and cleaned path %q", lib, dir)
|
|
}
|
|
}
|
|
|
|
func TestCreateLibraryValidation(t *testing.T) {
|
|
dir := t.TempDir()
|
|
file := filepath.Join(dir, "file.txt")
|
|
if err := os.WriteFile(file, []byte("x"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tests := []struct {
|
|
name, libName, path string
|
|
}{
|
|
{"empty name", "", dir},
|
|
{"blank name", " ", dir},
|
|
{"long name", strings.Repeat("n", 101), dir},
|
|
{"control character in name", "bad\x00name", dir},
|
|
{"empty path", "x", ""},
|
|
{"relative path", "x", "music/jazz"},
|
|
{"filesystem root", "x", "/"},
|
|
{"path does not exist", "x", filepath.Join(dir, "nope")},
|
|
{"path is a file", "x", file},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s, _ := newLibraries()
|
|
if _, err := s.Create(context.Background(), tt.libName, tt.path); !errors.Is(err, ErrInvalidLibrary) {
|
|
t.Fatalf("err = %v, want ErrInvalidLibrary", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCreateLibraryDuplicates(t *testing.T) {
|
|
s, _ := newLibraries()
|
|
ctx := context.Background()
|
|
a, b := mkdir(t, filepath.Join(t.TempDir(), "a")), mkdir(t, filepath.Join(t.TempDir(), "b"))
|
|
|
|
if _, err := s.Create(ctx, "Music", a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Create(ctx, "MUSIC", b); !errors.Is(err, ErrLibraryExists) {
|
|
t.Errorf("same name, other case: err = %v", err)
|
|
}
|
|
if _, err := s.Create(ctx, "Other", a); !errors.Is(err, ErrLibraryExists) {
|
|
t.Errorf("same path: err = %v", err)
|
|
}
|
|
if _, err := s.Create(ctx, "Other", a+"/"); !errors.Is(err, ErrLibraryExists) {
|
|
t.Errorf("same path with a trailing slash: err = %v", err)
|
|
}
|
|
}
|
|
|
|
// Nested libraries would index the same files twice.
|
|
func TestCreateLibraryRejectsOverlap(t *testing.T) {
|
|
s, _ := newLibraries()
|
|
ctx := context.Background()
|
|
base := t.TempDir()
|
|
parent := mkdir(t, filepath.Join(base, "music"))
|
|
child := mkdir(t, filepath.Join(parent, "jazz"))
|
|
sibling := mkdir(t, filepath.Join(base, "music2")) // shares a string prefix, not a directory
|
|
|
|
if _, err := s.Create(ctx, "Music", parent); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Create(ctx, "Jazz", child); !errors.Is(err, ErrInvalidLibrary) {
|
|
t.Errorf("inside an existing library: err = %v", err)
|
|
}
|
|
if _, err := s.Create(ctx, "Sibling", sibling); err != nil {
|
|
t.Errorf("a sibling with a common prefix must be allowed: %v", err)
|
|
}
|
|
if _, err := s.Create(ctx, "Everything", base); !errors.Is(err, ErrInvalidLibrary) {
|
|
t.Errorf("around existing libraries: err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCreateLibraryRejectsOverlapThroughSymlinks(t *testing.T) {
|
|
s, _ := newLibraries()
|
|
ctx := context.Background()
|
|
base := t.TempDir()
|
|
parent := mkdir(t, filepath.Join(base, "music"))
|
|
child := mkdir(t, filepath.Join(parent, "jazz"))
|
|
link := filepath.Join(base, "shortcut")
|
|
if err := os.Symlink(child, link); err != nil {
|
|
t.Skip("symlinks not supported:", err)
|
|
}
|
|
|
|
if _, err := s.Create(ctx, "Music", parent); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Create(ctx, "Shortcut", link); !errors.Is(err, ErrInvalidLibrary) {
|
|
t.Errorf("a symlink into an existing library: err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUpdateLibrary(t *testing.T) {
|
|
s, _ := newLibraries()
|
|
ctx := context.Background()
|
|
base := t.TempDir()
|
|
a, b := mkdir(t, filepath.Join(base, "a")), mkdir(t, filepath.Join(base, "b"))
|
|
inside := mkdir(t, filepath.Join(a, "inside"))
|
|
|
|
first, _ := s.Create(ctx, "First", a)
|
|
second, _ := s.Create(ctx, "Second", b)
|
|
str := func(v string) *string { return &v }
|
|
|
|
// Rename only.
|
|
got, err := s.Update(ctx, first.ID, str("Renamed"), nil)
|
|
if err != nil || got.Name != "Renamed" || got.Path != a {
|
|
t.Fatalf("rename: %+v err=%v", got, err)
|
|
}
|
|
// Moving to its own current path is not an overlap with itself.
|
|
if _, err := s.Update(ctx, first.ID, nil, str(a)); err != nil {
|
|
t.Errorf("same path: %v", err)
|
|
}
|
|
// A new path in an unrelated place.
|
|
c := mkdir(t, filepath.Join(base, "c"))
|
|
if got, err = s.Update(ctx, first.ID, nil, str(c)); err != nil || got.Path != c {
|
|
t.Fatalf("move: %+v err=%v", got, err)
|
|
}
|
|
// Into another library, or onto its name or path.
|
|
if _, err := s.Update(ctx, first.ID, nil, str(b)); !errors.Is(err, ErrLibraryExists) {
|
|
t.Errorf("onto another library's path: err = %v", err)
|
|
}
|
|
if _, err := s.Update(ctx, second.ID, str("renamed"), nil); !errors.Is(err, ErrLibraryExists) {
|
|
t.Errorf("onto another library's name: err = %v", err)
|
|
}
|
|
if _, err := s.Update(ctx, first.ID, nil, str(filepath.Join(b, "deeper"))); !errors.Is(err, ErrInvalidLibrary) {
|
|
t.Errorf("a missing directory: err = %v", err)
|
|
}
|
|
_ = inside
|
|
if _, err := s.Update(ctx, first.ID, str(""), nil); !errors.Is(err, ErrInvalidLibrary) {
|
|
t.Errorf("empty name: err = %v", err)
|
|
}
|
|
if _, err := s.Update(ctx, 999, str("x"), nil); !errors.Is(err, ErrLibraryNotFound) {
|
|
t.Errorf("unknown id: err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteLibrary(t *testing.T) {
|
|
s, st := newLibraries()
|
|
ctx := context.Background()
|
|
lib, _ := s.Create(ctx, "Music", t.TempDir())
|
|
|
|
if err := s.Delete(ctx, lib.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(st.libs) != 0 {
|
|
t.Error("library not deleted")
|
|
}
|
|
if err := s.Delete(ctx, lib.ID); !errors.Is(err, ErrLibraryNotFound) {
|
|
t.Errorf("deleting twice: err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestListLibraries(t *testing.T) {
|
|
s, _ := newLibraries()
|
|
ctx := context.Background()
|
|
base := t.TempDir()
|
|
_, _ = s.Create(ctx, "A", mkdir(t, filepath.Join(base, "a")))
|
|
_, _ = s.Create(ctx, "B", mkdir(t, filepath.Join(base, "b")))
|
|
libs, err := s.List(ctx)
|
|
if err != nil || len(libs) != 2 {
|
|
t.Fatalf("libs = %+v err=%v", libs, err)
|
|
}
|
|
}
|