release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every import and the Dockerfile's version ldflags. - buildinfo.ProjectURL, the contact outside services see in Minstrel's User-Agent: https://github.com/Fabled-Sword/minstrel. - README's compose example pulls ghcr.io/fabled-sword/minstrel:latest. - docs/security.md sends problem reports to the GitHub issues. - The demo-library stack builds Minstrel from the checkout, since :dev is not published to GHCR. - hygiene.go's same-site example uses example.com names. The workbench CI (.gitea/, ci/, export-ignored) still builds and publishes :dev to the private registry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.5 KiB
Go
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"
|
|
|
|
"github.com/Fabled-Sword/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
|
|
}
|