Files
romm/backend/endpoints/memory_cards.py
Sam Dornan 5a6c895d33 chore(mypy): give every generic its type arguments (#4818)
* chore(mypy): switch to strict mode with listed exceptions

Replace the hand-picked flags with `strict = True`, turning off only the
four flags that aren't clean yet (disallow_any_generics,
warn_return_any, disallow_untyped_calls, disallow_untyped_decorators).
Any other strict flag, including those future mypy releases add to the
preset, is on by default.

That turns on no_implicit_reexport, strict_equality and extra_checks:
- Import each name from the module that defines it. About 240 test
  sites patched a dependency through the module that imported it
  (`scan_module.fs_rom_handler`); they now import the same object from
  its source, so every patch targets what it did before. Tests that
  replace a module attribute are unchanged.
- handler.streaming.memory_cards re-exports store_memory_card_version
  explicitly, since endpoints call it there and tests patch it there.
- mutagen gets implicit_reexport, as it re-exports without __all__.
- Test comparisons build typed values (CoverResult, a typed expected
  dict, enum .value) and the CORS lookup casts for its identity check.
- One ss_handler ** spread keeps a coded ignore: extra_checks wants its
  NotRequired dump_* keys spelled out, and the media dict never has them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

* chore(mypy): give every generic its type arguments

Drop disallow_any_generics from the strict-mode exceptions and fix the
347 bare generics it reports, 230 of them in app code.

- Bare `dict` becomes `dict[str, Any]`: update payloads, raw provider
  JSON and endpoint bodies map string keys to mixed values, so the Any is
  real and now written down. The OpenAPI document is unchanged.
- Rom query filters take and return RomSelect; _music_facet_joins and
  _exclude_hidden are generic over the Select they're given, and
  _build_hash_selects returns id-only selects.
- Redis clients are typed by what they return: the decoding caches are
  Redis[str], the RQ connection and binary cache Redis[bytes].
- The rest get explicit parameters: Row, Pattern, ColumnElement,
  QueryableAttribute, SpooledTemporaryFile, Task, Future and friends.

Precise types reached callers that had been getting Any, which surfaced:
- The LaunchBox cache writer is fed binary-cache pipelines.
- The flashpoint and IGDB request helpers return raw JSON of varying
  shape, not always a dict.
- The rom search endpoint returned dicts under a list[SearchRomSchema]
  annotation; it now validates them into that schema.
- The audit filter's id subquery selects strings.
- In tests the binary cache is the non-decoding fake shared with
  async_cache, so it is cast to Redis[bytes] rather than changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

* test(upload): assert cleanup against the patched upload directory

The 409 test checked config's ROM_UPLOAD_TMP_BASE while the fixture patches
the endpoint's copy to tmp_path/uploads, so it passed even if cleanup failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

* chore(mypy): keep the row type through the ROM filter helpers

filter_roms and its helpers also receive select(Rom.id) membership queries,
so they now take and return the caller's Select type instead of narrowing to
RomSelect. merged_ra_metadata mixes scalar and list values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 16:32:29 -05:00

435 lines
15 KiB
Python

