mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 18:11:16 +01:00
fixes
This commit is contained in:
@@ -194,6 +194,13 @@ async def add_save(
|
||||
if slot:
|
||||
actual_filename = _apply_datetime_tag(sanitized_save_filename)
|
||||
|
||||
saves_path = fs_asset_handler.build_saves_file_path(
|
||||
user=request.user,
|
||||
platform_fs_slug=rom.platform.fs_slug,
|
||||
rom_id=rom.id,
|
||||
emulator=emulator,
|
||||
)
|
||||
|
||||
db_save = db_save_handler.get_save_by_filename(
|
||||
user_id=request.user.id, rom_id=rom.id, file_name=actual_filename, slot=slot
|
||||
)
|
||||
@@ -231,13 +238,6 @@ async def add_save(
|
||||
f"Uploading save {hl(actual_filename)} for {hl(str(rom.name), color=BLUE)}"
|
||||
)
|
||||
|
||||
saves_path = fs_asset_handler.build_saves_file_path(
|
||||
user=request.user,
|
||||
platform_fs_slug=rom.platform.fs_slug,
|
||||
rom_id=rom.id,
|
||||
emulator=emulator,
|
||||
)
|
||||
|
||||
await fs_asset_handler.write_file(
|
||||
file=saveFile, path=saves_path, filename=actual_filename
|
||||
)
|
||||
@@ -266,6 +266,21 @@ async def add_save(
|
||||
existing_by_hash, _syncs_for_save(existing_by_hash.id, device), device
|
||||
)
|
||||
|
||||
if db_save is None:
|
||||
# The write above landed on saves_path/actual_filename, whose bytes can
|
||||
# already belong to a save in another slot (file identity is path+name,
|
||||
# independent of slot). If that row's recorded hash no longer matches
|
||||
# what we just wrote, refresh it instead of inserting a duplicate, so it
|
||||
# does not keep reporting metadata that mismatches the bytes on disk.
|
||||
colliding_save = db_save_handler.get_save_by_path(
|
||||
user_id=request.user.id,
|
||||
rom_id=rom.id,
|
||||
file_path=scanned_save.file_path,
|
||||
file_name=actual_filename,
|
||||
)
|
||||
if colliding_save and colliding_save.content_hash != scanned_save.content_hash:
|
||||
db_save = colliding_save
|
||||
|
||||
if db_save:
|
||||
# Track where the bytes actually landed: the file is written under a
|
||||
# path derived from this request's emulator, which may differ from the
|
||||
@@ -282,13 +297,21 @@ async def add_save(
|
||||
update_data["slot"] = slot
|
||||
db_save = db_save_handler.update_save(db_save.id, update_data)
|
||||
|
||||
# The row moved to a new path, so the previously stored bytes are now
|
||||
# orphaned. Remove them to avoid leaving stale save files on disk.
|
||||
# The row moved to a new path, so the previously stored bytes may now
|
||||
# be orphaned. Slot is not part of the on-disk location, so another
|
||||
# row can still point at the old path; only unlink it if none do.
|
||||
if stale_full_path != db_save.full_path:
|
||||
try:
|
||||
await fs_asset_handler.remove_file(stale_full_path)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
still_referenced = any(
|
||||
other.id != db_save.id and other.full_path == stale_full_path
|
||||
for other in db_save_handler.get_saves(
|
||||
user_id=request.user.id, rom_id=rom.id
|
||||
)
|
||||
)
|
||||
if not still_referenced:
|
||||
try:
|
||||
await fs_asset_handler.remove_file(stale_full_path)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
else:
|
||||
scanned_save.rom_id = rom.id
|
||||
scanned_save.user_id = request.user.id
|
||||
|
||||
@@ -50,6 +50,26 @@ class DBSavesHandler(DBBaseHandler):
|
||||
query = query.filter(Save.slot.is_(None))
|
||||
return session.scalars(query.limit(1)).first()
|
||||
|
||||
@begin_session
|
||||
def get_save_by_path(
|
||||
self,
|
||||
user_id: int,
|
||||
rom_id: int,
|
||||
file_path: str,
|
||||
file_name: str,
|
||||
session: Session = None, # type: ignore
|
||||
) -> Save | None:
|
||||
# A save's on-disk identity is (file_path, file_name), independent of
|
||||
# slot. Used to find whichever row owns the bytes an upload is about to
|
||||
# overwrite.
|
||||
return session.scalars(
|
||||
select(Save)
|
||||
.filter_by(
|
||||
rom_id=rom_id, user_id=user_id, file_path=file_path, file_name=file_name
|
||||
)
|
||||
.limit(1)
|
||||
).first()
|
||||
|
||||
@begin_session
|
||||
def get_save_by_content_hash(
|
||||
self,
|
||||
|
||||
@@ -2682,6 +2682,53 @@ class TestSlotScopedDedupeMatrix:
|
||||
assert second.json()["slot"] == "slot2"
|
||||
assert second.json()["content_hash"] == first.json()["content_hash"]
|
||||
|
||||
def test_slotless_upload_over_named_slot_file_refreshes_that_row(
|
||||
self,
|
||||
client,
|
||||
access_token: str,
|
||||
rom: Rom,
|
||||
_isolated_assets_dir,
|
||||
):
|
||||
"""A slot-less upload that lands on a named slot's file (same emulator
|
||||
and filename) must not leave that row reporting a stale hash.
|
||||
|
||||
File identity is path+name, independent of slot, so writing new bytes
|
||||
there overwrites the named slot's file. The colliding row is refreshed
|
||||
in place rather than shadowed by a divergent null-slot duplicate.
|
||||
"""
|
||||
slotted = self._upload(
|
||||
client, access_token, rom, _build_fixture_a_zip(), slot="slot1"
|
||||
)
|
||||
assert slotted.status_code == status.HTTP_200_OK
|
||||
shared_name = slotted.json()["file_name"]
|
||||
|
||||
# Slot-less upload (slot param omitted) reusing the slot's on-disk
|
||||
# filename, but with new bytes.
|
||||
slotless = client.post(
|
||||
f"/api/saves?rom_id={rom.id}&emulator=test_emulator",
|
||||
files={
|
||||
"saveFile": (
|
||||
shared_name,
|
||||
BytesIO(_build_fixture_b_zip()),
|
||||
"application/octet-stream",
|
||||
)
|
||||
},
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
)
|
||||
assert slotless.status_code == status.HTTP_200_OK
|
||||
assert slotless.json()["id"] == slotted.json()["id"]
|
||||
assert slotless.json()["content_hash"] == FIXTURE_B_HASH
|
||||
|
||||
# No divergent second row was created for the same file.
|
||||
listing = client.get(
|
||||
f"/api/saves?rom_id={rom.id}",
|
||||
headers={"Authorization": f"Bearer {access_token}"},
|
||||
)
|
||||
assert listing.status_code == status.HTTP_200_OK
|
||||
rows = [s for s in listing.json() if s["file_name"] == shared_name]
|
||||
assert len(rows) == 1
|
||||
assert rows[0]["content_hash"] == FIXTURE_B_HASH
|
||||
|
||||
def test_different_bytes_same_slot_creates_distinct_records(
|
||||
self,
|
||||
client,
|
||||
|
||||
Reference in New Issue
Block a user