diff --git a/README.md b/README.md index 7b103ba..49cbebd 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr - **Accounts.** The first user to register becomes the admin and registration then closes; admins create the other users. Short-lived access tokens (JWT) plus rotating refresh tokens, so a stolen refresh token is detected. - **Libraries.** A library is a name and a folder, managed through the API. Sonda only ever reads your music folders: it never writes, renames or deletes a file. -- **Scanner.** Reads MP3 and FLAC tags and audio properties with several workers in parallel. Rescans are incremental, renamed files keep their identity, vanished files are flagged (never deleted), and a library whose folder looks empty or unreadable (an unmounted NAS) is left alone instead of being wiped. +- **Scanner.** Reads MP3 and FLAC tags and audio properties with several workers in parallel. Rescans are incremental, renamed files keep their identity, vanished files are flagged (never deleted), and a library whose folder looks empty or unreadable (an unmounted NAS) is left alone instead of being wiped. Audio in other formats (m4a, ogg, opus, wav, wma and similar) is not read yet, but it is counted as `unsupported` in the scan result instead of disappearing silently. NAS housekeeping folders (`@Recycle`, `#recycle`, `@eaDir`, `.@__thumb`, `$RECYCLE.BIN`) are skipped. - **Catalog.** Browse and search artists, albums and tracks, with paging, by library, artist or album. Only tracks you can actually play are listed. - **Streaming.** `GET` and `HEAD` with `Range` support, so players can seek. Opening or prefetching a stream does not record a listen. - **Listening history.** Clients report explicit `play`, `complete` and `skip` events. They stay in your database; nothing is sent anywhere. Sonda makes no outbound network requests at all. @@ -115,7 +115,7 @@ until [ "$(curl -s $BASE/library/scan -H "$AUTH" | jq .running)" = false ]; do s curl -s $BASE/library/scan -H "$AUTH" | jq .last ``` -`found`, `added`, `unchanged`, `renamed`, `missing` and `failed` tell you what the scan did. Run it again and everything is `unchanged`. +`found`, `added`, `unchanged`, `renamed`, `missing`, `failed` and `unsupported` tell you what the scan did. Run it again and everything is `unchanged`. **5. Browse and search:** diff --git a/internal/api/http/handlers_library.go b/internal/api/http/handlers_library.go index 704ae31..c2645d4 100644 --- a/internal/api/http/handlers_library.go +++ b/internal/api/http/handlers_library.go @@ -23,6 +23,8 @@ type summaryJSON struct { Missing int `json:"missing"` Failed int `json:"failed"` Skipped int `json:"skipped"` + // Unsupported counts audio files in formats Sonda cannot read yet. + Unsupported int `json:"unsupported"` } type scanStatusJSON struct { diff --git a/internal/api/http/library_test.go b/internal/api/http/library_test.go index f41d81d..647ae95 100644 --- a/internal/api/http/library_test.go +++ b/internal/api/http/library_test.go @@ -77,7 +77,7 @@ func TestScanStartAndConflict(t *testing.T) { func TestScanStatus(t *testing.T) { scan := &fakeScan{status: library.Status{ LastFinishedAt: time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC), - Last: &library.Summary{Libraries: 1, Found: 10, Added: 7, Failed: 1}, + Last: &library.Summary{Libraries: 1, Found: 10, Added: 7, Failed: 1, Unsupported: 4}, LastError: "boom", }} h := newTestAPIWithScan(t, scan) @@ -92,7 +92,7 @@ func TestScanStatus(t *testing.T) { t.Errorf("body = %v", r.Body) } last, _ := r.Body["last"].(map[string]any) - if last["found"] != float64(10) || last["added"] != float64(7) || last["failed"] != float64(1) { + if last["found"] != float64(10) || last["added"] != float64(7) || last["failed"] != float64(1) || last["unsupported"] != float64(4) { t.Errorf("last = %v", last) } } diff --git a/internal/library/scanner.go b/internal/library/scanner.go index 4a2b5a6..aa6be5e 100644 --- a/internal/library/scanner.go +++ b/internal/library/scanner.go @@ -12,8 +12,33 @@ import ( "time" ) +// audioExts are the formats Sonda reads. var audioExts = map[string]bool{".mp3": true, ".flac": true} +// unsupportedExts are audio formats Sonda does not read yet. They are counted, +// never opened. Video containers such as .mpg or .mp4 are deliberately absent. +var unsupportedExts = map[string]bool{ + ".m4a": true, ".m4b": true, ".aac": true, ".alac": true, ".ogg": true, ".oga": true, + ".opus": true, ".wav": true, ".wma": true, ".ape": true, ".wv": true, ".aif": true, + ".aiff": true, ".mpc": true, ".mka": true, ".dsf": true, ".dff": true, +} + +// systemDirs are housekeeping folders that NAS boxes and operating systems +// create inside shares: thumbnails, recycle bins, indexes. What they hold is +// never part of a library. Only the exact folder name matches, ignoring case. +var systemDirs = []string{ + ".@__thumb", "@Recycle", "#recycle", "@eaDir", "$RECYCLE.BIN", "System Volume Information", +} + +func isSystemDir(name string) bool { + for _, d := range systemDirs { + if strings.EqualFold(name, d) { + return true + } + } + return false +} + // Scanner indexes the audio files of a set of libraries. // // Per library it walks the tree (cheap, stat only), decides what changed, then @@ -257,7 +282,17 @@ func (s *Scanner) walk(ctx context.Context, lib Library, sum *Summary) (walkResu sum.Skipped++ // never follow symlinks: they could point outside the root return nil } - if d.IsDir() || !audioExts[strings.ToLower(filepath.Ext(rel))] { + if d.IsDir() { + if isSystemDir(d.Name()) { + return filepath.SkipDir + } + return nil + } + ext := strings.ToLower(filepath.Ext(rel)) + if !audioExts[ext] { + if unsupportedExts[ext] { + sum.Unsupported++ + } return nil } if !d.Type().IsRegular() { diff --git a/internal/library/scanner_test.go b/internal/library/scanner_test.go index 41ff0c6..9fe2976 100644 --- a/internal/library/scanner_test.go +++ b/internal/library/scanner_test.go @@ -793,3 +793,86 @@ func TestManagerScansOneLibrary(t *testing.T) { t.Errorf("status = %+v", st) } } + +func TestScanSkipsNASSystemFolders(t *testing.T) { + root := t.TempDir() + write(t, filepath.Join(root, "Album/real.mp3"), "1") + for _, dir := range []string{".@__thumb", "@Recycle", "#recycle", "@eaDir", "$RECYCLE.BIN", "System Volume Information", "@recycle"} { + write(t, filepath.Join(root, dir, "ghost.mp3"), "2") + } + write(t, filepath.Join(root, "Album/@eaDir/thumb.mp3"), "3") // nested, too + write(t, filepath.Join(root, "Album/.@__thumb/thumb.flac"), "4") + // Only the exact folder name is special. + write(t, filepath.Join(root, "My @Recycle stuff/kept.mp3"), "5") + write(t, filepath.Join(root, "recycle bin/kept2.mp3"), "6") + + store := newFakeStore() + s, _ := newScanner(store, &fakeReader{}, 2) + sum := mustScan(t, s, root) + + if sum.Found != 3 || sum.Added != 3 { + t.Fatalf("summary = %+v, want the 3 real files only", sum) + } + for _, rel := range []string{"Album/real.mp3", "My @Recycle stuff/kept.mp3", "recycle bin/kept2.mp3"} { + if store.row(root, rel) == nil { + t.Errorf("%s was not indexed", rel) + } + } + if store.row(root, "@Recycle/ghost.mp3") != nil || store.row(root, "Album/@eaDir/thumb.mp3") != nil { + t.Error("a file inside a system folder was indexed") + } +} + +// The check is about folders inside a library. A library may well live in a +// folder that carries one of these names. +func TestLibraryInsideASystemNamedFolderIsStillScanned(t *testing.T) { + root := filepath.Join(t.TempDir(), "@Recycle") + write(t, filepath.Join(root, "song.mp3"), "1") + s, _ := newScanner(newFakeStore(), &fakeReader{}, 1) + if sum := mustScan(t, s, root); sum.Found != 1 { + t.Fatalf("summary = %+v, want the library's own folder to be scanned", sum) + } +} + +// Files that were indexed before a folder became "system" are no longer part of +// the library: they are flagged missing like any other vanished file. +func TestFilesInASkippedFolderAreFlaggedMissing(t *testing.T) { + root := t.TempDir() + write(t, filepath.Join(root, "keep.mp3"), "1") + write(t, filepath.Join(root, "@Recycle/old.mp3"), "22") + + store := newFakeStore() + s, _ := newScanner(store, &fakeReader{}, 1) + if _, err := store.SaveFile(context.Background(), libOf(root).ID, + ParsedFile{FileInfo: FileInfo{RelPath: "@Recycle/old.mp3", Size: 2, MTimeUnix: 1}}, 0); err != nil { + t.Fatal(err) + } + + sum := mustScan(t, s, root) + if sum.Missing != 1 || !store.row(root, "@Recycle/old.mp3").Missing { + t.Fatalf("summary = %+v, want the old recycle-bin entry flagged missing", sum) + } +} + +func TestScanCountsUnsupportedAudioFormats(t *testing.T) { + root := t.TempDir() + write(t, filepath.Join(root, "a.mp3"), "1") + for _, name := range []string{"b.m4a", "c.OGG", "d.wav", "e.opus", "sub/f.wma", "g.Aiff"} { + write(t, filepath.Join(root, name), "x") + } + for _, name := range []string{"notes.txt", "cover.jpg", "album.nfo", "Thumbs.db", "clip.mpg"} { + write(t, filepath.Join(root, name), "x") // not audio, or not audio Sonda knows + } + + reader := &fakeReader{} + store := newFakeStore() + s, _ := newScanner(store, reader, 1) + sum := mustScan(t, s, root) + + if sum.Found != 1 || sum.Unsupported != 6 { + t.Fatalf("summary = %+v, want 1 supported and 6 unsupported", sum) + } + if reader.calls.Load() != 1 { + t.Errorf("reader called %d times: unsupported files must not be opened", reader.calls.Load()) + } +} diff --git a/internal/library/types.go b/internal/library/types.go index 9aa4439..9fc50df 100644 --- a/internal/library/types.go +++ b/internal/library/types.go @@ -113,7 +113,10 @@ type Summary struct { Restored int Missing int // newly marked missing Failed int - Skipped int // symlinks + Skipped int // symlinks and special files + // Unsupported counts audio files in formats Sonda cannot read yet (m4a, ogg, + // wav, ...), so they are not left out without a trace. + Unsupported int } func (s *Summary) add(o Summary) { @@ -126,4 +129,5 @@ func (s *Summary) add(o Summary) { s.Missing += o.Missing s.Failed += o.Failed s.Skipped += o.Skipped + s.Unsupported += o.Unsupported }