mirror of
https://github.com/rommapp/romm.git
synced 2026-12-04 12:52:19 +01:00
feat(streaming): let resolve_save_archive fall through to a broker import check
This commit is contained in:
@@ -734,8 +734,9 @@ async def claim_session(
|
||||
# Same for the save pick: a save the player cannot restore here has to
|
||||
# fail before the container is reserved, not during the launch.
|
||||
picked_save = None
|
||||
save_foreign = False
|
||||
if req.save_id is not None:
|
||||
picked_save = saves.resolve_save_archive(
|
||||
picked_save, save_foreign = saves.resolve_save_archive(
|
||||
request.user.id, rom, reference, req.save_id
|
||||
)
|
||||
|
||||
|
||||
@@ -263,13 +263,22 @@ def _newest_restorable(user_id: int, rom_id: int, emulator: str) -> Save | None:
|
||||
return max(archives, key=lambda s: (s.created_at, s.id), default=None)
|
||||
|
||||
|
||||
def newest_restorable(user_id: int, rom_id: int, emulator: str) -> Save | None:
|
||||
"""Public alias of `_newest_restorable` for `imports.py`."""
|
||||
return _newest_restorable(user_id, rom_id, emulator)
|
||||
|
||||
|
||||
def resolve_save_archive(
|
||||
user_id: int, rom: Rom, container: ResolvedContainer, save_id: int
|
||||
) -> Save:
|
||||
"""Validate a pick from the launch screen's save list and return the save.
|
||||
) -> tuple[Save, bool]:
|
||||
"""Validate a pick from the launch screen's save list and return
|
||||
(save, is_foreign).
|
||||
|
||||
Raises 404 for a save that is not the claiming user's own on this ROM, and
|
||||
400 when it cannot be restored on this container.
|
||||
Raises 404 for a save that is not the claiming user's own on this ROM. A
|
||||
pick this emulator can restore natively resolves as (save, False). A
|
||||
pick it cannot is checked against the broker's own import-spec before
|
||||
being refused with 400, resolving as (save, True) when the broker says
|
||||
it would still take it as a declared import.
|
||||
"""
|
||||
save = db_save_handler.get_save(user_id=user_id, id=save_id)
|
||||
# Same 404 for another user's save and another ROM's, so neither leaks.
|
||||
@@ -281,17 +290,22 @@ def resolve_save_archive(
|
||||
status_code=400,
|
||||
detail="This emulator always restores the newest save",
|
||||
)
|
||||
if not _written_by(save, container.emulator):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Save was made by a different emulator",
|
||||
)
|
||||
if _is_restorable(save, container.emulator):
|
||||
return save, False
|
||||
|
||||
spec = webstation.import_spec(container, container.emulator, container.platform)
|
||||
if spec is not None and spec.accepts("save"):
|
||||
return save, True
|
||||
|
||||
if not _is_archive(save):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Save is not a restorable archive",
|
||||
)
|
||||
return save
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Save was made by a different emulator",
|
||||
)
|
||||
|
||||
|
||||
async def _read_archive(save: Save) -> tuple[str, bytes] | None:
|
||||
@@ -304,6 +318,11 @@ async def _read_archive(save: Save) -> tuple[str, bytes] | None:
|
||||
return save.file_name, content
|
||||
|
||||
|
||||
async def read_restorable_archive(save: Save) -> tuple[str, bytes] | None:
|
||||
"""Public alias of `_read_archive` for `imports.py`: (file name, bytes)."""
|
||||
return await _read_archive(save)
|
||||
|
||||
|
||||
async def hydrate_saves_to_broker(
|
||||
user_id: int, rom_id: int, container: ResolvedContainer
|
||||
) -> bool:
|
||||
|
||||
@@ -4957,10 +4957,11 @@ def test_resolve_save_archive_accepts_the_players_own_archive(
|
||||
archive = db_save_handler.add_save(
|
||||
_save_for(rom, admin_user, "Game [retroarch a].saves.zip", "retroarch", "h1")
|
||||
)
|
||||
resolved = saves.resolve_save_archive(
|
||||
resolved, is_foreign = saves.resolve_save_archive(
|
||||
admin_user.id, rom, _resolved(_clearing_webstation(rom)), archive.id
|
||||
)
|
||||
assert resolved.id == archive.id
|
||||
assert is_foreign is False
|
||||
|
||||
|
||||
def test_resolve_save_archive_rejects_a_save_that_is_not_the_players(
|
||||
@@ -5000,10 +5001,11 @@ def test_resolve_save_archive_rejects_another_emulators_archive(
|
||||
other = db_save_handler.add_save(
|
||||
_save_for(rom, admin_user, "Game [pcsx2 a].saves.zip", "pcsx2", "h1")
|
||||
)
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
saves.resolve_save_archive(
|
||||
admin_user.id, rom, _resolved(_clearing_webstation(rom)), other.id
|
||||
)
|
||||
with patch("handler.streaming.saves.webstation.import_spec", return_value=None):
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
saves.resolve_save_archive(
|
||||
admin_user.id, rom, _resolved(_clearing_webstation(rom)), other.id
|
||||
)
|
||||
assert exc.value.status_code == 400
|
||||
assert exc.value.detail == "Save was made by a different emulator"
|
||||
|
||||
@@ -5013,10 +5015,11 @@ def test_resolve_save_archive_rejects_a_bare_save_file(rom: Rom, admin_user: Use
|
||||
loose = db_save_handler.add_save(
|
||||
_save_for(rom, admin_user, "Game.srm", "retroarch", "h1")
|
||||
)
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
saves.resolve_save_archive(
|
||||
admin_user.id, rom, _resolved(_clearing_webstation(rom)), loose.id
|
||||
)
|
||||
with patch("handler.streaming.saves.webstation.import_spec", return_value=None):
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
saves.resolve_save_archive(
|
||||
admin_user.id, rom, _resolved(_clearing_webstation(rom)), loose.id
|
||||
)
|
||||
assert exc.value.status_code == 400
|
||||
assert exc.value.detail == "Save is not a restorable archive"
|
||||
|
||||
@@ -5037,6 +5040,42 @@ def test_resolve_save_archive_rejects_a_pick_where_it_would_not_land(
|
||||
assert exc.value.detail == "This emulator always restores the newest save"
|
||||
|
||||
|
||||
def test_resolve_save_archive_accepts_a_foreign_pick_the_broker_will_import(
|
||||
rom: Rom, admin_user: User
|
||||
):
|
||||
"""A pick that fails the native check is not turned away outright: it is
|
||||
checked against the broker's own import-spec first."""
|
||||
other = db_save_handler.add_save(
|
||||
_save_for(rom, admin_user, "Game [pcsx2 a].saves.zip", "pcsx2", "h1")
|
||||
)
|
||||
spec = webstation.ImportSpec(
|
||||
kinds=(webstation.ImportKindSpec("save", False, None),),
|
||||
state_channel="archive",
|
||||
state_slot=0,
|
||||
)
|
||||
with patch("handler.streaming.saves.webstation.import_spec", return_value=spec):
|
||||
save, is_foreign = saves.resolve_save_archive(
|
||||
admin_user.id, rom, _resolved(_clearing_webstation(rom)), other.id
|
||||
)
|
||||
assert save.id == other.id
|
||||
assert is_foreign is True
|
||||
|
||||
|
||||
def test_resolve_save_archive_still_refuses_when_the_broker_has_no_import_spec(
|
||||
rom: Rom, admin_user: User
|
||||
):
|
||||
other = db_save_handler.add_save(
|
||||
_save_for(rom, admin_user, "Game [pcsx2 a].saves.zip", "pcsx2", "h1")
|
||||
)
|
||||
with patch("handler.streaming.saves.webstation.import_spec", return_value=None):
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
saves.resolve_save_archive(
|
||||
admin_user.id, rom, _resolved(_clearing_webstation(rom)), other.id
|
||||
)
|
||||
assert exc.value.status_code == 400
|
||||
assert exc.value.detail == "Save was made by a different emulator"
|
||||
|
||||
|
||||
def test_claim_hydrates_the_picked_save(
|
||||
client, access_token, rom: Rom, admin_user: User
|
||||
):
|
||||
@@ -5075,6 +5114,7 @@ def test_claim_with_an_unrestorable_pick_never_reserves_a_container(
|
||||
with _streaming(_clearing_webstation(rom)):
|
||||
with (
|
||||
patch("handler.streaming.webstation.activate", activate),
|
||||
patch("handler.streaming.saves.webstation.import_spec", return_value=None),
|
||||
patch("handler.streaming.background.spawn_sync_task"),
|
||||
):
|
||||
refused = _claim(client, access_token, rom.id, save_id=loose.id)
|
||||
|
||||
Reference in New Issue
Block a user