Files
romm/backend/endpoints/roms/files.py
Georges-Antoine Assi 3c95231294 feat: walkthrough documents with reading progress and GameFAQs import
Unify manuals and walkthroughs on one document model instead of a
separate walkthrough subsystem. Walkthroughs are RomFiles under a new
WALKTHROUGH category with an optional RomFileDocMeta provenance sidecar,
and reading progress is a per-user RomFileUser row keyed on rom_file_id
so the same mechanism covers manuals and walkthroughs.

Backend:
- models: RomFileCategory.WALKTHROUGH, DocSource, RomFileDocMeta,
  RomFileUser; migration 0110 (enum value + two tables, cross-DB)
- serve .txt/.html documents inline (HTML under a sandboxing CSP);
  add .txt to allowed manual files
- endpoints: walkthrough upload / GameFAQs fetch / delete, and per-user
  reading-progress GET (ROMS_READ) / PUT (ROMS_USER_WRITE, like notes, so a
  kiosk guest capped to the read scopes cannot write)
- last_page is range-checked, so an out-of-range value is a 400 rather than
  an integer column overflow
- the guide body is read under the size cap instead of being buffered in
  full and truncated afterwards
- GameFAQs import extracts text only (stored as .txt); no remote HTML is
  persisted or served. Host-allowlisted on top of the SSRF-safe client
- a Cloudflare bot challenge is detected and reported as "open it in your
  browser and upload the text" instead of an opaque 403
- reading-progress upsert retries as an update on a unique-constraint
  race, so concurrent saves for one user/file can't 500
- a GameFAQs guide is removed from disk again if indexing it fails,
  rather than being left unreferenced
- tests for CRUD, progress, the GameFAQs parser/URL validation, and
  hidden-rom masking on every walkthrough route

Frontend (v2):
- TextViewer (.txt/.html), useReadingProgress composable, WalkthroughSubtab
  (upload + add-from-GameFAQs-URL + delete + progress), wired into MediaTab
- ManualSubtab renders .txt manuals; walkthrough category surfaced in
  FilesTab; regenerated types; i18n keys across all locales
- a pending scroll position is committed before the viewer follows a new
  document, so switching files mid-debounce no longer drops it
- WalkthroughSubtab gates upload, the GameFAQs URL field, and delete on
  useCan("rom.edit"), as ManualSubtab already does

AI assistance: implemented with Claude Code (Opus 4.8). The data-model
design, backend, migration, tests, and v2 UI were AI-authored under human
direction. Alternative approach to #2892.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-25 19:40:20 -04:00

332 lines
12 KiB
Python