import asyncio
import re
from pathlib import Path
from typing import Annotated, Any
from fastapi import Body, File, HTTPException, Request, UploadFile, status
from fastapi.responses import FileResponse
from pydantic import BaseModel as PydanticBaseModel
from decorators.auth import protected_route
from endpoints.responses.memory_cards import (
MemoryCardSchema,
MemoryCardVersionSchema,
UserMemoryCardSchema,
)
from handler.auth.constants import Scope
from handler.database import db_memory_card_handler, db_platform_handler
from handler.filesystem import fs_asset_handler
from handler.filesystem.assets_handler import build_asset_file_response
from logger.formatter import highlight as hl
from logger.logger import log
from models.assets import MemoryCard, MemoryCardVersion
from utils.memory_cards import (
MEMORY_CARD_MAX_BYTES,
UnsafeCardArchive,
assert_card_archive_safe,
store_memory_card_version,
)
from utils.router import APIRouter
from utils.uploads import check_asset_upload_size
router = APIRouter(
prefix="/memory-cards",
tags=["memory-cards"],
)
MEMORY_CARD_FILE_UPLOAD = File(..., description="Memory card archive to upload.")
# The emulator name is a folder under the user's memory_cards directory, so it
# is held to what a folder may be called rather than to any list of emulators.
EMULATOR_NAME_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9 ._-]*$")
class MemoryCardCreatePayload(PydanticBaseModel):
name: str
emulator: str
# Loose display hint only; never scopes lookup (see MemoryCard model).
platform_id: int | None = None
is_public: bool = False
def _card_or_404(card_id: int, user_id: int) -> MemoryCard:
"""Fetch a card the caller may read: their own, or another user's public
one. Everything else is a 404 (never reveal a private card exists)."""
card = db_memory_card_handler.get_card_by_id(card_id)
if not card or (card.user_id != user_id and not card.is_public):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Memory card with ID {card_id} not found",
)
return card
def _owned_card_or_404(card_id: int, user_id: int) -> MemoryCard:
"""Fetch a card the caller owns, for mutations. A card owned by someone
else is a 404, matching how states scope writes to the owner."""
card = db_memory_card_handler.get_card(user_id=user_id, id=card_id)
if not card:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Memory card with ID {card_id} not found",
)
return card
def _apply_card_update(card_id: int, data: dict[str, Any]) -> MemoryCard:
"""Write a field update, treating a card deleted underneath us as a 404."""
updated = db_memory_card_handler.update_card(card_id, data)
if updated is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Memory card not found"
)
return updated
@protected_route(router.post, "", [Scope.ASSETS_WRITE])
def add_memory_card(
request: Request,
payload: MemoryCardCreatePayload,
) -> MemoryCardSchema:
"""Create an empty memory card. It hydrates onto a container at the next
streaming claim; its data accrues as versions on save-and-exit."""
name = payload.name.strip()
emulator = payload.emulator.strip()
if not name or not emulator:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Both name and emulator are required",
)
if not EMULATOR_NAME_RE.match(emulator):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Not a valid emulator name: {emulator}",
)
if payload.platform_id is not None and not db_platform_handler.get_platform(
payload.platform_id
):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Platform with ID {payload.platform_id} not found",
)
card = db_memory_card_handler.add_card(
MemoryCard(
user_id=request.user.id,
emulator=emulator,
platform_id=payload.platform_id,
name=name,
slot=1,
is_public=payload.is_public,
)
)
log.info(f"Created memory card {hl(name)} [{emulator}] for {request.user.username}")
return MemoryCardSchema.model_validate(card)
@protected_route(router.get, "", [Scope.ASSETS_READ])
def get_memory_cards(
request: Request, emulator: str | None = None
) -> list[MemoryCardSchema]:
"""The caller's own cards, newest-synced first, optionally one emulator."""
cards = db_memory_card_handler.get_cards(request.user.id, emulator)
return [MemoryCardSchema.model_validate(card) for card in cards]
@protected_route(router.get, "/shared", [Scope.ASSETS_READ])
def get_shared_memory_cards(
request: Request, emulator: str
) -> list[UserMemoryCardSchema]:
"""Cards for an emulator visible to the caller: their own plus other users'
public ones. Browsing and download only: mounting a card at claim is
owner-scoped, so the picker lists the caller's own cards instead."""
cards = db_memory_card_handler.get_shared_cards(
emulator=emulator, user_id=request.user.id
)
return [
UserMemoryCardSchema.model_validate(
{
**MemoryCardSchema.model_validate(card).model_dump(),
"username": card.user.username,
"user_avatar_path": card.user.avatar_path,
"user_updated_at": card.user.updated_at,
}
)
for card in cards
]
# The expansion check decompresses what the uploader sent, so it runs off the
# loop. The executor it lands in is the one every other blocking call in the
# process shares, hence the cap on how many uploads may occupy it at once.
_ARCHIVE_CHECKS = asyncio.Semaphore(2)
async def _assert_safe_archive(content: bytes) -> None:
"""The shared archive check, as a 400. Covers the whole gate: readable zip,
no escaping paths, no symlinks, no runaway expansion.
"""
try:
async with _ARCHIVE_CHECKS:
await asyncio.to_thread(assert_card_archive_safe, content)
except UnsafeCardArchive as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Memory card archive rejected, {exc}",
) from None
def _version_file_or_404(version: MemoryCardVersion) -> Path:
try:
file_path = fs_asset_handler.validate_path(version.full_path)
except ValueError:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Memory card 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="Memory card file not found on disk",
)
return file_path
@protected_route(
router.get,
"/versions/{id}/content",
[Scope.ASSETS_READ],
responses={status.HTTP_404_NOT_FOUND: {}},
)
def download_memory_card_version(request: Request, id: int) -> FileResponse:
"""Download a version's card archive. Readable if the caller owns the parent
card or it is public."""
version = db_memory_card_handler.get_version_by_id(id)
if not version:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Memory card version with ID {id} not found",
)
# Reuse the card visibility check on the parent.
_card_or_404(version.memory_card_id, request.user.id)
return build_asset_file_response(
_version_file_or_404(version), filename=version.file_name
)
@protected_route(
router.get,
"/{id}/content",
[Scope.ASSETS_READ],
responses={status.HTTP_404_NOT_FOUND: {}},
)
def download_memory_card(request: Request, id: int) -> FileResponse:
"""Download the card as it stands now, without going through its history.
This is the newest version, which is also what the next claim hydrates onto
a container, so what comes down here is what the emulator would boot with.
A card that has never been synced has nothing to serve and 404s."""
_card_or_404(id, request.user.id)
version = db_memory_card_handler.get_latest_version(id)
if not version:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Memory card has no stored data yet",
)
return build_asset_file_response(
_version_file_or_404(version), filename=version.file_name
)
@protected_route(
router.post,
"/{id}/versions",
[Scope.ASSETS_WRITE],
responses={
status.HTTP_400_BAD_REQUEST: {},
status.HTTP_404_NOT_FOUND: {},
status.HTTP_413_CONTENT_TOO_LARGE: {},
},
)
async def upload_memory_card_version(
request: Request,
id: int,
cardFile: UploadFile = MEMORY_CARD_FILE_UPLOAD,
) -> MemoryCardVersionSchema:
"""Store a card image the user supplied as the card's newest version, which
is what the next claim hydrates onto the container (owner only).
Only the zip layout the broker exchanges is accepted. A bare card image
(`.ps2`, `.raw`) is refused rather than stored, because nothing downstream
would notice until hydrate pushed it and the emulator rejected the card.
"""
check_asset_upload_size(cardFile, "Memory card file")
card = _owned_card_or_404(id, request.user.id)
content = await cardFile.read(MEMORY_CARD_MAX_BYTES + 1)
if len(content) > MEMORY_CARD_MAX_BYTES:
raise HTTPException(
status_code=status.HTTP_413_CONTENT_TOO_LARGE,
detail=(
f"Memory card exceeds the maximum size of "
f"{MEMORY_CARD_MAX_BYTES} bytes"
),
)
await _assert_safe_archive(content)
# The version this call wrote, not the card's latest: a teardown evacuating
# the same card alongside the upload would make the latest describe a
# snapshot the uploader never sent.
version = await store_memory_card_version(
request.user, card, content, deduplicate=False
)
if version is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Memory card upload was not stored",
)
log.info(f"Uploaded memory card {hl(card.name)} [{card.emulator}]")
return MemoryCardVersionSchema.model_validate(version)
@protected_route(router.get, "/{id}", [Scope.ASSETS_READ])
def get_memory_card(request: Request, id: int) -> MemoryCardSchema:
"""A single card: the caller's own or another user's public one."""
card = _card_or_404(id, request.user.id)
return MemoryCardSchema.model_validate(card)
def _reconcile_missing(version: MemoryCardVersion) -> bool:
"""Whether this version's archive is gone, persisting the answer.
Nothing else writes `missing_from_fs`, and the history is the only place a
user can see that a snapshot is unrecoverable before clicking download, so
the listing is where the flag is brought back in line with the disk.
"""
try:
path = fs_asset_handler.validate_path(version.full_path)
missing = not path.is_file()
except ValueError, OSError:
missing = True
if missing != version.missing_from_fs:
db_memory_card_handler.set_version_missing(version.id, missing)
return missing
@protected_route(router.get, "/{id}/versions", [Scope.ASSETS_READ])
def get_memory_card_versions(
request: Request, id: int
) -> list[MemoryCardVersionSchema]:
"""A card's snapshot history, newest first."""
_card_or_404(id, request.user.id)
versions = db_memory_card_handler.get_versions(id)
return [
MemoryCardVersionSchema.model_validate(v).model_copy(
update={"missing_from_fs": _reconcile_missing(v)}
)
for v in versions
]
@protected_route(
router.put,
"/{id}",
[Scope.ASSETS_WRITE],
responses={status.HTTP_404_NOT_FOUND: {}},
)
def rename_memory_card(
request: Request,
id: int,
name: Annotated[str, Body(embed=True)],
) -> MemoryCardSchema:
"""Rename a card (owner only)."""
_owned_card_or_404(id, request.user.id)
cleaned = name.strip()
if not cleaned:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Name cannot be empty",
)
return MemoryCardSchema.model_validate(_apply_card_update(id, {"name": cleaned}))
@protected_route(
router.put,
"/{id}/visibility",
[Scope.ASSETS_WRITE],
responses={status.HTTP_404_NOT_FOUND: {}},
)
def update_memory_card_visibility(
request: Request,
id: int,
is_public: Annotated[bool, Body(embed=True)],
) -> MemoryCardSchema:
"""Toggle a card's public/private visibility (owner only). Sharing is
one-way: a recipient's writes go to their own card, never back to this one."""
_owned_card_or_404(id, request.user.id)
return MemoryCardSchema.model_validate(
_apply_card_update(id, {"is_public": is_public})
)
@protected_route(
router.post,
"/delete",
[Scope.ASSETS_WRITE],
responses={
status.HTTP_400_BAD_REQUEST: {},
status.HTTP_404_NOT_FOUND: {},
},
)
async def delete_memory_cards(
request: Request,
cards: Annotated[
list[int],
Body(
description="List of memory card ids to delete.",
embed=True,
),
],
) -> list[int]:
"""Delete cards the caller owns, with their version files. Versions cascade
in the database; their on-disk archives are removed here."""
if not cards:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="No memory cards were provided",
)
# Resolve every card up front: deletion is irreversible and spans one
# transaction per card, so a bad id in the batch must fail before the
# first archive is removed rather than half way through.
owned = [
(card_id, _owned_card_or_404(card_id, request.user.id)) for card_id in cards
]
for card_id, card in owned:
# The delete reports the archives that went with it, so the removal list
# cannot miss a version written while the batch was running. A file that
# will not budge (permissions, a locked mount) must not abort the batch:
# the rest of the cards would be left untouched with nothing to tell the
# caller how far it got. An orphaned archive is recoverable, a
# half-deleted batch is not.
for path in db_memory_card_handler.delete_card(card_id):
try:
await fs_asset_handler.remove_file(file_path=path)
except FileNotFoundError:
log.warning(f"Memory card file {hl(path)} already gone from disk")
except OSError as exc:
log.error(
f"Could not remove memory card file {hl(path)}, "
f"leaving it orphaned: {exc}"
)
log.info(f"Deleted memory card {hl(card.name)} [{card.emulator}]")
return cards