mirror of
https://github.com/rommapp/romm.git
synced 2026-12-04 12:52:19 +01:00
- Registering the tracks can fail after they were written. When every track landed, return 500 asking for a quick scan, as uploads do. When a track failed, log the registration error and keep the real one. - The playlist guard runs inside promote_single_file_to_folder, off the event loop and under the promotion lock, so every caller gets it, convert-to-folder included (409). /upload/start and the GameFAQs import still check it before any bytes or remote fetch. - GET /soundtracks/cd-audio returns `extractable`, false for a loose sheet or a lone disc an .m3u lists. The v2 Soundtrack subtab reads it, so hasDiscImage goes and a playlist-listed CHD no longer offers a button that then gets a 409. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ExPHX7HoorQFgf5nocz88d
121 lines
4.1 KiB
Python
121 lines
4.1 KiB
Python
import asyncio
|
|
|
|
from exceptions.fs_exceptions import (
|
|
RomAlreadyExistsException,
|
|
RomListedByPlaylistException,
|
|
)
|
|
from handler.database import db_rom_handler
|
|
from handler.filesystem import fs_rom_handler
|
|
from logger.formatter import BLUE
|
|
from logger.formatter import highlight as hl
|
|
from logger.logger import log
|
|
from models.rom import Rom
|
|
from utils.m3u import listing_playlist
|
|
|
|
_STAGE_PREFIX = ".romm_tmp_"
|
|
|
|
# Uploading several files fires a request per file, each promoting the same ROM.
|
|
_promotion_lock = asyncio.Lock()
|
|
|
|
|
|
def assert_promotable(rom: Rom) -> None:
|
|
"""Refuse to move a lone file into a folder when a playlist beside it lists it.
|
|
|
|
Raises:
|
|
RomListedByPlaylistException: An .m3u in the file's folder lists it.
|
|
"""
|
|
if not rom.has_simple_single_file:
|
|
return
|
|
playlist = listing_playlist(fs_rom_handler.validate_path(rom.full_path))
|
|
if playlist:
|
|
raise RomListedByPlaylistException(playlist)
|
|
|
|
|
|
async def promote_single_file_to_folder(rom: Rom) -> Rom:
|
|
"""Promote a simple single-file ROM to a folder ROM in place, keeping rom.id
|
|
and every relation. Idempotent; raises RomAlreadyExistsException on a
|
|
folder-name collision and RomListedByPlaylistException when an .m3u lists it.
|
|
"""
|
|
async with _promotion_lock:
|
|
return await _promote(db_rom_handler.get_rom(rom.id) or rom)
|
|
|
|
|
|
async def _promote(rom: Rom) -> Rom:
|
|
if not rom.has_simple_single_file:
|
|
return rom
|
|
await asyncio.to_thread(assert_promotable, rom)
|
|
|
|
folder = rom.fs_name_no_ext
|
|
fs_path = rom.fs_path
|
|
fs_name = rom.fs_name
|
|
origin = f"{fs_path}/{fs_name}"
|
|
dest_dir = f"{fs_path}/{folder}"
|
|
final = f"{dest_dir}/{fs_name}"
|
|
staged = f"{fs_path}/{_STAGE_PREFIX}{fs_name}"
|
|
extensionless = folder == fs_name
|
|
|
|
# Extensionless dest_dir is the file's own path; only a directory collides.
|
|
dest_path = fs_rom_handler.validate_path(dest_dir)
|
|
collision = dest_path.is_dir() if extensionless else dest_path.exists()
|
|
if collision:
|
|
raise RomAlreadyExistsException(folder)
|
|
|
|
# A promotion racing this one from another worker can win the move and own
|
|
# the folder, so undo only what this call did. Removal is recursive, and a
|
|
# folder with anything left in it holds someone else's ROM.
|
|
moved = False
|
|
|
|
async def _drop_dir() -> None:
|
|
path = fs_rom_handler.validate_path(dest_dir)
|
|
if path.is_dir() and not any(path.iterdir()):
|
|
await fs_rom_handler.remove_directory(dest_dir)
|
|
|
|
async def _restore() -> None:
|
|
"""Return the lone file to its original path and drop the new folder."""
|
|
src = next(
|
|
(
|
|
p
|
|
for p in (final, staged)
|
|
if moved and fs_rom_handler.validate_path(p).exists()
|
|
),
|
|
None,
|
|
)
|
|
if src is not None and extensionless:
|
|
if src != staged:
|
|
await fs_rom_handler.move_file_or_folder(src, staged)
|
|
await _drop_dir()
|
|
await fs_rom_handler.move_file_or_folder(staged, origin)
|
|
elif src is not None:
|
|
await fs_rom_handler.move_file_or_folder(src, origin)
|
|
await _drop_dir()
|
|
else:
|
|
await _drop_dir()
|
|
|
|
try:
|
|
if extensionless:
|
|
await fs_rom_handler.move_file_or_folder(origin, staged)
|
|
moved = True
|
|
await fs_rom_handler.make_directory(dest_dir)
|
|
await fs_rom_handler.move_file_or_folder(staged, final)
|
|
else:
|
|
await fs_rom_handler.make_directory(dest_dir)
|
|
await fs_rom_handler.move_file_or_folder(origin, final)
|
|
moved = True
|
|
db_rom_handler.convert_rom_to_folder(rom.id, folder, dest_dir)
|
|
except Exception:
|
|
try:
|
|
await _restore()
|
|
except Exception:
|
|
log.error(f"Failed to roll back folder conversion for ROM {rom.id}")
|
|
raise
|
|
|
|
refetched = db_rom_handler.get_rom(rom.id)
|
|
if refetched is None:
|
|
return rom
|
|
|
|
log.info(
|
|
f"Converted {hl(rom.name or 'ROM', color=BLUE)} [{hl(fs_name)}] "
|
|
f"to folder ROM [{hl(folder)}]"
|
|
)
|
|
return refetched
|