Files
romm/backend/models/assets.py
Terri 8a1e6280b1 feat(saves): add save channels and snapshots (#5198)
* feat(saves): add save channels and snapshots

* feat(saves): close spec gaps in save channels and hash with sigil

* feat(saves): play snapshots in the v2 player, which pushes snapshots

* fix(saves): code review: authorization, retention losses, held-save sync, batched responses

* refactor(saves): simplify pass

* refactor(saves): review-polish pass

* docs(saves): humanizer pass

* feat(saves): run every EmulatorJS page on the v2 player

* feat(saves): per-user snapshot pins

* feat(sigil): store the identity a save restore needs and pin sigil 0f05f25

* fix(saves): meet type-aware lint in the v2 player

* feat(sigil): store N64 header and playlist title ids, pin sigil 0847fda

* feat(saves): convert saves for a core on download and list save layouts

* feat(saves): prune unreachable archival snapshots and empty detached channels on delete

* feat(saves): device sync from the token, held snapshots and shared-card pushes

* feat(saves): arc restores past abandoned snapshots and color channels by age

* fix(saves): code-review and security-audit fixes

* refactor(saves): simplify pass

* chore(saves): review-polish pass

* docs(saves): humanizer pass

* fix(sync): file RetroArch autosave versions as their channel's next snapshot

* fix(saves): address review findings and trim docstrings

* fix(saves): share-lock planned rows by id so PostgreSQL accepts the lock

* test(saves): stub the detached-channels fetch in SaveDataTab stories

* feat(saves): carry the full current snapshot in a 409, pin sigil 2071dff

* chore(sigil): pin 5d4ddb4, which reads .cue and .gdi sheets

* docs(saves): drop v2 wording now that one UI remains

* chore(sigil): pin 0190443, which reads PS2 raw BIN images and cue sheets

* fix(sigil): treat a file too short for its format as routine during scans

* chore(migrations): renumber save channel migrations after upstream's 0154

* fix(snapshots): a hardcore switch on unchanged content makes a new current

* fix(sync): decide a channel's slot from its history and the device's snapshot

* chore(docker): pin sigil at 794d99f for PSP Store release save prefixes

* refactor(db): squash the save channel migrations into 0161

Fold the slot linking (0162) and the rom_files sigil identity columns (0163)
into 0161_save_channels, so the feature ships as one revision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): close visibility and input gaps found in review (pr-ready step 2)

code-review --fix:
- negotiate_sync drops channel slots on ROMs hidden from the caller
- channel PATCH/attach/DELETE answer 404 when the channel's ROM is hidden
- a push copying a save from a hidden ROM answers 404
- _own_device applies require_token_device to bound client tokens
- adopting a legacy save syncs its sharing with the channel it joins

security-audit follow-ups:
- another user's detached public save or state is no longer downloadable
- profile and option values must be a single path segment
- companions are de-duplicated and capped at 8

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(saves): share channel and asset lookups, drop duplicates (pr-ready step 3)

simplify:
- readable_channel_or_404 and channels_on_files replace four copies of the
  channel 404 check and two copies of the file-fed channel listing
- readable_rom_asset_or_404 serves both public save and state downloads
- _own_device resolves through require_device; _clone_origin takes the source
- reuse _unprocessable, DESCRIPTOR_EXTENSIONS, SAVE_SLOT_MAX_LENGTH, uniqueBy
  and uuidv7; neutral forms default their required pattern to the allowed one
- RetroArch delete checks held rows in one query, a guarded upload lists the
  slot only without a channel head, and uploads are read off the event loop
- snapshotSession builds push requests once; player comment cleanups

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(saves): trim two docstrings to two lines (pr-ready step 4)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(saves): answer held RetroArch paths with 423, mirror slot labels

- RetroArch Cloud Sync answers 423 Locked for a PUT over a held unslotted save
  and for a DELETE where only held rows sit at the path, instead of 409 and 404
- channelLabelForSlot returns null for blank and state_ slots, as the backend's
  channel_label does, so the player files them under no channel either
- document that sharing a channel lets every user read and push into it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(docker): pin sigil at 85f88a8 for Switch .nup containers

