mirror of
https://github.com/rommapp/romm.git
synced 2026-12-06 15:54:45 +01:00
refactor: simplify pass (pr-ready step 3)
Route add_save's save and screenshot names through sanitize_asset_filename and keep the before-commit note on the helper instead of at each call site. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
374b626f2e
commit
2dfde1fee5
3 files changed
+19
-32
No files matched your search
+17
-29
@@ -39,7 +39,6 @@ from models.device import Device
|
||||
from models.device_save_sync import DeviceSaveSync
|
||||
from utils.assets import normalize_asset_labels
|
||||
from utils.datetime import to_utc
|
||||
from utils.filesystem import check_filename_length, sanitize_filename
|
||||
from utils.router import APIRouter
|
||||
from utils.uploads import (
|
||||
check_asset_upload_size,
|
||||
@@ -239,36 +238,26 @@ async def add_save(
|
||||
status_code=status.HTTP_400_BAD_REQUEST, detail="Save file has no filename"
|
||||
)
|
||||
|
||||
try:
|
||||
sanitized_save_filename = sanitize_filename(saveFile.filename)
|
||||
actual_filename = sanitized_save_filename
|
||||
if slot:
|
||||
actual_filename = _apply_datetime_tag(sanitized_save_filename)
|
||||
# Checked after tagging: the tag adds 22 bytes.
|
||||
check_filename_length(actual_filename)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Invalid save filename: {str(exc)}",
|
||||
) from exc
|
||||
actual_filename = sanitize_asset_filename(saveFile.filename, "save")
|
||||
if slot:
|
||||
# Checked again because the tag adds 22 bytes.
|
||||
actual_filename = sanitize_asset_filename(
|
||||
_apply_datetime_tag(actual_filename), "save"
|
||||
)
|
||||
|
||||
# Checked before the save is stored, so a bad name cannot fail after commit.
|
||||
sanitized_screenshot_filename = ""
|
||||
if screenshotFile and screenshotFile.filename:
|
||||
try:
|
||||
sanitized_screenshot_filename = sanitize_filename(screenshotFile.filename)
|
||||
# Save.screenshot is matched by stem, so a slotted upload names the
|
||||
# screenshot after the tagged save whatever the client called it.
|
||||
if slot:
|
||||
save_stem, _ = os.path.splitext(actual_filename)
|
||||
_, screenshot_ext = os.path.splitext(sanitized_screenshot_filename)
|
||||
sanitized_screenshot_filename = f"{save_stem}{screenshot_ext}"
|
||||
check_filename_length(sanitized_screenshot_filename)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Invalid screenshot filename: {str(exc)}",
|
||||
) from exc
|
||||
sanitized_screenshot_filename = sanitize_asset_filename(
|
||||
screenshotFile.filename, "screenshot"
|
||||
)
|
||||
# Save.screenshot is matched by stem, so a slotted upload names the
|
||||
# screenshot after the tagged save whatever the client called it.
|
||||
if slot:
|
||||
save_stem, _ = os.path.splitext(actual_filename)
|
||||
_, screenshot_ext = os.path.splitext(sanitized_screenshot_filename)
|
||||
sanitized_screenshot_filename = sanitize_asset_filename(
|
||||
f"{save_stem}{screenshot_ext}", "screenshot"
|
||||
)
|
||||
|
||||
check_emulator_folder_name(emulator)
|
||||
|
||||
@@ -661,7 +650,6 @@ async def update_save(
|
||||
log.error(error)
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=error)
|
||||
|
||||
# Checked before the save is written, so a bad name cannot fail after commit.
|
||||
sanitized_screenshot_filename = (
|
||||
sanitize_asset_filename(screenshotFile.filename, "screenshot")
|
||||
if screenshotFile and screenshotFile.filename
|
||||
|
||||
@@ -90,7 +90,6 @@ async def add_state(
|
||||
)
|
||||
|
||||
sanitized_state_filename = sanitize_asset_filename(stateFile.filename, "state")
|
||||
# Checked before the state is stored, so a bad name cannot fail after commit.
|
||||
sanitized_screenshot_filename = (
|
||||
sanitize_asset_filename(screenshotFile.filename, "screenshot")
|
||||
if screenshotFile and screenshotFile.filename
|
||||
@@ -233,7 +232,6 @@ async def update_state(
|
||||
log.error(error)
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=error)
|
||||
|
||||
# Checked before the state is written, so a bad name cannot fail after commit.
|
||||
sanitized_screenshot_filename = (
|
||||
sanitize_asset_filename(screenshotFile.filename, "screenshot")
|
||||
if screenshotFile and screenshotFile.filename
|
||||
|
||||
@@ -5,7 +5,8 @@ from utils.filesystem import check_filename_length, sanitize_filename
|
||||
|
||||
|
||||
def sanitize_asset_filename(filename: str, label: str) -> str:
|
||||
"""`filename` made safe to write, or a 400 naming the `label` upload."""
|
||||
"""`filename` made safe to write, or a 400 naming the `label` upload, so a
|
||||
caller can reject a bad name before it stores anything."""
|
||||
try:
|
||||
sanitized = sanitize_filename(filename)
|
||||
check_filename_length(sanitized)
|
||||
|
||||
Reference in new issue
Block a user