From f64b51f161071acdfa88c6ef190a242bd50fa9ab Mon Sep 17 00:00:00 2001 From: Deluan Date: Tue, 11 Nov 2025 11:14:51 -0500 Subject: [PATCH] refactor(scanner): enhance ScanTarget struct with String method for better target representation Signed-off-by: Deluan --- scanner/controller.go | 5 +++++ scanner/external.go | 11 +++-------- scanner/watcher.go | 5 +++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/scanner/controller.go b/scanner/controller.go index d2ebdbfd4..28dbb7287 100644 --- a/scanner/controller.go +++ b/scanner/controller.go @@ -27,11 +27,16 @@ var ( ) // ScanTarget represents a specific folder within a library to be scanned. +// NOTE: This struct is used as a map key, so it should only contain comparable types. type ScanTarget struct { LibraryID int FolderPath string // Relative path within the library, or "" for entire library } +func (st ScanTarget) String() string { + return fmt.Sprintf("%d:%s", st.LibraryID, st.FolderPath) +} + type Scanner interface { // ScanAll starts a full scan of the music library. This is a blocking operation. ScanAll(ctx context.Context, fullScan bool) (warnings []string, err error) diff --git a/scanner/external.go b/scanner/external.go index 869034cc2..690748d8e 100644 --- a/scanner/external.go +++ b/scanner/external.go @@ -8,10 +8,11 @@ import ( "io" "os" "os/exec" - "strconv" + "strings" "github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/log" + "github.com/navidrome/navidrome/utils/slice" ) // scannerExternal is a scanner that runs an external process to do the scanning. It is used to avoid @@ -49,13 +50,7 @@ func (s *scannerExternal) scan(ctx context.Context, fullScan bool, targets []Sca // Add targets if provided if len(targets) > 0 { - var targetsStr string - for i, target := range targets { - if i > 0 { - targetsStr += "," - } - targetsStr += strconv.Itoa(target.LibraryID) + ":" + target.FolderPath - } + targetsStr := strings.Join(slice.Map(targets, func(t ScanTarget) string { return t.String() }), ",") args = append(args, "--targets", targetsStr) log.Debug(ctx, "Spawning external scanner process with targets", "fullScan", fullScan, "path", exe, "targets", targetsStr) } else { diff --git a/scanner/watcher.go b/scanner/watcher.go index 122af1c08..dd0d1cca2 100644 --- a/scanner/watcher.go +++ b/scanner/watcher.go @@ -121,10 +121,11 @@ func (w *watcher) Run(ctx context.Context) error { folderPath := notification.FolderPath // If already scheduled for scan, skip - if _, exists := targets[ScanTarget{LibraryID: lib.ID, FolderPath: folderPath}]; exists { + target := ScanTarget{LibraryID: lib.ID, FolderPath: folderPath} + if _, exists := targets[target]; exists { continue } - targets[ScanTarget{LibraryID: lib.ID, FolderPath: folderPath}] = struct{}{} + targets[target] = struct{}{} trigger.Reset(w.triggerWait) log.Debug(ctx, "Watcher: Detected changes. Waiting for more changes before triggering scan",