diff --git a/README.md b/README.md index 9339570..5ce1c8d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr - **Libraries.** A library is a name and a folder, managed through the API. Sonda only reads your music folders: it never writes or renames a file, and it deletes one only when an admin asks to remove the extra copy of a song and you turned on `library.allow_delete` (see **Duplicates**), which is off by default. - **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. -- **Duplicates.** `GET /library/duplicates` (admin) lists the songs that are in more than one file: the same set of main artists, the same title and durations within 3 seconds, possibly on different albums. It is worked out from the index, touches no file, and returns no file path. Each group proposes the copy to keep (lossless first, then the higher bitrate, then the bigger file, then the oldest). A background job (`POST /library/duplicates/check`, like the other bulk jobs) reads the files of the groups and keeps their SHA-256, so a group can be called `identical` (byte for byte). `POST /library/duplicates/clean` **deletes the extra copies of one song from the disk**, and it is the one thing Sonda does that writes in your music folders, so it is off until you set `library.allow_delete` (or `SONDA_ALLOW_DELETE=true`) and mount the folders writable. It deletes only when an admin asks, never by itself, and the server checks again at that moment that no scan is running, that the tracks are copies of one song, and that every file is still the one the last scan saw (same size and date, a regular file, not a link, inside its library): so the last copy of a song is never deleted, a file that changed is not deleted, and every file is checked before the first is deleted. The listens of a deleted copy move to the one that stays, so your taste is not lost. Folders are never removed, and nothing is sent anywhere. +- **Duplicates.** `GET /library/duplicates` (admin) lists the songs that are in more than one file: the same set of main artists, the same title and durations within 3 seconds, possibly on different albums. It is worked out from the index, touches no file, and returns no file path. Each group proposes the copy to keep (lossless first, then the higher bitrate, then the bigger file, then the oldest). A background job (`POST /library/duplicates/check`, like the other bulk jobs) reads the files of the groups and keeps their SHA-256, so a group can be called `identical` (byte for byte). `POST /library/duplicates/clean` **deletes the extra copies of one song from the disk**, and it is the one thing Sonda does that writes in your music folders, so it is off until you set `library.allow_delete` (or `SONDA_ALLOW_DELETE=true`) and mount the folders writable. It deletes only when an admin asks, never by itself, and the server checks again at that moment that no scan is running, that the tracks are copies of one song, and that every file is still the one the last scan saw (same size and date, a regular file, not a link, inside its library): so the last copy of a song is never deleted, a file that changed is not deleted, and every file is checked before the first is deleted. The listens and the playlist items of a deleted copy move to the one that stays, so your taste and your playlists are not lost. Folders are never removed, and nothing is sent anywhere. - **Streaming.** `GET` and `HEAD` with `Range` support, so players can seek. Opening or prefetching a stream does not record a listen. - **API documentation.** The full API is described in OpenAPI and browsable with Swagger UI, served by Sonda itself. The UI is embedded in the binary, so opening it contacts no other server. - **Listening history.** Clients report explicit `play`, `complete` and `skip` events. An event counts as a listen when it is `complete`, or when it was played for at least 20 seconds (`listening.min_listen_seconds`); the rule is applied when the history is read, so changing it re-reads the past. You can read your own history, with a `counted` flag on every event. It stays in your database and is never sent anywhere. diff --git a/internal/duplicates/service.go b/internal/duplicates/service.go index 1a9e7bb..e203bda 100644 --- a/internal/duplicates/service.go +++ b/internal/duplicates/service.go @@ -91,7 +91,7 @@ type Result struct { // Build, the copy to keep is still the file that was scanned (so it is never the last copy that is // removed), and so is every copy to remove (a file that changed, a link, or a path that leaves the // library is refused). The files are checked all together before the first is removed. Each removed -// file takes its track out of the index, and its listens go to the track that stays. +// file takes its track out of the index, and its listens and playlist items go to the track that stays. // // On an error part way (the folder turned out to be read-only on the second file), the Result says // what was already removed. diff --git a/internal/storage/duplicates.go b/internal/storage/duplicates.go index 93965ec..9df531e 100644 --- a/internal/storage/duplicates.go +++ b/internal/storage/duplicates.go @@ -176,10 +176,10 @@ func (r *DuplicatesRepo) SaveHash(ctx context.Context, fileID, sizeBytes, mtimeU return n > 0, nil } -// RemoveCopy takes a track out of the index in favour of another that stays: the listens of the -// removed track become listens of the one that stays (they would be deleted with it otherwise), and -// the track goes, with its file, credits and genres. The covers of the albums involved are built -// again. It is one transaction. +// RemoveCopy takes a track out of the index in favour of another that stays: the listens and the +// playlist items of the removed track move to the one that stays (they would be deleted with it +// otherwise), and the track goes, with its file, credits and genres. The covers of the albums +// involved are built again. It is one transaction. func (r *DuplicatesRepo) RemoveCopy(ctx context.Context, keepTrackID, removeTrackID int64) error { if keepTrackID == removeTrackID { return duplicates.ErrInvalid @@ -200,6 +200,11 @@ func (r *DuplicatesRepo) RemoveCopy(ctx context.Context, keepTrackID, removeTrac if _, err := tx.ExecContext(ctx, "UPDATE listening_events SET track_id = ? WHERE track_id = ?", keepTrackID, removeTrackID); err != nil { return fmt.Errorf("move the listens: %w", err) } + // Playlist items go the same way: an item has its own id and position, so the song may appear + // twice in a playlist and nothing shifts. + if _, err := tx.ExecContext(ctx, "UPDATE playlist_items SET track_id = ? WHERE track_id = ?", keepTrackID, removeTrackID); err != nil { + return fmt.Errorf("move the playlist items: %w", err) + } if _, err := tx.ExecContext(ctx, "DELETE FROM tracks WHERE id = ?", removeTrackID); err != nil { return fmt.Errorf("remove the track: %w", err) } diff --git a/internal/storage/duplicates_test.go b/internal/storage/duplicates_test.go index d303d33..bddb187 100644 --- a/internal/storage/duplicates_test.go +++ b/internal/storage/duplicates_test.go @@ -3,6 +3,7 @@ package storage import ( "context" "errors" + "slices" "testing" "time" @@ -245,6 +246,48 @@ func TestRemovingACopyMovesItsListensToTheOneThatStays(t *testing.T) { }) } +func TestRemovingACopyKeepsItsPlaceInThePlaylists(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + lib := NewLibraryRepo(db) + l := mkLib(t, lib, "music") + saved(t, lib, l, song("a/1.flac", "Song", "A", 200_000, 1, "flac", 900, "Ann")) + saved(t, lib, l, song("b/2.mp3", "Song", "B", 200_000, 1, "mp3", 192, "Ann")) + saved(t, lib, l, song("c/3.mp3", "Other", "C", 100_000, 1, "mp3", 192, "Ann")) + keep, gone, other := trackOf(t, db, "a/1.flac"), trackOf(t, db, "b/2.mp3"), trackOf(t, db, "c/3.mp3") + + user, err := NewUserRepo(db).CreateUser(ctx, "alice@example.org", "h", false) + if err != nil { + t.Fatal(err) + } + pl := NewPlaylistRepo(db) + now := time.Unix(1_700_000_000, 0) + p, err := pl.Create(ctx, user.ID, "Mix", "", now) + if err != nil { + t.Fatal(err) + } + // The song twice in a row (the removed copy between two others), then another song. + if _, err := pl.AddTracks(ctx, user.ID, p.ID, []int64{keep, gone, other}, nil, now); err != nil { + t.Fatal(err) + } + + if err := NewDuplicatesRepo(db).RemoveCopy(ctx, keep, gone); err != nil { + t.Fatal(err) + } + + items, total, err := pl.Items(ctx, user.ID, p.ID, 50, 0) + if err != nil { + t.Fatal(err) + } + if total != 3 || len(items) != 3 { + t.Fatalf("the playlist has %d items, want 3: the removed copy's place must stay", total) + } + if got := []int64{items[0].Track.ID, items[1].Track.ID, items[2].Track.ID}; !slices.Equal(got, []int64{keep, keep, other}) { + t.Errorf("tracks in the playlist = %v, want %v", got, []int64{keep, keep, other}) + } + }) +} + func TestRemovingACopyNeedsTwoTracksThatExistAndAreDifferent(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background()