Files
2026-04-21 16:24:11 +09:00

75 lines
2.2 KiB
Python

"""Local music library endpoints.
Phase 1 provides diagnostics + listing/import APIs while keeping existing
processing constrained to IMPORTS_DIR.
"""
from __future__ import annotations
from pathlib import Path
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel
from ..services import local_library_service
router = APIRouter(prefix="/api/local-library", tags=["local-library"])
class LocalLibraryValidateRequest(BaseModel):
path: str = ""
class LocalLibraryImportRequest(BaseModel):
source_path: str
def _resolve_library_root_or_400() -> Path:
configured = local_library_service.read_configured_music_library_path()
validation = local_library_service.validate_library_path(configured)
if not validation.get("valid"):
raise HTTPException(status_code=400, detail=validation.get("error") or "Invalid music library path.")
return Path(validation["path"]).resolve()
@router.get("/config")
def get_local_library_config():
configured_path, source = local_library_service.read_configured_music_library_path_with_source()
result = local_library_service.validate_library_path(configured_path)
result["source"] = source
return result
@router.post("/validate")
def validate_local_library_path(body: LocalLibraryValidateRequest):
result = local_library_service.validate_library_path(body.path)
result["source"] = "request"
return result
@router.get("/tracks")
def list_local_library_tracks(
offset: int = Query(0, ge=0),
limit: int = Query(100, ge=1, le=500),
query: str = Query(""),
extensions: str = Query(""),
):
library_root = _resolve_library_root_or_400()
ext_values = [value.strip() for value in extensions.split(',') if value.strip()] if extensions else None
return local_library_service.list_tracks(
library_root,
offset=offset,
limit=limit,
query=query,
extensions=ext_values,
)
@router.post("/import")
def import_local_library_track(body: LocalLibraryImportRequest):
library_root = _resolve_library_root_or_400()
try:
return local_library_service.import_track(library_root, body.source_path)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc