Files
romm/backend/handler/rom_upload.py
zurdi f9f68dc0f1 Unify manual file uploads into a ROM across the Files and Media tabs
The Files tab streamed chunks through /roms/upload/* and registered the
result with the scanner listing, while each Media route (soundtracks,
screenshots, manual files, walkthrough files) carried its own copy of the
sanitize, promote, mkdir, stream, stat and DB upsert sequence. Those copies
crashed with FileNotFoundError on a file name outside Latin-1: the browser
sends the header as Latin-1 and the multipart field name as UTF-8, so the
parser target registered under the header name never received the part
and os.stat failed after the folder had been created.

handler/rom_upload.py now owns the shared ingestion (resolve and prepare
the destination, stage next to the target, move into place, refresh the
ROM's file rows through the scanner) and both the chunked completion and a
new receive_rom_file sit on it. The media routes only check the extension
and delegate; a body naming the part differently is a 400. /start gains an
optional body filename that wins over the header, so names outside Latin-1
arrive intact, and rom_id becomes optional in that body.

On the v2 frontend a single useRomFileUpload composable wraps the chunked
upload, the single-file conversion prompt, the toasts and the refetch; the
Files tab, Soundtrack panel, Screenshots subtab, Walkthrough subtab and the
manual "add to ROM folder" target all call it with their folder. The
service sends the real name in the body and a percent-encoded one in the
header. The v2-only uploadScreenshots/uploadWalkthroughFiles service
functions and the soundtrack-only toasts are gone. Media subtabs now
refuse an existing file name (409) like the Files tab, instead of
overwriting it silently.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X1FgnCzXjM3rzkxggi7g6C
2026-09-16 15:29:22 +02:00

206 lines
7.2 KiB
Python

"""One ingestion path for every file uploaded into a ROM's folder, whether the
bytes arrive in chunks or as a single multipart request."""
import os
from dataclasses import dataclass
from pathlib import Path
from uuid import uuid4
from config import ROM_UPLOAD_ASSEMBLING_EXT
from handler.filesystem import fs_rom_handler
from handler.rom_conversion import promote_single_file_to_folder
from handler.rom_files import refresh_rom_files
from logger.logger import log
from models.rom import Rom, RomFileCategory
# The folder each media route uploads into. The scanner maps these names back to
# the category, so a file landing here is registered the same way a scan would.
CATEGORY_UPLOAD_FOLDERS: dict[RomFileCategory, str] = {
RomFileCategory.MANUAL: "manual",
RomFileCategory.WALKTHROUGH: "walkthrough",
RomFileCategory.SCREENSHOT: "screenshots",
RomFileCategory.SOUNDTRACK: "soundtrack",
}
class UploadRejectedException(ValueError):
"""The upload names a file or folder the ROM's folder cannot take."""
class UploadConflictException(FileExistsError):
"""A file of that name is already in the ROM's folder."""
class UploadNotRegisteredException(Exception):
"""The file is on disk but the ROM's file rows could not be refreshed."""
@dataclass(frozen=True)
class UploadDestination:
"""Where an upload lands: the ROM it belongs to (none for a platform folder
upload), the library-relative directory and the absolute file path."""
rom: Rom | None
rel_dir: str
location: Path
def sanitize_upload_filename(filename: str) -> str:
"""A plain file name, or UploadRejectedException when it carries a path."""
try:
safe_filename = fs_rom_handler._sanitize_filename(filename)
except ValueError as exc:
raise UploadRejectedException(f"Invalid upload filename: {exc}") from exc
if safe_filename != filename:
raise UploadRejectedException(
"Upload filename must be a plain file name, not a path"
)
return safe_filename
def parse_upload_folder(folder: str) -> str:
"""Normalize the target subfolder to a relative path made of plain names."""
raw = folder.strip()
if raw.startswith("/") or "\\" in raw:
raise UploadRejectedException(
"Upload folder must be a relative, forward-slashed path"
)
raw = raw.rstrip("/")
if not raw:
return ""
segments = raw.split("/")
for segment in segments:
try:
safe_segment = fs_rom_handler._sanitize_filename(segment)
except ValueError as exc:
raise UploadRejectedException(f"Invalid upload folder: {exc}") from exc
if safe_segment != segment:
raise UploadRejectedException(
"Upload folder must be a relative path inside the game folder"
)
return "/".join(segments)
def resolve_upload_destination(
rom: Rom, folder: str, filename: str, *, overwrite: bool = False
) -> tuple[str, Path]:
"""The library-relative directory and absolute file path of an upload into a
ROM, kept inside the folder a lone file gets promoted into.
Raises:
UploadRejectedException: The destination is unusable or the scanner
would never register the file.
UploadConflictException: A file of that name is already there (or will
be, once promoted), unless `overwrite` allows replacing it.
"""
if fs_rom_handler.is_excluded_multi_part(filename):
raise UploadRejectedException(
f"File {filename} would be ignored by the scanner"
)
root = (
f"{rom.fs_path}/{rom.fs_name_no_ext}"
if rom.has_simple_single_file
else rom.full_path
)
rel_dir = f"{root}/{folder}" if folder else root
try:
root_location = fs_rom_handler.validate_path(root)
location = fs_rom_handler.validate_path(f"{rel_dir}/{filename}")
except ValueError as exc:
raise UploadRejectedException(str(exc)) from exc
if not location.is_relative_to(root_location):
raise UploadRejectedException(
"Upload destination must be inside the game folder"
)
promoted_over_itself = (
rom.has_simple_single_file and not folder and filename == rom.fs_name
)
replaceable = overwrite and location.is_file()
if promoted_over_itself or (location.exists() and not replaceable):
raise UploadConflictException(
f"File {filename} already exists in the game folder"
)
return rel_dir, location
async def prepare_upload_destination(
rom: Rom, folder: str, filename: str, *, overwrite: bool = False
) -> UploadDestination:
"""Resolve where the file lands, promote a lone file into its folder and
create the target directory.
Raises the same as `resolve_upload_destination`, plus
RomAlreadyExistsException when the promotion collides with a folder.
"""
rel_dir, location = resolve_upload_destination(
rom, folder, filename, overwrite=overwrite
)
if rom.has_simple_single_file:
rom = await promote_single_file_to_folder(rom)
await fs_rom_handler.make_directory(rel_dir)
return UploadDestination(rom=rom, rel_dir=rel_dir, location=location)
def staging_path(location: Path) -> Path:
"""A private sibling of the destination, so the final move is a rename on
the same filesystem."""
return location.with_name(
f".{location.name}.{uuid4().hex}.{ROM_UPLOAD_ASSEMBLING_EXT}"
)
def claim_destination(location: Path) -> None:
"""Create the final path exclusively, so two uploads racing for the same
name cannot overwrite each other's bytes."""
try:
os.close(os.open(location, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644))
except FileExistsError as exc:
raise UploadConflictException(f"File {location.name} already exists") from exc
def _move_into_place(location: Path, staged: Path, *, overwrite: bool) -> None:
claimed = False
try:
if not overwrite:
claim_destination(location)
claimed = True
staged.replace(location)
except BaseException:
staged.unlink(missing_ok=True)
if claimed:
# The claim is an empty placeholder the rename never replaced;
# left behind, a scan would ingest it as a zero-byte ROM.
location.unlink(missing_ok=True)
raise
async def commit_upload(
destination: UploadDestination, staged: Path, *, overwrite: bool = False
) -> None:
"""Move the staged bytes into place and register the file on its ROM.
The staged file is removed whatever the outcome.
Raises:
UploadConflictException: The name got taken while the bytes arrived.
UploadNotRegisteredException: The file is in place but the ROM's rows
could not be refreshed.
"""
_move_into_place(destination.location, staged, overwrite=overwrite)
log.info(f"Upload complete: {destination.location}")
if destination.rom is None:
return
try:
await refresh_rom_files(destination.rom)
except Exception as exc:
log.error(
f"Error registering uploaded file for ROM {destination.rom.id}",
exc_info=exc,
)
raise UploadNotRegisteredException(
"File uploaded but not registered yet, run a quick scan"
) from exc