From 68b1ba9a730e021a56ff148570d9d9028d6d71d3 Mon Sep 17 00:00:00 2001 From: drondeseries Date: Sun, 8 Feb 2026 23:49:31 -0500 Subject: [PATCH] feat(health): add robust path matching and Download webhook re-linking --- internal/api/arrs_handlers.go | 8 +++--- internal/database/health_repository.go | 35 ++++++++++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/internal/api/arrs_handlers.go b/internal/api/arrs_handlers.go index ed7bdaa57..a5224e8a9 100644 --- a/internal/api/arrs_handlers.go +++ b/internal/api/arrs_handlers.go @@ -323,14 +323,14 @@ func (s *Server) handleArrsWebhook(c *fiber.Ctx) error { var releaseDate *time.Time var sourceNzb *string - // Handle Rename specifically: try to find and re-link old record - if req.EventType == "Rename" { + // Handle Rename and Download specifically: try to find and re-link old record + if req.EventType == "Rename" || req.EventType == "Download" { fileName := filepath.Base(normalizedPath) // Try to find a record with the same filename but currently under /complete/ // or with a NULL library_path if err := s.healthRepo.RelinkFileByFilename(c.Context(), fileName, normalizedPath, path); err == nil { - slog.InfoContext(c.Context(), "Successfully re-linked health record during Rename webhook", - "filename", fileName, "new_library_path", path) + slog.InfoContext(c.Context(), "Successfully re-linked health record during webhook", + "event", req.EventType, "filename", fileName, "new_library_path", path) continue // Successfully re-linked, no need to add new } } diff --git a/internal/database/health_repository.go b/internal/database/health_repository.go index a00d85be1..18a9a5a6b 100644 --- a/internal/database/health_repository.go +++ b/internal/database/health_repository.go @@ -1506,18 +1506,31 @@ func (r *HealthRepository) RenameHealthRecord(ctx context.Context, oldPath, newP oldPath = strings.TrimPrefix(oldPath, "/") newPath = strings.TrimPrefix(newPath, "/") - query := ` - UPDATE file_health - SET file_path = ?, - library_path = CASE - WHEN library_path = ? THEN ? - ELSE library_path - END, - updated_at = datetime('now') - WHERE file_path = ? - ` + query := ` + + UPDATE file_health + + SET file_path = ?, + + library_path = CASE - _, err := r.db.ExecContext(ctx, query, newPath, oldPath, newPath, oldPath) + WHEN library_path IS NULL OR library_path = ? OR library_path = '/' || ? OR library_path LIKE '%' || ? THEN ? + + ELSE library_path + + END, + + updated_at = datetime('now') + + WHERE file_path = ? + + ` + + + + _, err := r.db.ExecContext(ctx, query, newPath, oldPath, oldPath, oldPath, newPath, oldPath) + + if err != nil { return fmt.Errorf("failed to rename health record: %w", err) }