feat(library): Missing files says why each track waits, and can forget or export it
release / go (push) Successful in 1m49s
release / web (push) Successful in 1m26s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m29s
release / android (push) Successful in 4m43s
release / Build signed APK (push) Successful in 5m1s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
release / go (push) Successful in 1m49s
release / web (push) Successful in 1m26s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m29s
release / android (push) Successful in 4m43s
release / Build signed APK (push) Successful in 5m1s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
- The missing list carries each track's disc and track number, its
recording and release-group MBIDs, the Collection queue its file was
removed from, and the admin who removed it.
- POST /api/admin/library/missing/forget deletes waiting tracks for good,
tidying away an album or artist they leave empty, and writes a
track_forget audit row naming each. It is all or nothing: a track that
has its file again is refused with 409 track_has_file.
- GET /api/admin/library/missing.csv exports every waiting track (artist,
album, disc, track, title, MBIDs, why, since) to hand to whatever finds
music.
- DELETE /api/admin/tracks/{id} is retired. No client called it; its
service becomes tracks.Service.ForgetTracks.
Grimoire #7744 (milestone 635).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,29 +1,49 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/tracks"
|
||||
)
|
||||
|
||||
// missingTrackView is one track whose file the scan could not find.
|
||||
// missingTrackView is one track waiting for a file: removed by an admin from
|
||||
// a Collection queue, or gone from disk.
|
||||
//
|
||||
// The text fields come from the tracks row, not from the filesystem, which is
|
||||
// the point: the recording is still a known thing with a title, an artist and
|
||||
// a play history — only its bytes are absent. LastPlayedAt is nullable because
|
||||
// plenty of missing files were never played, and that is exactly the signal an
|
||||
// operator wants when deciding whether a lost file is worth replacing.
|
||||
//
|
||||
// RemovedFor is the queue the file was removed from ("report", "duplicate",
|
||||
// "suspect"), or null when the scan found it gone; RemovedBy is the admin who
|
||||
// removed it, null for the duplicate resolver. The MBIDs let an export be
|
||||
// matched exactly by whatever finds the good copy.
|
||||
type missingTrackView struct {
|
||||
TrackID string `json:"track_id"`
|
||||
Title string `json:"title"`
|
||||
ArtistID string `json:"artist_id"`
|
||||
ArtistName string `json:"artist_name"`
|
||||
AlbumID string `json:"album_id"`
|
||||
AlbumTitle string `json:"album_title"`
|
||||
FilePath string `json:"file_path"`
|
||||
DurationSec int32 `json:"duration_sec"`
|
||||
MissingSince string `json:"missing_since"`
|
||||
LastPlayedAt *string `json:"last_played_at"`
|
||||
TrackID string `json:"track_id"`
|
||||
Title string `json:"title"`
|
||||
ArtistID string `json:"artist_id"`
|
||||
ArtistName string `json:"artist_name"`
|
||||
AlbumID string `json:"album_id"`
|
||||
AlbumTitle string `json:"album_title"`
|
||||
FilePath string `json:"file_path"`
|
||||
DurationSec int32 `json:"duration_sec"`
|
||||
DiscNumber *int32 `json:"disc_number"`
|
||||
TrackNumber *int32 `json:"track_number"`
|
||||
RecordingMBID *string `json:"recording_mbid"`
|
||||
ReleaseGroupMBID *string `json:"release_group_mbid"`
|
||||
RemovedFor *string `json:"removed_for"`
|
||||
RemovedBy *string `json:"removed_by"`
|
||||
MissingSince string `json:"missing_since"`
|
||||
LastPlayedAt *string `json:"last_played_at"`
|
||||
}
|
||||
|
||||
// missingGroupView is a directory's worth of missing tracks.
|
||||
@@ -49,12 +69,11 @@ type adminMissingResponse struct {
|
||||
Groups []missingGroupView `json:"groups"`
|
||||
}
|
||||
|
||||
// handleListMissingTracks implements GET /api/admin/library/missing.
|
||||
//
|
||||
// Read-only by design. Nothing on this surface deletes a track: a missing file
|
||||
// keeps its row, its history and its likes because it may come back, and if it
|
||||
// comes back renamed the scanner adopts it. The surface exists so an admin
|
||||
// can SEE what the library has lost and act on it deliberately.
|
||||
// handleListMissingTracks implements GET /api/admin/library/missing: the
|
||||
// tracks waiting for a file. A waiting track keeps its row, its history and
|
||||
// its likes because a file may come back, and a good copy that lands in the
|
||||
// library is adopted or folded into it. Forgetting one is a separate,
|
||||
// deliberate call (handleForgetMissingTracks).
|
||||
func (h *handlers) handleListMissingTracks(w http.ResponseWriter, r *http.Request) {
|
||||
limit, offset, err := parsePaging(r.URL.Query())
|
||||
if err != nil {
|
||||
@@ -102,15 +121,21 @@ func groupMissingByDirectory(rows []dbq.ListMissingTracksRow) []missingGroupView
|
||||
groups := make([]missingGroupView, 0, 8)
|
||||
for _, row := range rows {
|
||||
t := missingTrackView{
|
||||
TrackID: uuidToString(row.ID),
|
||||
Title: row.Title,
|
||||
ArtistID: uuidToString(row.ArtistID),
|
||||
ArtistName: row.ArtistName,
|
||||
AlbumID: uuidToString(row.AlbumID),
|
||||
AlbumTitle: row.AlbumTitle,
|
||||
FilePath: row.FilePath,
|
||||
DurationSec: row.DurationMs / 1000,
|
||||
MissingSince: formatTimestamp(row.MissingSince),
|
||||
TrackID: uuidToString(row.ID),
|
||||
Title: row.Title,
|
||||
ArtistID: uuidToString(row.ArtistID),
|
||||
ArtistName: row.ArtistName,
|
||||
AlbumID: uuidToString(row.AlbumID),
|
||||
AlbumTitle: row.AlbumTitle,
|
||||
FilePath: row.FilePath,
|
||||
DurationSec: row.DurationMs / 1000,
|
||||
DiscNumber: row.DiscNumber,
|
||||
TrackNumber: row.TrackNumber,
|
||||
RecordingMBID: row.Mbid,
|
||||
ReleaseGroupMBID: row.ReleaseGroupMbid,
|
||||
RemovedFor: row.RemovedReason,
|
||||
RemovedBy: row.RemovedByName,
|
||||
MissingSince: formatTimestamp(row.MissingSince),
|
||||
}
|
||||
if row.LastPlayedAt.Valid {
|
||||
s := formatTimestamp(row.LastPlayedAt)
|
||||
@@ -134,3 +159,116 @@ func groupMissingByDirectory(rows []dbq.ListMissingTracksRow) []missingGroupView
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
// forgetMissingRequest is the body of POST /api/admin/library/missing/forget:
|
||||
// the waiting tracks to forget, one track or a whole folder's.
|
||||
type forgetMissingRequest struct {
|
||||
TrackIDs []string `json:"track_ids"`
|
||||
}
|
||||
|
||||
// forgetMissingRequestLimit caps the body: a folder of a few hundred ids is
|
||||
// a few tens of KB.
|
||||
const forgetMissingRequestLimit = 1 << 20
|
||||
|
||||
// handleForgetMissingTracks implements POST
|
||||
// /api/admin/library/missing/forget: the tracks go for good, with their
|
||||
// likes, plays and playlist places, and an album or artist they leave empty
|
||||
// goes too. It is all or nothing.
|
||||
//
|
||||
// Errors: 400 invalid_body / invalid_id; 404 track_not_found when an id is
|
||||
// unknown; 409 track_has_file when one has its file again, since forgetting
|
||||
// it would throw away music the library holds.
|
||||
func (h *handlers) handleForgetMissingTracks(w http.ResponseWriter, r *http.Request) {
|
||||
admin, ok := requireUser(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body forgetMissingRequest
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, forgetMissingRequestLimit)).Decode(&body); err != nil || len(body.TrackIDs) == 0 {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_body")
|
||||
return
|
||||
}
|
||||
ids := make([]pgtype.UUID, 0, len(body.TrackIDs))
|
||||
for _, raw := range body.TrackIDs {
|
||||
id, ok := parseUUID(raw)
|
||||
if !ok {
|
||||
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
|
||||
return
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
gone, err := h.tracks.ForgetTracks(r.Context(), ids, admin.ID)
|
||||
switch {
|
||||
case errors.Is(err, tracks.ErrNotFound):
|
||||
writeAdminJSONErr(w, http.StatusNotFound, "track_not_found")
|
||||
return
|
||||
case errors.Is(err, library.ErrTrackPresent):
|
||||
writeAdminJSONErr(w, http.StatusConflict, "track_has_file")
|
||||
return
|
||||
case err != nil:
|
||||
h.logger.Error("admin: forget missing tracks", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]int{"forgotten": len(gone)})
|
||||
}
|
||||
|
||||
// missingCSVHeader is the export's columns. They name the song for whatever
|
||||
// finds the good copy: the MBIDs match it exactly where the tags carried
|
||||
// them, and the names do the rest.
|
||||
var missingCSVHeader = []string{
|
||||
"artist", "album", "disc", "track", "title",
|
||||
"recording_mbid", "release_group_mbid", "why", "since",
|
||||
}
|
||||
|
||||
// handleExportMissingTracks implements GET /api/admin/library/missing.csv:
|
||||
// every track waiting for a file, in the list's folder order, as a CSV to
|
||||
// hand to whatever finds music. "why" is the queue the file was removed from,
|
||||
// or "missing" when the scan found it gone.
|
||||
func (h *handlers) handleExportMissingTracks(w http.ResponseWriter, r *http.Request) {
|
||||
q := dbq.New(h.pool)
|
||||
total, err := q.CountMissingTracks(r.Context())
|
||||
if err != nil {
|
||||
h.logger.Error("admin: count missing tracks", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
return
|
||||
}
|
||||
rows, err := q.ListMissingTracks(r.Context(), dbq.ListMissingTracksParams{PageLimit: int32(total), PageOffset: 0})
|
||||
if err != nil {
|
||||
h.logger.Error("admin: list missing tracks", "err", err)
|
||||
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="needs-a-file.csv"`)
|
||||
out := csv.NewWriter(w)
|
||||
_ = out.Write(missingCSVHeader)
|
||||
for _, row := range rows {
|
||||
why := "missing"
|
||||
if row.RemovedReason != nil {
|
||||
why = *row.RemovedReason
|
||||
}
|
||||
_ = out.Write([]string{
|
||||
row.ArtistName, row.AlbumTitle, optInt(row.DiscNumber), optInt(row.TrackNumber), row.Title,
|
||||
optString(row.Mbid), optString(row.ReleaseGroupMbid), why, formatTimestamp(row.MissingSince),
|
||||
})
|
||||
}
|
||||
out.Flush()
|
||||
if err := out.Error(); err != nil {
|
||||
h.logger.Warn("admin: write missing-tracks csv", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func optInt(v *int32) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
return strconv.Itoa(int(*v))
|
||||
}
|
||||
|
||||
func optString(v *string) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user