* fix(snapshots): a save pushed in another shape or format makes a new current

* refactor(saves): drop the always-true SAVE_SYNC.SNAPSHOTS heartbeat flag

Nothing reads it, and the SAVE_SYNC block's presence already tells a
client the server speaks the snapshot API. Also trim the rom detail
channel field comments to one line each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(frontend): use uuidv7 for every client-generated id

Drop the randomToken wrapper and the notification inbox's Math.random tab
id, so uuidv7 is the one helper that works outside a secure context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(saves): trim redundant snapshot API surface, dead code and duplicate logic

API:
- Drop PUT /snapshots/{id}/devices/{device_id}; GET /snapshots/{id}?hold=true holds.
- GET /snapshots is history only (channel_id required); GET /channels serves currents.
- A snapshot carries channel_id instead of an embedded channel.
- POST /saves/{id}/content takes the same query parameters as the GET, plus container_path.

Backend:
- Remove the archival write path, clone.py, DBStatesHandler.get_state, the sigil
  identity-hash path, unused relationships and composite snapshot indexes.
- One held/frozen lookup, one bulk-delete guard, SQLAlchemy dialect upserts, and
  the shared reserve_version_name in the writer.
- The writer takes a visibility filter and checks the channel and parent itself.
- The restore handler picks per-game or merge; fewer wrapper types and tables.

Frontend:
- Channel delete uses the shared typed confirm; DeleteChannelDialog is gone.
- Held offline pushes go through a per-channel SnapshotSession.
- Remove the slot-based pending upload path.
- The timeline shares the fan's badge logic.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* refactor(saves): release archival backups through module functions

The saves and states handlers each built a private DBSnapshotsHandler to
call release_backup and refresh_backup_digests, since they can't import
the shared instance. Both only ever ran inside the caller's session, so
they are now module functions taking it, like channel_for_slot.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* refactor(saves): share one deleted-assets handler instance

The saves handler built its own DBDeletedAssetsHandler because it can't
import the package's instance. The instance is now built in
deleted_assets_handler.py and re-exported by the package, so both use it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 21:59:19 -05:00

387 lines
15 KiB
Python

