Files
romm/backend/endpoints/states.py
Georges-Antoine Assi 1da0212b7f refactor(assets): one home for filing a state or a screenshot
`endpoints/states.py` wrote the same write-scan-upsert sequence twice for
screenshots and once for states, and the streaming sync had written its own
third copy of each. The sequence now lives in `handler/asset_store.py`, which
both callers use.

What differs between them stays with the caller: the upload route still owns
its filename validation and last-played bump, and the streaming sync still owns
its byte-identical dedup and its retention limit. `store_state_file` takes a
`fields` map for columns only one caller owns, so the upload path can never
clear the disc the streaming path recorded.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-05 10:24:56 -04:00

347 lines
12 KiB
Python

from datetime import datetime, timezone
from typing import Annotated
from fastapi import Body, File, HTTPException, Query, Request, UploadFile, status
from fastapi.responses import FileResponse
from decorators.auth import protected_route
from endpoints.responses.assets import StateSchema
from endpoints.roms import refresh_affected_smart_collections
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
from handler.asset_store import store_screenshot, store_state_file
from handler.auth.constants import Scope
from handler.auth.dependencies import assert_rom_visible
from handler.database import db_rom_handler, db_screenshot_handler, db_state_handler
from handler.filesystem import fs_asset_handler
from handler.filesystem.assets_handler import build_asset_file_response
from logger.formatter import BLUE
from logger.formatter import highlight as hl
from logger.logger import log
from models.assets import State
from utils.filesystem import sanitize_filename
from utils.router import APIRouter
from utils.uploads import check_asset_upload_size
from utils.validation import RomIdScope, narrow_rom_id_scope
router = APIRouter(
prefix="/states",
tags=["states"],
)
STATE_FILE_UPLOAD = File(..., description="State file to upload.")
STATE_SCREENSHOT_UPLOAD = File(
default=None,
description="Screenshot file associated with this state.",
)
STATE_FILE_UPDATE = File(default=None, description="Updated state file content.")
STATE_SCREENSHOT_UPDATE = File(default=None, description="Updated screenshot file.")
@protected_route(router.post, "", [Scope.ASSETS_WRITE])
async def add_state(
request: Request,
rom_id: int,
emulator: str | None = None,
stateFile: UploadFile = STATE_FILE_UPLOAD,
screenshotFile: UploadFile | None = STATE_SCREENSHOT_UPLOAD,
) -> StateSchema:
check_asset_upload_size(stateFile, "State file")
check_asset_upload_size(screenshotFile, "Screenshot file")
rom = db_rom_handler.get_rom(rom_id)
if not rom:
raise RomNotFoundInDatabaseException(rom_id)
assert_rom_visible(request, rom)
if not stateFile.filename:
log.error("State file has no filename")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="State file has no filename"
)
try:
sanitized_state_filename = sanitize_filename(stateFile.filename)
except ValueError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid state filename: {str(exc)}",
) from exc
log.info(
f"Uploading state {hl(sanitized_state_filename)} for {hl(str(rom.name), color=BLUE)}"
)
db_state = await store_state_file(
request.user, rom, emulator, stateFile, sanitized_state_filename
)
if screenshotFile and screenshotFile.filename:
try:
sanitized_screenshot_filename = sanitize_filename(screenshotFile.filename)
except ValueError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid screenshot filename: {str(exc)}",
) from exc
await store_screenshot(
request.user, rom, screenshotFile, sanitized_screenshot_filename
)
# Set the last played time for the current user
rom_user = db_rom_handler.get_rom_user(rom_id=rom.id, user_id=request.user.id)
if not rom_user:
rom_user = db_rom_handler.add_rom_user(rom_id=rom.id, user_id=request.user.id)
db_rom_handler.update_rom_user(
rom_user.id, {"last_played": datetime.now(timezone.utc)}
)
# Refetch the rom to get updated states
rom = db_rom_handler.get_rom(rom_id)
if not rom:
raise RomNotFoundInDatabaseException(rom_id)
refresh_affected_smart_collections([rom.id], membership_only=True)
return StateSchema.model_validate(db_state)
@protected_route(router.get, "", [Scope.ASSETS_READ])
def get_states(
request: Request,
rom_id: int | None = None,
rom_ids: Annotated[
RomIdScope,
Query(
description=(
"ROM IDs to scope the results to, for clients syncing a known "
"set of ROMs. Multiple values are allowed by repeating the "
"parameter. Combined with `rom_id` when both are given."
),
),
] = None,
platform_id: int | None = None,
) -> list[StateSchema]:
"""Retrieve states for the current user."""
states = db_state_handler.get_states(
user_id=request.user.id,
rom_ids=narrow_rom_id_scope(rom_id, rom_ids),
platform_id=platform_id,
)
return [StateSchema.model_validate(state) for state in states]
@protected_route(router.get, "/identifiers", [Scope.ASSETS_READ])
def get_state_identifiers(
request: Request,
) -> list[int]:
"""Get state identifiers endpoint
Args:
request (Request): Fastapi Request object
Returns:
list[int]: List of state IDs
"""
states = db_state_handler.get_states(
user_id=request.user.id,
only_fields=[State.id],
)
return [state.id for state in states]
@protected_route(router.get, "/{id}", [Scope.ASSETS_READ])
def get_state(request: Request, id: int) -> StateSchema:
state = db_state_handler.get_state(user_id=request.user.id, id=id)
if not state:
error = f"State with ID {id} not found"
log.error(error)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=error)
return StateSchema.model_validate(state)
@protected_route(router.get, "/{id}/content", [Scope.ASSETS_READ])
def download_state(request: Request, id: int) -> FileResponse:
"""Download a state file. Owner can download any of their states; everyone
else only public ones."""
state = db_state_handler.get_state_by_id(id)
if not state or (state.user_id != request.user.id and not state.is_public):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"State with ID {id} not found",
)
# Sharing must not override the hidden-ROM/platform policy: a state on a ROM
# hidden from the caller stays 404-masked, just like the ROM itself.
assert_rom_visible(
request, state.rom, not_found_detail=f"State with ID {id} not found"
)
try:
file_path = fs_asset_handler.validate_path(state.full_path)
except ValueError:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="State file not found",
) from None
if not file_path.exists() or not file_path.is_file():
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="State file not found on disk",
)
return build_asset_file_response(file_path, filename=state.file_name)
@protected_route(router.put, "/{id}", [Scope.ASSETS_WRITE])
async def update_state(
request: Request,
id: int,
stateFile: UploadFile | None = STATE_FILE_UPDATE,
screenshotFile: UploadFile | None = STATE_SCREENSHOT_UPDATE,
) -> StateSchema:
check_asset_upload_size(stateFile, "State file")
check_asset_upload_size(screenshotFile, "Screenshot file")
db_state = db_state_handler.get_state(user_id=request.user.id, id=id)
if not db_state:
error = f"State with ID {id} not found"
log.error(error)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=error)
if stateFile:
await fs_asset_handler.write_file(
file=stateFile, path=db_state.file_path, filename=db_state.file_name
)
db_state = db_state_handler.update_state(
db_state.id, {"file_size_bytes": stateFile.size}
)
if screenshotFile and screenshotFile.filename:
try:
sanitized_screenshot_filename = sanitize_filename(screenshotFile.filename)
except ValueError as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Invalid screenshot filename: {str(exc)}",
) from exc
await store_screenshot(
request.user,
db_state.rom,
screenshotFile,
sanitized_screenshot_filename,
)
# Set the last played time for the current user
rom_user = db_rom_handler.get_rom_user(db_state.rom_id, request.user.id)
if not rom_user:
rom_user = db_rom_handler.add_rom_user(db_state.rom_id, request.user.id)
db_rom_handler.update_rom_user(
rom_user.id, {"last_played": datetime.now(timezone.utc)}
)
# Refetch the state to get updated fields
return StateSchema.model_validate(db_state)
@protected_route(
router.put,
"/{id}/visibility",
[Scope.ASSETS_WRITE],
responses={status.HTTP_404_NOT_FOUND: {}},
)
def update_state_visibility(
request: Request,
id: int,
is_public: Annotated[bool, Body(embed=True)],
) -> StateSchema:
"""Toggle a state's public/private visibility (owner only)."""
state = db_state_handler.get_state_by_id(id)
if not state or state.user_id != request.user.id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"State with ID {id} not found",
)
updated = db_state_handler.update_state(id, {"is_public": is_public})
# Keep the auto-captured thumbnail's visibility in sync so a shared state
# still renders its preview for other users.
if state.screenshot:
db_screenshot_handler.update_screenshot(
state.screenshot.id, {"is_public": is_public}
)
# Sharing a state exposes it to every other user's `has_states` filter.
refresh_affected_smart_collections([state.rom_id], membership_only=True)
return StateSchema.model_validate(updated)
@protected_route(
router.post,
"/delete",
[Scope.ASSETS_WRITE],
responses={
status.HTTP_400_BAD_REQUEST: {},
status.HTTP_404_NOT_FOUND: {},
},
)
async def delete_states(
request: Request,
states: Annotated[
list[int],
Body(
description="List of states ids to delete from database.",
embed=True,
),
],
) -> list[int]:
"""Delete states."""
if not states:
error = "No states were provided"
log.error(error)
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=error)
affected_rom_ids: set[int] = set()
for state_id in states:
state = db_state_handler.get_state(user_id=request.user.id, id=state_id)
if not state:
error = f"State with ID {state_id} not found"
log.error(error)
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=error)
affected_rom_ids.add(state.rom_id)
db_state_handler.delete_state(state_id)
log.info(
f"Deleting state {hl(state.file_name)} [{state.rom.platform_slug}] from filesystem"
)
try:
file_path = f"{state.file_path}/{state.file_name}"
await fs_asset_handler.remove_file(file_path=file_path)
except FileNotFoundError:
error = f"State file {hl(state.file_name)} not found for platform {hl(state.rom.platform_display_name, color=BLUE)}[{hl(state.rom.platform_slug)}]"
log.error(error)
if state.screenshot:
db_screenshot_handler.delete_screenshot(state.screenshot.id)
try:
file_path = f"{state.screenshot.file_path}/{state.screenshot.file_name}"
await fs_asset_handler.remove_file(file_path=file_path)
except FileNotFoundError:
error = f"Screenshot file {hl(state.screenshot.file_name)} not found for state {hl(state.file_name)}[{hl(state.rom.platform_slug)}]"
log.error(error)
refresh_affected_smart_collections(list(affected_rom_ids), membership_only=True)
return states