Files
romm/backend/handler/streaming
Georges-Antoine Assi 63faff8883 fix(misc): Rename paths, rescan artwork, scan lookups and streaming teardown fixes (#5396)
* fix(roms): rewrite only the moved part of a renamed rom's file paths

Renaming a rom rewrote each of its files with
`str.replace(old fs_name, new fs_name)` on both the name and the path. On
disk, rename_fs_rom moves only `fs_path/old` to `fs_path/new`, so:

- a folder rom's inner file named after the folder (`Game (Disc 1).bin`
  in `Game/`) was renamed in the database but not on disk;
- an fs_name that also appears in the platform path (a rom named `n64`
  under `n64/roms`) rewrote the platform path too.

_moved_file_location mirrors the move instead: a single-file rom's name
changes, a folder rom's files keep their names under the renamed folder,
and anything else is left alone.

_refetch_changed_matches now returns the ids it refetched, so the
RetroAchievements badge download keys off that rather than repeating the
comparison, and the shared-media rematch uses the same _match_changed
check as the refetch loop.

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

* fix(scan): keep artwork a complete rescan has nothing to replace with

A complete rescan deleted the rom's cover, manual, screenshots and every
provider media folder before asking any provider, then reset the stored
paths. A provider that was down, rate limited or no longer had the art
left the rom with none.

The wipe is gone, and scan_rom no longer resets the path columns on a
complete rescan. The download step already replaces what changed:
covers, manuals and screenshots refetch when their URL differs, and
remove_stale_media drops provider media whose owner or URL changed. The
URLs and locks are still cleared, so whatever answers now can replace a
file; a file nothing replaces stays where it was.

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

* refactor(scan): move scan_rom's provider lookups onto a _RomLookups class

scan_rom defined 16 provider lookups as closures over its own locals
(scan type, platform, rom, rom_attrs, enabled sources, the attempted and
inconclusive sets), so none of them could be read or tested apart from
the 1,000-line function around them.

They are now methods of _RomLookups, a dataclass holding exactly what
they used to capture, along with the helpers they share (consults,
note_inconclusive, resolve_fetch and the cached match files). scan_rom
builds one instance and keeps its local names for the shared sets, so its
body is unchanged apart from the calls. SteamGridDB's lookup stays nested
because it reads the other providers' results.

scan_rom drops from 977 to 664 lines. Its own complexity is unchanged:
that comes from the merge after the lookups, not the lookups themselves.

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

* refactor(streaming): one teardown order for every way a session ends

A session could be torn down three ways, each spelling out its steps:

- teardown_released_session (owner release and single admin release)
- teardown_abandoned_session (stale takeover and the reaper)
- force_release_all's inline _teardown

The first two ran the same stop/playtime/activity/state/saves sequence
in two copies. The sweep differed in ways that mattered: it held no drain
marker while it worked, recorded the termination after the stop (so a
polling tab could miss it), marked the exit save pull pending only after
the stop, and deleted the key unconditionally, even if a newer claim had
replaced the one it read.

_stop_and_collect now holds the shared sequence, and both lifecycle
teardowns call it after their own opening (a drain marker for a release,
a staleness-checked marker for an abandoned session). The sweep runs
teardown_released_session for each session, like a single admin release.
With no container config left, it still records the termination, then
frees the key through the guarded release_own_session.

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

* fix: apply code-review findings (pr-ready step 2)

Record playtime and clear the activity entry when force-release-all frees
a session whose container config is gone, and let the PICO-8 cartridge
fallback see a cover a complete rescan keeps.

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

* refactor: apply simplify findings (pr-ready step 3)

Let _RomLookups.attempts() record the attempt it gates, drop scan_rom's
local aliases for the lookups' state, read the platform slug off the
lookups in fetch_launchbox_rom, seed the kept artwork paths into
rom_attrs for every rescan so the PICO-8 fallback reads them directly,
return None from _moved_file_location when a file did not move, and
share the playtime and activity steps through close_session_record.

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

* fix: address Greptile review on #5396

Drop a cover's other formats once a new one is stored, so a kept upload
in another extension can't shadow it, and move the rename path rule onto
FSRomsHandler next to rename_fs_rom.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 11:06:52 -04:00
..