Files
minstrel/internal/library/remove.go
T
bvandeusenandClaude Opus 5.5 9462052625
release / go (push) Successful in 2m6s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m46s
release / android (push) Successful in 4m47s
release / Build signed APK (push) Successful in 5m38s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 31s
release / Verify release artifacts (tag releases only) (push) Skipped
feat(library): removing a bad file keeps its track, waiting for a good copy
Reports, Duplicates and Suspect sources now end the same way: the file
goes and the track stays, marked missing with its likes, plays and
playlist places, until a good copy lands in the library and move
adoption or missing-pair folding brings it back.

- library.RemoveFile removes one track's file and marks the track
  missing with the queue it was removed from and who removed it
  (tracks.removed_reason / removed_by, migration 0104). The duplicate
  resolver's wrong-slot removal shares the same marking.
- POST /api/admin/quarantine/{id}/remove-file removes a reported file,
  clears the listeners' reports, marks open playback errors deleted and
  records removed_file in the Reports history. A wrong-file or
  wrong-tags report drops the file's recording MBID.
- POST /api/admin/library/suspect-sources/{id}/remove removes a video
  rip's file and its MBID, and writes a suspect_file_remove audit row.
- The removal reason clears with the missing mark when a file returns.

Grimoire #7743 (milestone 635).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 18:17:26 -05:00

105 lines
3.5 KiB
Go

package library
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// RemovedFor is the Collection queue a track's file was removed from. Every
// queue ends the same way: the bad file goes and the track waits, missing,
// for a good copy (tracks.removed_reason).
type RemovedFor string
const (
RemovedForReport RemovedFor = "report"
RemovedForDuplicate RemovedFor = "duplicate"
RemovedForSuspect RemovedFor = "suspect"
)
// RemoveFileParams is one track whose file an admin is removing.
type RemoveFileParams struct {
TrackID pgtype.UUID
// ActorID is the admin removing it.
ActorID pgtype.UUID
Reason RemovedFor
// ForgetMBID drops the track's recording MBID. Set it when the file was
// not the track's song, or its tags are not to be trusted: the MBID came
// from that file, and a good copy carrying the real song's MBID must not
// be kept from folding in by it, nor the slot folded into another song.
ForgetMBID bool
// Within runs on the same transaction as the marking, for what the
// caller's queue records about the removal (a report's history row). An
// error from it undoes the marking, though not the removed file.
Within func(ctx context.Context, tq *dbq.Queries) error
}
// RemoveFile removes a track's file from disk and keeps the track: it is
// marked missing, with its likes, plays and playlist places, and the queue it
// was removed from. A good copy that lands in the library later is adopted
// into it (moved.go) or folded into it (missing_pairs.go), so the track comes
// back with its history.
//
// The file goes first. If it cannot, nothing is marked and a
// *FileRemoveError comes back. A file that is already gone is not a failure.
func RemoveFile(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, p RemoveFileParams) (dbq.Track, error) {
if logger == nil {
logger = slog.Default()
}
q := dbq.New(pool)
track, err := q.GetTrackByID(ctx, p.TrackID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return dbq.Track{}, ErrTrackNotFound
}
return dbq.Track{}, fmt.Errorf("get track: %w", err)
}
if err := removeTrackFileOnDisk(track.FilePath); err != nil {
return dbq.Track{}, err
}
err = pgx.BeginFunc(ctx, pool, func(tx pgx.Tx) error {
tq := dbq.New(tx)
ids := []pgtype.UUID{p.TrackID}
if err := markRemoved(ctx, tq, ids, p.Reason, p.ActorID); err != nil {
return err
}
if p.ForgetMBID {
if err := tq.ClearTrackMbids(ctx, ids); err != nil {
return fmt.Errorf("clear mbid: %w", err)
}
}
if p.Within != nil {
return p.Within(ctx, tq)
}
return nil
})
if err != nil {
return dbq.Track{}, err
}
logger.Info("library: removed a file and kept its track",
"track_id", track.ID.String(), "file_path", track.FilePath, "reason", string(p.Reason))
return track, nil
}
// markRemoved marks tracks whose files were removed on purpose, and tells
// clients they are no longer playable. actorID is invalid when the
// duplicate resolver removed them on its own.
func markRemoved(ctx context.Context, tq *dbq.Queries, ids []pgtype.UUID, reason RemovedFor, actorID pgtype.UUID) error {
if _, err := tq.MarkTracksRemoved(ctx, dbq.MarkTracksRemovedParams{
Reason: string(reason), RemovedBy: actorID, Ids: ids,
}); err != nil {
return fmt.Errorf("mark removed: %w", err)
}
if err := logTrackChanges(ctx, tq, ids); err != nil {
return fmt.Errorf("log changes: %w", err)
}
return nil
}