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 }