Files
alembic/app/routers/dedup.py
T
andrew 8f1fc458f6 Background long-running actions; confirm destructive ones
- Run now (jobs and playlists), dedup scan, genre preview/apply, and manual
  import now dispatch via BackgroundTasks and redirect immediately, instead
  of awaiting a job that can run for the better part of an hour and hang the
  browser or reverse proxy. Progress shows in the Jobs runs table (which
  already polls); if the pipeline is busy the run records skipped_lock there.
- Fix the import banner, which claimed work was ongoing after the request
  had actually blocked to completion; it now reflects the backgrounded start.
- Add confirmation prompts to the dedup per-row and bulk delete and to
  "Fix genres now", matching the existing confirms on library and playlist
  deletes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 14:31:54 -06:00

91 lines
3.7 KiB
Python

from fastapi import APIRouter, BackgroundTasks, Depends, Request
from fastapi.responses import RedirectResponse
from fastapi.templating import Jinja2Templates
from app.security.deps import require_auth
from app.services import dedup_review_service
from app.settings import settings
router = APIRouter(prefix="/dedup", tags=["dedup"])
templates = Jinja2Templates(directory="app/templates")
# Every library track lives under this prefix -- showing it on every row of
# every candidate adds nothing but width, so strip it for display (the full
# path is still available in the title attribute on hover).
_LIBRARY_PREFIX = str(settings.music_data_dir / "Library") + "/"
# dedup-library.sh's four tag-based passes (numbered_sibling, case_insensitive,
# normalized, cross_album_fuzzy) all match on filenames/tags; find-fuzzy-dupes.py's
# one pass (fuzzy_audio) matches on acoustic fingerprint. The specific pass
# name is an implementation detail -- the table just needs to say which of
# the two matching methods caught it.
_ACOUSTIC_PASS = "fuzzy_audio"
def _pass_label(pass_name: str) -> str:
return "Acoustically Similar" if pass_name == _ACOUSTIC_PASS else "File Naming"
def _display_path(path: str) -> str:
return path[len(_LIBRARY_PREFIX):] if path.startswith(_LIBRARY_PREFIX) else path
def _to_row(c) -> dict:
return {
"id": c.id,
"pass_name": c.pass_name,
"pass_label": _pass_label(c.pass_name),
"keep_path": c.keep_path,
"delete_path": c.delete_path,
"keep_display": _display_path(c.keep_path),
"delete_display": _display_path(c.delete_path),
"delete_size_bytes": c.delete_size_bytes,
}
@router.get("")
async def dedup_index(request: Request, user: dict = Depends(require_auth)):
candidates = [_to_row(c) for c in dedup_review_service.list_pending_candidates()]
ignored = [_to_row(c) for c in dedup_review_service.list_ignored_candidates()]
return templates.TemplateResponse(
request, "dedup/index.html", {"candidates": candidates, "ignored": ignored}
)
@router.post("/scan")
async def scan(background: BackgroundTasks, user: dict = Depends(require_auth)):
# Runs both the file-naming and acoustic passes -- see
# dedup_review_service.scan_all(). The acoustic pass can take a while on a
# large library, so run it in the background and return immediately; the
# candidates appear here once it finishes (reload), and the run shows up
# under Settings then Jobs. If the pipeline is busy it records skipped_lock
# there rather than blocking.
background.add_task(dedup_review_service.scan_all, triggered_by="manual")
return RedirectResponse(url="/dedup?started=1", status_code=303)
@router.post("/confirm")
async def confirm(request: Request, user: dict = Depends(require_auth)):
form = await request.form()
candidate_ids = [int(v) for k, v in form.multi_items() if k == "candidate_id"]
confirmed_by = user.get("email") or user.get("sub", "unknown")
await dedup_review_service.confirm_and_apply(candidate_ids, confirmed_by)
return RedirectResponse(url="/dedup", status_code=303)
@router.post("/ignore")
async def ignore(request: Request, user: dict = Depends(require_auth)):
form = await request.form()
ignored_by = user.get("email") or user.get("sub", "unknown")
for v in form.getlist("candidate_id"):
dedup_review_service.ignore_candidate(int(v), ignored_by)
return RedirectResponse(url="/dedup", status_code=303)
@router.post("/unignore")
async def unignore(request: Request, user: dict = Depends(require_auth)):
form = await request.form()
for v in form.getlist("candidate_id"):
dedup_review_service.unignore_candidate(int(v))
return RedirectResponse(url="/dedup", status_code=303)