This commit is contained in:
2026-05-09 14:51:25 -04:00
parent 97fabc2c17
commit f23a8a2be6
20 changed files with 382 additions and 185 deletions

View File

@@ -1,6 +1,5 @@
import os
from pathlib import Path
from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
@@ -19,7 +18,11 @@ async def list_libraries(db: AsyncSession = Depends(get_db)):
@router.post("", response_model=LibraryOut, status_code=201)
async def create_library(body: LibraryCreate, db: AsyncSession = Depends(get_db)):
async def create_library(
body: LibraryCreate,
background_tasks: BackgroundTasks,
db: AsyncSession = Depends(get_db),
):
path = Path(body.path)
if not path.is_dir():
raise HTTPException(400, f"Path does not exist or is not a directory: {body.path}")
@@ -30,19 +33,18 @@ async def create_library(body: LibraryCreate, db: AsyncSession = Depends(get_db)
lib = Library(name=body.name, path=str(path))
db.add(lib)
await db.flush()
await db.refresh(lib)
lib_id = lib.id
lib_path = lib.path
await db.commit()
await db.refresh(lib)
await scanner.scan_library(lib_id, lib_path, db)
watcher_service.start_watcher(lib_id, lib_path)
# Scan runs in the background so the HTTP response returns immediately
background_tasks.add_task(scanner.scan_library_background, lib.id, lib.path)
watcher_service.start_watcher(lib.id, lib.path)
return lib
async with db.begin():
pass
result = await db.execute(select(Library).where(Library.id == lib_id))
return result.scalars().first()
@router.get("/{library_id}/scan-status")
async def get_scan_status(library_id: int):
return {"scanning": scanner.is_scanning(library_id)}
@router.delete("/{library_id}", status_code=204)
@@ -72,8 +74,6 @@ async def browse_library(library_id: int, path: str = "", db: AsyncSession = Dep
if not target.is_dir():
raise HTTPException(404, "Directory not found")
# Load all media items in this directory (non-recursive)
rel_prefix = path.strip("/")
items_result = await db.execute(
select(MediaItem).where(
MediaItem.library_id == library_id,
@@ -84,19 +84,32 @@ async def browse_library(library_id: int, path: str = "", db: AsyncSession = Dep
for item in items_result.scalars().all():
db_items[item.rel_path] = item
from app.services.scanner import classify
entries: list[BrowseEntry] = []
for entry in sorted(target.iterdir(), key=lambda e: (e.is_file(), e.name.lower())):
rel_entry = str(entry.relative_to(root))
if entry.is_dir():
entries.append(BrowseEntry(name=entry.name, type="dir", rel_path=rel_entry))
elif entry.is_file() and rel_entry in db_items:
item = db_items[rel_entry]
entries.append(BrowseEntry(
name=entry.name,
type=item.media_type,
rel_path=rel_entry,
media_item_id=item.id,
))
elif entry.is_file():
db_item = db_items.get(rel_entry)
if db_item:
entries.append(BrowseEntry(
name=entry.name,
type=db_item.media_type,
rel_path=rel_entry,
media_item_id=db_item.id,
))
else:
# File exists on disk but scan hasn't indexed it yet; show by extension
media_type = classify(entry)
if media_type:
entries.append(BrowseEntry(
name=entry.name,
type=media_type,
rel_path=rel_entry,
media_item_id=None,
))
return BrowseResult(path=path, entries=entries)