92e5326437
confirm_and_apply no longer marks candidates confirmed before their apply job actually succeeds (a stuck/skipped_lock run was silently vanishing confirmed deletes without deleting anything), switches the fuzzy pass to --apply-pairs to avoid a full rescan on every confirm, and adds a job timeout so a hung subprocess can't hold the global pipeline lock forever. dedup-library.sh's Pass 2-4 use gawk-only features (gensub, POSIX interval regex) but were running under mawk (Debian's default awk) in the deployed image, throwing silent syntax errors on every run -- switched those invocations to gawk explicitly and added it to the Dockerfile. Also: per-track delete button on the library list/detail pages, and two CSS fixes (the primary button's hover gradient was getting clobbered by the base .btn:hover rule's plain background, and the select dropdown arrow had no background-size so it rendered oversized).
118 lines
3.7 KiB
Python
118 lines
3.7 KiB
Python
from fastapi import APIRouter, Depends, Form, HTTPException, Request
|
|
from fastapi.responses import RedirectResponse
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
from app.db import get_db
|
|
from app.security.deps import require_auth
|
|
from app.services import beets_service, genre_fix, library_edit, retag
|
|
|
|
router = APIRouter(prefix="/library", tags=["library"])
|
|
templates = Jinja2Templates(directory="app/templates")
|
|
|
|
|
|
PAGE_SIZE = 50
|
|
|
|
|
|
@router.get("")
|
|
async def library_index(
|
|
request: Request,
|
|
grouping: str | None = None,
|
|
format: str | None = None,
|
|
q: str | None = None,
|
|
page: int = 1,
|
|
user: dict = Depends(require_auth),
|
|
):
|
|
page = max(1, page)
|
|
offset = (page - 1) * PAGE_SIZE
|
|
items = beets_service.query_items(grouping=grouping, search=q, format=format, limit=PAGE_SIZE, offset=offset)
|
|
total = beets_service.count_items(grouping=grouping, search=q, format=format)
|
|
total_pages = max(1, (total + PAGE_SIZE - 1) // PAGE_SIZE)
|
|
stats = beets_service.stats()
|
|
|
|
return templates.TemplateResponse(
|
|
request,
|
|
"library/index.html",
|
|
{
|
|
"items": items,
|
|
"grouping": grouping or "",
|
|
"format": format or "",
|
|
"q": q or "",
|
|
"page": page,
|
|
"total_pages": total_pages,
|
|
"total": total,
|
|
"all_formats": beets_service.distinct_formats(),
|
|
"all_groupings": stats["groupings"],
|
|
},
|
|
)
|
|
|
|
|
|
@router.get("/track/{item_id}")
|
|
async def track_detail(request: Request, item_id: int, user: dict = Depends(require_auth)):
|
|
item = beets_service.get_item(item_id)
|
|
if item is None:
|
|
raise HTTPException(404, "no such track")
|
|
return templates.TemplateResponse(
|
|
request,
|
|
"library/track_detail.html",
|
|
{"item": item, "editable_fields": library_edit.EDITABLE_FIELDS},
|
|
)
|
|
|
|
|
|
@router.post("/track/{item_id}")
|
|
async def update_track(
|
|
request: Request,
|
|
item_id: int,
|
|
user: dict = Depends(require_auth),
|
|
db=Depends(get_db),
|
|
):
|
|
form = await request.form()
|
|
changes = {
|
|
field: value
|
|
for field, value in form.items()
|
|
if field in library_edit.EDITABLE_FIELDS and value != ""
|
|
}
|
|
changed_by = user.get("email") or user.get("sub", "unknown")
|
|
changed = await library_edit.update_track_fields(db, item_id, changes, changed_by)
|
|
return RedirectResponse(url=f"/library/track/{item_id}?changed={len(changed)}", status_code=303)
|
|
|
|
|
|
@router.post("/track/{item_id}/genre")
|
|
async def set_genre(
|
|
item_id: int,
|
|
artist: str = Form(...),
|
|
genre: str = Form(...),
|
|
user: dict = Depends(require_auth),
|
|
):
|
|
changed_by = user.get("email") or user.get("sub", "unknown")
|
|
await genre_fix.set_artist_genre(artist, genre, changed_by)
|
|
return RedirectResponse(url=f"/library/track/{item_id}", status_code=303)
|
|
|
|
|
|
@router.post("/track/{item_id}/retag")
|
|
async def retag_track(
|
|
item_id: int,
|
|
url: str = Form(...),
|
|
keep_genre: bool = Form(False),
|
|
user: dict = Depends(require_auth),
|
|
):
|
|
item = beets_service.get_item(item_id)
|
|
if item is None:
|
|
raise HTTPException(404, "no such track")
|
|
changed_by = user.get("email") or user.get("sub", "unknown")
|
|
await retag.retag_from_url(item["path"], url, changed_by, keep_genre=keep_genre)
|
|
return RedirectResponse(url=f"/library/track/{item_id}", status_code=303)
|
|
|
|
|
|
@router.post("/track/{item_id}/delete")
|
|
async def delete_track(
|
|
item_id: int,
|
|
user: dict = Depends(require_auth),
|
|
db=Depends(get_db),
|
|
):
|
|
deleted_by = user.get("email") or user.get("sub", "unknown")
|
|
try:
|
|
await library_edit.delete_track(db, item_id, deleted_by)
|
|
except ValueError:
|
|
raise HTTPException(404, "no such track")
|
|
return RedirectResponse(url="/library?deleted=1", status_code=303)
|