from datetime import datetime, timezone
from typing import Annotated
from anyio import Path
from fastapi import Body, HTTPException
from fastapi import Path as PathVar
from fastapi import Request, status
from fastapi.responses import Response
from starlette.responses import FileResponse
from config import DEV_MODE, DISABLE_DOWNLOAD_ENDPOINT_AUTH
from decorators.auth import protected_route
from endpoints.responses.rom import RomFileSchema, RomFileUserSchema
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
from handler.auth.constants import Scope
from handler.auth.dependencies import assert_can, assert_rom_visible, get_permissions
from handler.database import db_rom_handler
from handler.filesystem import fs_rom_handler
from logger.formatter import BLUE
from logger.formatter import highlight as hl
from logger.logger import log
from models.permission import PermAction, PermEntity
from models.rom import DOCUMENT_CATEGORIES, RomFileCategory
from utils.audio_tags import guess_audio_media_type
from utils.media_types import (
guess_media_file_type,
is_allowed_document_file,
is_allowed_media_file,
is_html_document_file,
)
from utils.nginx import FileRedirectResponse
from utils.router import APIRouter
router = APIRouter()
# Signed 32-bit INT ceiling, the narrowest `last_page` column across our DBs.
MAX_LAST_PAGE = 2**31 - 1
@protected_route(
router.get,
"/{id}/files",
[Scope.ROMS_READ],
responses={status.HTTP_404_NOT_FOUND: {}},
)
async def get_romfile(
request: Request,
id: Annotated[int, PathVar(description="Rom file internal id.", ge=1)],
) -> RomFileSchema:
"""Retrieve a rom file by ID."""
file = db_rom_handler.get_rom_file_by_id(id)
if not file:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="File not found",
)
# Resolve back to the parent rom and enforce its visibility, so a file
# belonging to a hidden rom can't be read by direct RomFile.id.
rom = db_rom_handler.get_rom(file.rom_id)
if not rom:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="File not found",
)
assert_rom_visible(request, rom, not_found_detail="File not found")
return RomFileSchema.model_validate(file)
@protected_route(
router.get,
"/{id}/files/content/{file_name}",
[] if DISABLE_DOWNLOAD_ENDPOINT_AUTH else [Scope.ROMS_READ],
responses={status.HTTP_404_NOT_FOUND: {}},
)
async def get_romfile_content(
request: Request,
id: Annotated[int, PathVar(description="Rom file internal id.", ge=1)],
file_name: Annotated[str, PathVar(description="File name to download")],
):
"""Download a rom file."""
current_username = (
request.user.username if request.user.is_authenticated else "unknown"
)
file = db_rom_handler.get_rom_file_by_id(id)
if not file:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="File not found",
)
# 404-mask file bytes of roms hidden from the caller: resolve the parent
# rom and apply its visibility before serving any content.
rom = db_rom_handler.get_rom(file.rom_id)
if not rom:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="File not found",
)
assert_rom_visible(request, rom, not_found_detail="File not found")
log.info(
f"User {hl(current_username, color=BLUE)} is downloading {hl(file.file_name)}"
)
# Derive content type / disposition / download name from the trusted DB
# record, never from the client-supplied file_name path param — otherwise a
# caller could request the same bytes with an arbitrary extension to force a
# mismatched Content-Type while served inline (content-sniffing/XSS).
# Audio, images and videos are served inline so <audio>/<video>/<img> in the
# details view can render and seek them; everything else downloads.
if file.category == RomFileCategory.SOUNDTRACK:
media_type = guess_audio_media_type(file.file_name)
disposition = "inline"
elif file.category in DOCUMENT_CATEGORIES and is_allowed_document_file(
file.file_name
):
# Only document-category files (manuals, walkthroughs) are served inline
# so the in-page viewer can render them; a game/extra file that happens
# to end in .pdf/.md/.txt still downloads.
media_type = guess_media_file_type(file.file_name)
disposition = "inline"
elif is_allowed_media_file(file.file_name):
media_type = guess_media_file_type(file.file_name)
disposition = "inline"
else:
media_type = "application/octet-stream"
disposition = "attachment"
# Inline files are served under an explicit, trusted Content-Type; nosniff
# keeps the browser from sniffing them into anything script-capable (e.g. a
# Markdown manual into HTML).
headers = {"X-Content-Type-Options": "nosniff"} if disposition == "inline" else {}
# HTML documents are sanitized on ingest, but serve them under a sandboxing
# CSP anyway so a crafted document can never run scripts or reach the
# session origin if it is opened directly.
if disposition == "inline" and is_html_document_file(file.file_name):
headers["Content-Security-Policy"] = "sandbox; default-src 'none'"
# Serve the file directly in development mode for emulatorjs
if DEV_MODE:
rom_path = fs_rom_handler.validate_path(file.full_path)
# Starlette sets Content-Length and honors Range natively — inline
# disposition lets <audio> seek via Range requests.
return FileResponse(
path=rom_path,
filename=file.file_name,
media_type=media_type,
content_disposition_type=disposition,
headers=headers,
)
# Otherwise proxy through nginx (which parses Range itself via X-Accel-Redirect)
return FileRedirectResponse(
download_path=Path(f"/library/{file.full_path}"),
disposition=disposition,
media_type=media_type,
headers=headers,
)
@protected_route(
router.delete,
"/{rom_id}/files/{file_id}",
[Scope.ROMS_WRITE],
responses={status.HTTP_404_NOT_FOUND: {}},
)
async def delete_rom_file(
request: Request,
rom_id: Annotated[int, PathVar(description="Rom internal id.", ge=1)],
file_id: Annotated[int, PathVar(description="Rom file internal id.", ge=1)],
) -> Response:
"""Delete a single file from a ROM."""
# Removing a game file destroys library content, so it needs the same
# DELETE grant the whole-ROM delete route requires. The sibling routes for
# manuals/soundtracks/screenshots settle for ROMS_WRITE because a category
# guard keeps them off the game files themselves.
assert_can(get_permissions(request), PermEntity.ROMS, PermAction.DELETE)
rom = db_rom_handler.get_rom(rom_id)
if not rom:
raise RomNotFoundInDatabaseException(rom_id)
assert_rom_visible(request, rom, not_found_detail="File not found")
rom_file = db_rom_handler.get_rom_file_by_id(file_id)
if not rom_file or rom_file.rom_id != rom.id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="File not found",
)
file_rel_path = rom_file.full_path
try:
await fs_rom_handler.remove_file(file_rel_path)
except FileNotFoundError:
log.warning(
f"ROM file {hl(file_rel_path)} not found on disk; "
f"removing DB row anyway"
)
except Exception as exc:
log.error(f"Error deleting ROM file {hl(file_rel_path)}", exc_info=exc)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="There was an error deleting the file",
) from exc
db_rom_handler.delete_rom_file(file_id)
log.info(
f"Deleted file {hl(rom_file.file_name)} from "
f"{hl(rom.name or 'ROM', color=BLUE)} [{hl(rom.fs_name)}]"
)
return Response()
def _assert_document_file(
rom_id: int, file_id: int, request: Request
) -> RomFileCategory:
"""Resolve a document-category file, enforcing parent-rom visibility."""
rom = db_rom_handler.get_rom(rom_id)
if not rom:
raise RomNotFoundInDatabaseException(rom_id)
assert_rom_visible(request, rom, not_found_detail="File not found")
rom_file = db_rom_handler.get_rom_file_by_id(file_id)
if (
not rom_file
or rom_file.rom_id != rom.id
or rom_file.category not in DOCUMENT_CATEGORIES
):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Document file not found",
)
return rom_file.category # type: ignore[return-value]
@protected_route(
router.get,
"/{rom_id}/files/{file_id}/progress",
[Scope.ROMS_READ],
responses={status.HTTP_404_NOT_FOUND: {}},
)
async def get_rom_file_progress(
request: Request,
rom_id: Annotated[int, PathVar(description="Rom internal id.", ge=1)],
file_id: Annotated[int, PathVar(description="Rom file internal id.", ge=1)],
) -> RomFileUserSchema:
"""Get the current user's reading progress for a document file."""
_assert_document_file(rom_id, file_id, request)
state = db_rom_handler.get_rom_file_user(
rom_file_id=file_id, user_id=request.user.id
)
if state:
return RomFileUserSchema.model_validate(state)
# No stored progress yet: return a zeroed default so the client has a shape.
return RomFileUserSchema(
rom_file_id=file_id,
user_id=request.user.id,
progress=0.0,
last_page=None,
finished=False,
last_read_at=None,
)
@protected_route(
router.put,
"/{rom_id}/files/{file_id}/progress",
[Scope.ROMS_USER_WRITE],
responses={status.HTTP_404_NOT_FOUND: {}},
)
async def update_rom_file_progress(
request: Request,
rom_id: Annotated[int, PathVar(description="Rom internal id.", ge=1)],
file_id: Annotated[int, PathVar(description="Rom file internal id.", ge=1)],
body: Annotated[dict, Body()],
) -> RomFileUserSchema:
"""Upsert the current user's reading progress for a document file.
Self-service per-user state, so it takes ROMS_USER_WRITE rather than the
library-wide ROMS_WRITE, exactly as rom notes do.
"""
_assert_document_file(rom_id, file_id, request)
values: dict = {"last_read_at": datetime.now(timezone.utc)}
if "progress" in body:
try:
values["progress"] = min(1.0, max(0.0, float(body["progress"])))
except (TypeError, ValueError) as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="progress must be a number between 0 and 1",
) from exc
if "last_page" in body:
last_page = body["last_page"]
if last_page is not None:
try:
last_page = int(last_page)
except (TypeError, ValueError) as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="last_page must be an integer",
) from exc
# Bounded so an out-of-range value is a 400 rather than an integer
# column overflow at insert time.
if not 0 <= last_page <= MAX_LAST_PAGE:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"last_page must be between 0 and {MAX_LAST_PAGE}",
)
values["last_page"] = last_page
if "finished" in body:
values["finished"] = bool(body["finished"])
state = db_rom_handler.upsert_rom_file_user(
rom_file_id=file_id, user_id=request.user.id, values=values
)
return RomFileUserSchema.model_validate(state)