mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 10:33:22 +01:00
`converto.platform_formats` now names the format a platform's library is stored in, and the new "Convert library" task brings every matched rom to it. Each output is staged in a `.romm_tmp_` directory beside the original, moved into place, and only then are the originals (and a cue's tracks) deleted. Single-file roms take the new name, folder roms keep theirs and get their .m3u entries rewritten, and the file rows refresh, so a rom keeps its id, saves and collections. Only lossless conversions qualify, so xbox (xiso trims the dump) has no library format. Unmatched roms are skipped because a converted file no longer hash-matches a DAT. `/api/config` now carries the library targets and, per input extension, the formats a download can be converted to. Co-Authored-By: DevYukine <20647088+DevYukine@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017BuuZVVtVPNLRaCjTN9bgQ
110 lines
3.9 KiB
Python
110 lines
3.9 KiB
Python
from exceptions.fs_exceptions import RomAlreadyExistsException
|
|
from handler.database import db_rom_handler
|
|
from handler.filesystem import fs_rom_handler
|
|
from handler.redis_handler import redis_lock
|
|
from logger.formatter import BLUE
|
|
from logger.formatter import highlight as hl
|
|
from logger.logger import log
|
|
from models.rom import Rom
|
|
|
|
# Scans and the watcher skip dot-prefixed entries, so staged files stay unseen.
|
|
STAGE_PREFIX = ".romm_tmp_"
|
|
|
|
# Parallel uploads each promote the ROM, maybe on different gunicorn workers.
|
|
# Keyed by the target folder, since lone files sharing a stem promote into one.
|
|
PROMOTION_LOCK_TIMEOUT_SECONDS = 600
|
|
|
|
|
|
def promotion_lock_key(rom: Rom) -> str:
|
|
return f"rom_promotion:{rom.fs_path}/{rom.fs_name_no_ext}"
|
|
|
|
|
|
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.
|
|
"""
|
|
async with redis_lock(
|
|
promotion_lock_key(rom), timeout_seconds=PROMOTION_LOCK_TIMEOUT_SECONDS
|
|
):
|
|
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
|
|
|
|
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
|