mirror of
https://github.com/navidrome/navidrome.git
synced 2026-09-13 22:24:14 -04:00
* feat(cli): add missing file list and remap subcommands Signed-off-by: zerovox <933064+zerovox@users.noreply.github.com> * fix: prevent remapping from dropping participants on target track * fix: after remapping, refresh stats synchronously * fix: only move album annotations if moving a track would empty the old album * fix(persistence): keep the new item's annotation when reassigning onto an item the user already annotated ReassignAnnotation was a plain UPDATE; the annotation table is unique on (user_id, item_id, item_type), so when a user had annotated both items the statement aborted and none of the rows moved. In the scanner that surfaced as a warning; in the missing-file remap it rolled back the whole operation. UPDATE OR IGNORE moves what it can and leaves the conflicting rows for GC. * fix(core): keep the target track's history when remapping a missing file onto it The remap discards the target's row, and GC then dropped its play counts, stars, ratings, bookmarks and every playlist entry pointing at it. That is harmless in the scanner, whose target was imported seconds earlier, but the CLI lets the user pick any existing track. Move those references onto the surviving id first; where a user already has a row for both, theirs on the missing file wins. * fix(persistence): stop FindByPaths dropping plain paths that contain a colon Any colon was taken as the libraryID separator, and a non-numeric prefix made the whole path vanish from the lookup. 'missing fix' then rejected the very paths 'missing list' printed, and M3U imports silently skipped such tracks. Only a numeric prefix qualifies a path now. * perf(cli): stream 'missing list' instead of loading every missing file into memory GetAll materialised the whole result set before a single row was written; on a library with 97k missing files that peaked at 1.28 GB of RSS. Iterate the repository cursor and write rows as they arrive. * refactor(core): tidy the missing-file remap Drop the log lines copied from deleteMissing that still said 'after deleting missing files', the debug-on-success branches, and the what-comments; build the affected album list without slice helpers. * fix(cli): move path to the last column of 'missing list' Path is the only variable-width field, so leading with it misaligns every row that follows. Applies to both csv and json. * fix(persistence): also try a numeric colon prefix as a plain path '1999: A Different Life/01.mp3' parsed as library 1999 plus a truncated path and matched nothing. The prefix is ambiguous, so search both ways. Also buffer the json branch of 'missing list', which wrote a syscall per row. * fix(persistence): move scrobbles and buffered scrobbles off a discarded media file Both tables carry ON DELETE CASCADE on media_file_id, so 'missing fix' deleting the target erased its play history and dropped scrobbles still waiting on an external service. scrobble_buffer needs OR IGNORE for its unique (user_id, service, media_file_id, play_time). * fix(persistence): recompute the cached average rating after merging annotations Merging the discarded row's annotations grows the rating population of the surviving track, so media_file.average_rating no longer matched what the annotation rows say. Only reachable since the remap started merging those rows instead of deleting them. * fix(persistence): recompute the cached average rating inside ReassignAnnotation Moving annotation rows always changes the new item's rating population, so the recompute belongs with the move rather than at each call site. Covers the album reassign in the remap and the two scanner sites, and replaces the explicit call ReassignReferences was making. Album was the worse case: rate an album, move its files, and 'missing fix' handed the rating to an album still caching an average of 0. * fix(cli): let libraryID:path win over a file literally named like one FindByPaths searches a numeric-prefixed reference both ways, so a top-level file named '1:foo.mp3' can tie with library 1's 'foo.mp3'. The CLI then rejected the reference as ambiguous while advising the exact syntax the caller had used. Also disambiguates the same path in two libraries, which is what the qualified form is for. --------- Signed-off-by: zerovox <933064+zerovox@users.noreply.github.com> Co-authored-by: Deluan Quintão <deluan@navidrome.org>
172 lines
4.9 KiB
Go
172 lines
4.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/csv"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/core"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var missingListFormat string
|
|
|
|
func init() {
|
|
missingListCmd.Flags().StringVarP(&missingListFormat, "format", "f", "csv", "output format [supported values: csv, json]")
|
|
missingCmd.AddCommand(missingListCmd)
|
|
missingCmd.AddCommand(missingFixCmd)
|
|
rootCmd.AddCommand(missingCmd)
|
|
}
|
|
|
|
var (
|
|
missingCmd = &cobra.Command{
|
|
Use: "missing",
|
|
Short: "Manage missing files",
|
|
Long: "List files marked as missing and remap them onto existing files",
|
|
}
|
|
|
|
missingListCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List missing files",
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
|
runMissingList(cmd.Context())
|
|
},
|
|
}
|
|
|
|
missingFixCmd = &cobra.Command{
|
|
Use: "fix <missing path|id> <target path|id>",
|
|
Short: "Remap a missing file onto an existing file",
|
|
Long: "Remap a file marked as missing onto an existing (non-missing) file, the same way\n" +
|
|
"the scanner reconciles moved or renamed files. Each argument may be a media file ID,\n" +
|
|
"a library-relative path, or a libraryID:path pair.",
|
|
Args: cobra.ExactArgs(2),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
runMissingFix(cmd.Context(), args[0], args[1])
|
|
},
|
|
}
|
|
)
|
|
|
|
type displayMissingFile struct {
|
|
ID string `json:"id"`
|
|
LibraryID int `json:"libraryId"`
|
|
Title string `json:"title"`
|
|
Album string `json:"album"`
|
|
Artist string `json:"artist"`
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
func runMissingList(ctx context.Context) {
|
|
if missingListFormat != "csv" && missingListFormat != "json" {
|
|
log.Fatal("Invalid output format. Must be one of csv, json", "format", missingListFormat)
|
|
}
|
|
|
|
ds, ctx := getAdminContext(ctx)
|
|
mfs, err := ds.MediaFile(ctx).GetCursor(model.QueryOptions{
|
|
Filters: squirrel.Eq{"missing": true},
|
|
Sort: "path",
|
|
})
|
|
if err == nil {
|
|
err = writeMissingList(os.Stdout, missingListFormat, mfs)
|
|
}
|
|
if err != nil {
|
|
log.Fatal(ctx, "Failed to retrieve missing files", err)
|
|
}
|
|
}
|
|
|
|
// writeMissingList streams the cursor so a library with many missing files doesn't get loaded into memory
|
|
func writeMissingList(w io.Writer, format string, mfs model.MediaFileCursor) error {
|
|
if format == "json" {
|
|
bw := bufio.NewWriter(w)
|
|
_, _ = io.WriteString(bw, "[")
|
|
sep := ""
|
|
for mf, err := range mfs {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
j, _ := json.Marshal(displayMissingFile{ID: mf.ID, LibraryID: mf.LibraryID, Title: mf.Title, Album: mf.Album, Artist: mf.Artist, Path: mf.Path})
|
|
_, _ = fmt.Fprintf(bw, "%s%s", sep, j)
|
|
sep = ","
|
|
}
|
|
_, _ = io.WriteString(bw, "]\n")
|
|
return bw.Flush()
|
|
}
|
|
|
|
cw := csv.NewWriter(w)
|
|
_ = cw.Write([]string{"id", "library id", "title", "album", "artist", "path"})
|
|
for mf, err := range mfs {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_ = cw.Write([]string{mf.ID, strconv.Itoa(mf.LibraryID), mf.Title, mf.Album, mf.Artist, mf.Path})
|
|
}
|
|
cw.Flush()
|
|
return cw.Error()
|
|
}
|
|
|
|
func runMissingFix(ctx context.Context, missingRef, targetRef string) {
|
|
ds, ctx := getAdminContext(ctx)
|
|
|
|
missing := resolveMediaFile(ctx, ds, missingRef)
|
|
target := resolveMediaFile(ctx, ds, targetRef)
|
|
|
|
if err := core.NewMaintenance(ds).RemapMissingFile(ctx, missing.ID, target.ID); err != nil {
|
|
log.Fatal(ctx, "Failed to remap missing file", "missing", missing.Path, "target", target.Path, err)
|
|
}
|
|
fmt.Printf("Remapped %q onto %q\n", missing.Path, target.Path)
|
|
}
|
|
|
|
// resolveMediaFile looks up a media file by ID first, then by path (optionally libraryID:path).
|
|
func resolveMediaFile(ctx context.Context, ds model.DataStore, ref string) *model.MediaFile {
|
|
mf, err := ds.MediaFile(ctx).Get(ref)
|
|
if err == nil {
|
|
return mf
|
|
}
|
|
if !errors.Is(err, model.ErrNotFound) {
|
|
log.Fatal(ctx, "Error looking up media file", "ref", ref, err)
|
|
}
|
|
|
|
mfs, err := ds.MediaFile(ctx).FindByPaths([]string{ref})
|
|
if err != nil {
|
|
log.Fatal(ctx, "Error looking up media file by path", "ref", ref, err)
|
|
}
|
|
if len(mfs) == 0 {
|
|
log.Fatal(ctx, "No media file found", "ref", ref)
|
|
}
|
|
mfs = preferQualified(ref, mfs)
|
|
if len(mfs) > 1 {
|
|
log.Fatal(ctx, "Path matches multiple files; disambiguate with an ID or libraryID:path", "ref", ref, "matches", len(mfs))
|
|
}
|
|
return &mfs[0]
|
|
}
|
|
|
|
// preferQualified resolves the ambiguity FindByPaths creates by searching a "libraryID:path"
|
|
// reference both ways: an explicit library wins over a file literally named like one.
|
|
func preferQualified(ref string, mfs model.MediaFiles) model.MediaFiles {
|
|
id, path, ok := strings.Cut(ref, ":")
|
|
if !ok {
|
|
return mfs
|
|
}
|
|
libraryID, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
return mfs
|
|
}
|
|
qualified := slice.Filter(mfs, func(mf model.MediaFile) bool {
|
|
return mf.LibraryID == libraryID && strings.EqualFold(mf.Path, path)
|
|
})
|
|
if len(qualified) == 0 {
|
|
return mfs
|
|
}
|
|
return qualified
|
|
}
|