from __future__ import annotations
import enum
import uuid
from functools import cached_property
from typing import TYPE_CHECKING, NamedTuple
from urllib.parse import quote
from sqlalchemy import BigInteger, Enum, ForeignKey, Index, String, Uuid
from sqlalchemy.orm import Mapped, mapped_column, relationship
from models.base import (
FILE_EXTENSION_MAX_LENGTH,
FILE_NAME_MAX_LENGTH,
FILE_PATH_MAX_LENGTH,
BaseModel,
FileNamePartsMixin,
compute_file_extension,
)
from utils.database import CustomJSON, ExactString
if TYPE_CHECKING:
from models.device_save_sync import DeviceSaveSync
from models.platform import Platform
from models.rom import Rom
from models.user import User
SAVE_SLOT_MAX_LENGTH = 255
# Where a client with no slot of its own files new progress.
AUTOSAVE_SLOT = "autosave"
# A slot's versions, newest last: pruning locks exactly these rows through it.
SAVE_SLOT_VERSIONS_INDEX = "ix_saves_rom_user_slot_updated"
EMULATOR_MAX_LENGTH = 50
MEMORY_CARD_NAME_MAX_LENGTH = 255
ASSET_LABEL_MAX_LENGTH = 255
ASSET_LABELS_MAX = 20
CONTENT_HASH_MAX_LENGTH = 32
EMULATOR_VERSION_MAX_LENGTH = 100
class SaveShape(enum.StrEnum):
"""How a save unit travels, as sigil packs it."""
SINGLE = "SINGLE"
MULTI = "MULTI"
FOLDER = "FOLDER"
class SaveFormat(enum.StrEnum):
NEUTRAL = "neutral"
NATIVE = "native"
class SaveLineage(NamedTuple):
"""One emulator's saves in one format, whose versions in a slot are pruned together."""
emulator: str | None
file_extension: str
@classmethod
def of(cls, emulator: str | None, file_name: str) -> SaveLineage:
return cls(emulator, compute_file_extension(file_name).lower())
class BaseAsset(FileNamePartsMixin, BaseModel):
__abstract__ = True
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
file_name: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
file_name_no_tags: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
file_name_no_ext: Mapped[str] = mapped_column(String(length=FILE_NAME_MAX_LENGTH))
file_extension: Mapped[str] = mapped_column(
String(length=FILE_EXTENSION_MAX_LENGTH)
)
file_path: Mapped[str] = mapped_column(String(length=FILE_PATH_MAX_LENGTH))
file_size_bytes: Mapped[int] = mapped_column(BigInteger(), default=0)
missing_from_fs: Mapped[bool] = mapped_column(default=False, nullable=False)
@cached_property
def full_path(self) -> str:
return f"{self.file_path}/{self.file_name}"
@cached_property
def download_path(self) -> str:
# Served by the per-type `/{id}/content` route. A rename keeps
# `updated_at`, so the name joins the cache key.
return (
f"/api/{self.__tablename__}/{self.id}/content"
f"?timestamp={self.updated_at}&name={quote(self.file_name)}"
)
class RomAsset(BaseAsset):
__abstract__ = True
rom_id: Mapped[int] = mapped_column(ForeignKey("roms.id", ondelete="CASCADE"))
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
class DetachedContentError(LookupError):
"""A save or state whose ROM was deleted reached a flow that needs the ROM."""
def __init__(self, asset: "ChannelContent"):
super().__init__(f"{asset.file_name} has no ROM")
class ChannelContent(RomAsset):
"""A save or state that sync files under a channel. Null is a backup the user manages."""
__abstract__ = True
# Deleting the ROM detaches the row; a rescan with the same file key reattaches it.
rom_id: Mapped[int | None] = mapped_column( # type: ignore[assignment]
ForeignKey("roms.id", ondelete="SET NULL")
)
if TYPE_CHECKING:
rom: Mapped[Rom | None]
@property
def attached_rom(self) -> Rom:
"""The ROM, for legacy flows, whose queries never load a detached row."""
if self.rom is None:
raise DetachedContentError(self)
return self.rom
@property
def attached_rom_id(self) -> int:
if self.rom_id is None:
raise DetachedContentError(self)
return self.rom_id
channel_id: Mapped[uuid.UUID | None] = mapped_column(
Uuid(), ForeignKey("channels.id", ondelete="SET NULL"), default=None
)
content_hash: Mapped[str | None] = mapped_column(
String(length=CONTENT_HASH_MAX_LENGTH), default=None
)
class Screenshot(RomAsset):
__tablename__ = "screenshots"
__table_args__ = (
# `Save.screenshot` / `State.screenshot` hit this once per rendered card.
Index("ix_screenshots_rom_user", "rom_id", "user_id"),
Index("idx_screenshots_public", "is_public"),
Index("ix_screenshots_save_id", "save_id", unique=True),
Index("ix_screenshots_state_id", "state_id", unique=True),
{"extend_existing": True},
)
# `is_gallery` distinguishes intentionally-uploaded gallery screenshots from
# the auto-captured save/state thumbnails that also live in this table.
# `is_public` mirrors RomNote: it lets other users browse a user's public
# screenshots (community). Both default false; save/state thumbnails keep the
# defaults, only the gallery upload endpoint sets `is_gallery=True`.
is_gallery: Mapped[bool] = mapped_column(default=False)
is_public: Mapped[bool] = mapped_column(default=False)
# The content row a thumbnail belongs to; both null on a gallery screenshot.
save_id: Mapped[int | None] = mapped_column(
ForeignKey("saves.id", ondelete="CASCADE"), default=None
)
state_id: Mapped[int | None] = mapped_column(
ForeignKey("states.id", ondelete="CASCADE"), default=None
)
rom: Mapped[Rom] = relationship(lazy="joined", back_populates="screenshots")
user: Mapped[User] = relationship(lazy="joined", back_populates="screenshots")
class Save(ChannelContent):
__tablename__ = "saves"
__table_args__ = (
Index("ix_saves_rom_user_hash", "rom_id", "user_id", "content_hash"),
Index("idx_saves_public", "is_public"),
Index(SAVE_SLOT_VERSIONS_INDEX, "rom_id", "user_id", "slot", "updated_at"),
Index("ix_saves_channel_updated", "channel_id", "updated_at"),
{"extend_existing": True},
)
emulator: Mapped[str | None] = mapped_column(String(length=EMULATOR_MAX_LENGTH))
# Exact, so the database pairs slots as sync negotiation does in Python.
slot: Mapped[str | None] = mapped_column(
ExactString(SAVE_SLOT_MAX_LENGTH), index=True
)
# `content_hash` without the clock member, so a clock tick alone reads as no change.
identity_hash: Mapped[str | None] = mapped_column(
String(length=CONTENT_HASH_MAX_LENGTH), default=None
)
shape: Mapped[SaveShape | None] = mapped_column(Enum(SaveShape), default=None)
format: Mapped[SaveFormat | None] = mapped_column(Enum(SaveFormat), default=None)
emulator_version: Mapped[str | None] = mapped_column(
String(length=EMULATOR_VERSION_MAX_LENGTH), default=None
)
# The libretro core that wrote the save; NULL for a standalone emulator or when unreported.
core: Mapped[str | None] = mapped_column(
String(length=EMULATOR_MAX_LENGTH), default=None
)
core_version: Mapped[str | None] = mapped_column(
String(length=EMULATOR_VERSION_MAX_LENGTH), default=None
)
origin_device_id: Mapped[str | None] = mapped_column(
String(length=255),
ForeignKey("devices.id", ondelete="SET NULL"),
default=None,
index=True,
)
# `is_public` mirrors Screenshot/RomNote: it lets other users browse and
# download a user's public saves (community). Defaults false (private).
is_public: Mapped[bool] = mapped_column(default=False)
# Owner-only annotations: favorites sort ahead of the rest, labels tell
# apart runs a filename and a timestamp cannot (a route, a seed, a run).
is_favorite: Mapped[bool] = mapped_column(default=False)
labels: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
rom: Mapped[Rom | None] = relationship(lazy="joined", back_populates="saves")
user: Mapped[User] = relationship(lazy="joined", back_populates="saves")
device_syncs: Mapped[list[DeviceSaveSync]] = relationship(
back_populates="save",
cascade="all, delete-orphan",
lazy="raise",
)
@property
def lineage(self) -> SaveLineage:
return SaveLineage.of(self.emulator, self.file_name)
@cached_property
def screenshot(self) -> Screenshot | None:
from handler.database import db_screenshot_handler
if self.rom_id is None:
return None
return db_screenshot_handler.get_screenshot(
rom_id=self.rom_id,
user_id=self.user_id,
file_name=self.file_name, # Match state filename against screenshot filename stem
file_name_no_ext=self.file_name_no_ext,
)
class State(ChannelContent):
__tablename__ = "states"
__table_args__ = (
Index("ix_states_rom_user", "rom_id", "user_id"),
Index("idx_states_public", "is_public"),
Index("ix_states_channel_id", "channel_id"),
{"extend_existing": True},
)
emulator: Mapped[str | None] = mapped_column(String(length=EMULATOR_MAX_LENGTH))
# `is_public` mirrors Screenshot/RomNote: it lets other users browse and
# download a user's public states (community). Defaults false (private).
is_public: Mapped[bool] = mapped_column(default=False)
# Owner-only annotations: favorites sort ahead of the rest, labels tell
# apart runs a filename and a timestamp cannot (a route, a seed, a run).
is_favorite: Mapped[bool] = mapped_column(default=False)
labels: Mapped[list[str] | None] = mapped_column(CustomJSON(), default=[])
# The disc mounted when this state was captured, so a resume can put the
# same one back. SET NULL rather than CASCADE: losing the file row must
# not take the player's save with it.
disc_file_id: Mapped[int | None] = mapped_column(
ForeignKey("rom_files.id", ondelete="SET NULL"),
nullable=True,
default=None,
index=True,
)
# The libretro core that wrote this RetroArch state. NULL is the platform's
# default core, which wrote every state stored before cores were recorded.
core: Mapped[str | None] = mapped_column(
String(length=EMULATOR_MAX_LENGTH), nullable=True, default=None
)
emulator_version: Mapped[str | None] = mapped_column(
String(length=EMULATOR_VERSION_MAX_LENGTH), default=None
)
core_version: Mapped[str | None] = mapped_column(
String(length=EMULATOR_VERSION_MAX_LENGTH), default=None
)
# The device that wrote the current content; NULL when the client named none.
origin_device_id: Mapped[str | None] = mapped_column(
String(length=255),
ForeignKey("devices.id", ondelete="SET NULL"),
default=None,
index=True,
)
rom: Mapped[Rom | None] = relationship(lazy="joined", back_populates="states")
user: Mapped[User] = relationship(lazy="joined", back_populates="states")
@property
def is_stream_capture(self) -> bool:
"""Whether a streaming session captured this state: only a stream records its core."""
return self.core is not None
@cached_property
def screenshot(self) -> Screenshot | None:
from handler.database import db_screenshot_handler
if self.rom_id is None:
return None
return db_screenshot_handler.get_screenshot(
rom_id=self.rom_id,
user_id=self.user_id,
file_name=self.file_name, # Match state filename against screenshot filename stem
file_name_no_ext=self.file_name_no_ext,
)
class MemoryCard(BaseModel):
"""A per-user, per-emulator memory card that follows the user across
streaming sessions. Unlike Save/State it is not tied to a single ROM: for
formats like the PCSX2 folder card one card holds every game's saves. The
card is an identity (name, owner); its actual data lives in `versions`,
a snapshot history mirroring how EmulatorJS keeps multiple Save rows.
"""
__tablename__ = "memory_cards"
__table_args__ = (
Index("ix_memory_cards_user_emulator", "user_id", "emulator"),
Index("ix_memory_cards_public", "is_public"),
{"extend_existing": True},
)
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
# `emulator` is the hard scoping key: a card is looked up by (user, emulator)
# at session claim, so one Dolphin card serves both GameCube and Wii roms.
emulator: Mapped[str] = mapped_column(String(length=EMULATOR_MAX_LENGTH))
# `platform_id` is a loose, nullable hint (which platform the card was
# created under) for display/filtering only. It never scopes the lookup, so
# a card stays visible across every platform its emulator drives.
platform_id: Mapped[int | None] = mapped_column(
ForeignKey("platforms.id", ondelete="SET NULL"),
default=None,
)
name: Mapped[str] = mapped_column(String(length=MEMORY_CARD_NAME_MAX_LENGTH))
# Only slot 1 is used today; kept so a future multi-slot layout needs no
# schema change.
slot: Mapped[int] = mapped_column(default=1)
# `is_public` mirrors Save/State, letting another user browse this card and
# hydrate a snapshot of it. Sharing is one-way: the recipient's writes go
# to their own new card, never back to this one.
is_public: Mapped[bool] = mapped_column(default=False)
user: Mapped[User] = relationship(lazy="joined", back_populates="memory_cards")
# One-directional: the loose platform hint, for display only.
platform: Mapped[Platform | None] = relationship(lazy="joined")
versions: Mapped[list[MemoryCardVersion]] = relationship(
back_populates="memory_card",
cascade="all, delete-orphan",
lazy="raise",
order_by="MemoryCardVersion.created_at.desc()",
)
class MemoryCardVersion(BaseAsset):
"""A single snapshot of a `MemoryCard`'s data (the whole card image, e.g.
the zipped PCSX2 folder card). Multiple versions per card form its history.
"""
__tablename__ = "memory_card_versions"
__table_args__ = (
Index("ix_memory_card_versions_card_hash", "memory_card_id", "content_hash"),
{"extend_existing": True},
)
memory_card_id: Mapped[int] = mapped_column(
ForeignKey("memory_cards.id", ondelete="CASCADE")
)
content_hash: Mapped[str | None] = mapped_column(
String(length=CONTENT_HASH_MAX_LENGTH)
)
memory_card: Mapped[MemoryCard] = relationship(
lazy="joined", back_populates="versions"
)
@cached_property
def download_path(self) -> str:
# Served under the memory-cards router rather than the default
# `/api/{tablename}/...`, keeping every card route in one namespace.
return (
f"/api/memory-cards/versions/{self.id}/content?timestamp={self.updated_at}"
)