Files
DeepTutor/deeptutor/api/routers/file_library.py
Srini Jagadeesh 3006ec3671 feat: Persistent File Library with API router (issue #1437) (#1446)
* feat: Persistent File Library with API router (issue #1437)

FileLibraryStore (deeptutor/services/storage/file_library.py):
- add_file: stores bytes to disk, deduplicates by SHA-256
- get_file: returns entry by id
- delete_file: soft-delete, idempotent
- hard_delete_file: permanent delete (requires prior soft-delete)
- restore_file: clears is_deleted, idempotent
- list_files: active entries, newest-first, paginated
- search_files: case-insensitive filename LIKE search
- resolve_path: returns absolute Path or None

API router (deeptutor/api/routers/file_library.py):
- GET    /files/library/              — list files
- POST   /files/library/              — multipart upload with SHA-256 deduplication
- GET    /files/library/search        — filename search
- GET    /files/library/{id}          — get entry
- GET    /files/library/{id}/download — download file bytes
- DELETE /files/library/{id}          — soft-delete
- POST   /files/library/{id}/restore   — restore soft-deleted

25 unit tests + 20 API integration tests, all passing.

* fix: ruff import-sort/unused-import fixes for file library

* fix: scope file library singleton per user, close upload dedup race

- get_file_library_store() was caching the FileLibraryStore under a
  hardcoded key ("default") instead of the resolved per-user root, so
  in multi-user deployments every user after the first shared one
  library (list/get/download/delete/restore all leaked across
  accounts). Now resolves get_path_service().get_user_root() on every
  call and keys the cache by str(user_root), mirroring
  get_attachment_store().

- _add_file_sync's SHA-256 dedup check was a plain SELECT followed by
  an INSERT with no synchronization; concurrent uploads of identical
  content (double-click, duplicate tabs, client retries) could both
  miss the dedup lookup and each create a duplicate row/file. Added a
  per-instance lock around the check-then-write-then-insert sequence,
  plus a partial unique index on sha256 (active rows only) with
  IntegrityError handling as a defense-in-depth backstop for
  multi-process deployments.

- Fixed the test fixture in test_file_library_router.py: it patched
  file_library._fl.get_file_library_store then reverted the patch
  before any request ran, so the mock never actually intercepted
  anything; removed the dead code and added tokens/auth_enabled
  support for multi-user testing.

- Added regression tests: test_upload_and_list_are_isolated_between_users
  and test_add_file_concurrent_uploads_of_same_content_do_not_duplicate,
  both verified to fail against the pre-fix code.

- ruff format on the three touched files (pre-existing line-length
  debt in code this change touches).
2026-09-14 01:28:50 +08:00

170 lines
5.5 KiB
Python

"""HTTP endpoints for the Persistent File Library (issue #1437).
URL shape::
GET /files/library/ — list files (newest first)
POST /files/library/ — upload a file (multipart)
GET /files/library/search — search by filename (query param: q)
GET /files/library/{file_id} — get one entry
GET /files/library/{file_id}/download — download the file
DELETE /files/library/{file_id} — soft-delete a file
POST /files/library/{file_id}/restore — restore a soft-deleted file
The library is scoped to the authenticated user (via ``require_auth``) so
each user sees only their own files.
"""
from __future__ import annotations
import logging
import mimetypes
from fastapi import (
APIRouter,
Depends,
File,
Form,
HTTPException,
Query,
)
from fastapi.responses import FileResponse
from deeptutor.api.routers.auth import require_auth
from deeptutor.services.auth import TokenPayload
from deeptutor.services.storage import file_library as _fl
logger = logging.getLogger(__name__)
router = APIRouter()
def _get_store():
"""Resolve the store at call time so tests can monkey-patch ``_fl``."""
return _fl.get_file_library_store()
def _mime_type(filename: str) -> str:
mt, _ = mimetypes.guess_type(filename)
return mt or "application/octet-stream"
# ── endpoints ──────────────────────────────────────────────────────────────
@router.get("/", operation_id="library_list")
async def list_library_files(
limit: int = Query(default=50, ge=1, le=200),
offset: int = Query(default=0, ge=0),
_auth: TokenPayload = Depends(require_auth),
) -> list[dict]:
"""List active library entries, newest first."""
return await _get_store().list_files(limit=limit, offset=offset)
@router.post("/", operation_id="library_add")
async def add_library_file(
filename: str = File(..., description="Original filename"),
mime_type: str = Form(""),
file: bytes = File(..., description="Raw file bytes"),
_auth: TokenPayload = Depends(require_auth),
) -> dict:
"""Add a file to the library.
The request must be ``multipart/form-data`` with a ``file`` field containing
the raw bytes. If a file with the same SHA-256 content hash already exists
in the library, returns the existing entry (deduplication).
"""
entry = await _get_store().add_file(
data=file,
filename=filename,
mime_type=mime_type,
)
return entry
@router.get("/search", operation_id="library_search")
async def search_library_files(
q: str = Query(..., min_length=1, max_length=200),
_auth: TokenPayload = Depends(require_auth),
) -> list[dict]:
"""Search library entries by filename (case-insensitive substring)."""
return await _get_store().search_files(query=q)
@router.get("/{file_id}", operation_id="library_get")
async def get_library_file(
file_id: str,
_auth: TokenPayload = Depends(require_auth),
) -> dict:
"""Get a single library entry by id (includes soft-deleted)."""
entry = await _get_store().get_file(file_id)
if entry is None:
raise HTTPException(status_code=404, detail="File not found")
return entry
@router.get("/{file_id}/download", operation_id="library_download")
async def download_library_file(
file_id: str,
_auth: TokenPayload = Depends(require_auth),
) -> FileResponse:
"""Download a library file by id."""
store = _get_store()
entry = await store.get_file(file_id)
if entry is None or entry.get("is_deleted"):
raise HTTPException(status_code=404, detail="File not found")
path = store.resolve_path(file_id)
if path is None or not path.is_file():
raise HTTPException(status_code=404, detail="File not found on disk")
media_type = _mime_type(entry["filename"])
return FileResponse(
path,
media_type=media_type,
filename=entry["filename"],
headers={
"Cache-Control": "private, max-age=31536000, immutable",
"X-Content-Type-Options": "nosniff",
},
)
@router.head("/{file_id}/download", operation_id="library_download_head")
async def download_library_file_head(
file_id: str,
_auth: TokenPayload = Depends(require_auth),
) -> None:
"""Check whether a library file exists (HEAD only)."""
store = _get_store()
entry = await store.get_file(file_id)
if entry is None or entry.get("is_deleted"):
raise HTTPException(status_code=404, detail="File not found")
path = store.resolve_path(file_id)
if path is None or not path.is_file():
raise HTTPException(status_code=404, detail="File not found on disk")
@router.delete("/{file_id}", operation_id="library_delete")
async def delete_library_file(
file_id: str,
_auth: TokenPayload = Depends(require_auth),
) -> dict:
"""Soft-delete a library file (sets is_deleted=1)."""
deleted = await _get_store().delete_file(file_id)
if not deleted:
raise HTTPException(status_code=404, detail="File not found")
return {"id": file_id, "deleted": True}
@router.post("/{file_id}/restore", operation_id="library_restore")
async def restore_library_file(
file_id: str,
_auth: TokenPayload = Depends(require_auth),
) -> dict:
"""Restore a soft-deleted library file."""
restored = await _get_store().restore_file(file_id)
if not restored:
raise HTTPException(status_code=404, detail="File not found")
return {"id": file_id, "restored": True}