Files
romm/backend/endpoints/configs.py
HadNutz b1d7d2b57a feat(emulatorjs): expose download cache enable setting (#5737)
* feat(emulatorjs): expose download cache enable setting

* pr-ready: code-review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: review-polish

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(config): move cache_limit under emulatorjs.cache and read config.yml strictly

emulatorjs.cache.limit replaces emulatorjs.cache_limit, which is still read
with a deprecation warning and is rewritten to the new key on the next save.

Every config.yml read now goes through one reader that exits with code 3 when
a parent block is not a dictionary (e.g. `netplay: true`), where get_nested
silently fell back to the default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(emulatorjs): clear every EmulatorJS cache and keep nightly's cache size

The clear-cache dialog now also deletes EmulatorJS-bios (4.2.3) and
EmulatorJS-Cache (nightly), which kept serving downloaded files.

Nightly turns EJS_CacheLimit, a per-file byte cap in 4.2.3, into its total
cache size in MB, so a limit under 1 MiB gave a 0 MB cache. Passing
cacheMaxSizeMB explicitly keeps nightly at its own 4096 MB default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(utils): drop get_nested

The config loader reads config.yml through its own strict reader now, so
get_nested has no callers left.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 11:13:18 -05:00

456 lines
16 KiB
Python

from typing import Annotated, Any, Final
from fastapi import HTTPException
from fastapi import Path as PathVar
from fastapi import Request, status
from pydantic import Field, StringConstraints, field_validator
from adapters.services.rom_converto import (
LIBRARY_TARGETS_BY_PLATFORM,
normalize_platform_formats,
)
from config.config_manager import (
CONVERTO_INT_MINIMUMS,
DEFAULT_EXCLUDED_EXTENSIONS,
DEFAULT_EXCLUDED_FILES,
DEFAULT_EXCLUDED_MULTI_FILE_DIRS,
DEFAULT_EXCLUDED_PLATFORM_DIRS,
VALID_GAMELIST_IMAGE_TYPES,
VALID_GAMELIST_THUMBNAIL_TYPES,
VALID_SCAN_PRIORITY_SOURCES,
ConvertoConfig,
ExclusionType,
MetadataMediaType,
)
from config.config_manager import config_manager as cm
from decorators.auth import protected_route
from endpoints.responses.base import BaseModel
from endpoints.responses.config import ConfigResponse
from exceptions.config_exceptions import ConfigNotWritableException
from handler.audit_handler import AuditTarget, record
from handler.auth.constants import Scope
from handler.database import db_rom_handler
from handler.filesystem import fs_platform_handler
from logger.logger import log
from models.audit_event import AuditAction, AuditTargetType
from utils.router import APIRouter
router = APIRouter(
prefix="/config",
tags=["config"],
)
CONFIG_NOT_WRITABLE_RESPONSE: Final[dict[int | str, dict[str, Any]]] = {
status.HTTP_500_INTERNAL_SERVER_ERROR: {
"description": "The config file is not writable."
}
}
AMBIGUOUS_FOLDER_RESPONSE: Final[dict[int | str, dict[str, Any]]] = {
status.HTTP_409_CONFLICT: {
"description": (
"Several folders in the library have this name with different"
" letter case."
)
}
}
# config.yml refuses to load an empty mapping value, so one must never be written.
NonEmptySlug = Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)]
class PlatformBindingPayload(BaseModel):
fs_slug: NonEmptySlug
slug: NonEmptySlug
class ExclusionPayload(BaseModel):
exclusion_value: str
# Left a string rather than the enum to keep the generated frontend type one.
exclusion_type: str
@field_validator("exclusion_type")
@classmethod
def validate_exclusion_type(cls, value: str) -> str:
if value not in ExclusionType:
raise ValueError(
f"Unknown exclusion type, expected one of {[t.value for t in ExclusionType]}"
)
return value
class ScanSettingsPayload(BaseModel):
"""Full replacement of the scan.* config section.
The three artwork override lists (cover/screenshot/manual) are optional:
a null value clears the override so that field falls back to
`artwork_priority`.
"""
metadata_priority: list[str]
artwork_priority: list[str]
cover_priority: list[str] | None = None
screenshot_priority: list[str] | None = None
manual_priority: list[str] | None = None
region_priority: list[str]
language_priority: list[str]
media: list[MetadataMediaType]
gamelist_export: bool
gamelist_thumbnail: MetadataMediaType
gamelist_image: MetadataMediaType
pegasus_export: bool
@field_validator(
"metadata_priority",
"artwork_priority",
"cover_priority",
"screenshot_priority",
"manual_priority",
)
@classmethod
def validate_sources(cls, value: list[str] | None) -> list[str] | None:
if value is None:
return value
invalid = [s for s in value if s not in VALID_SCAN_PRIORITY_SOURCES]
if invalid:
raise ValueError(f"Unknown metadata source(s): {sorted(set(invalid))}")
# Drop duplicates while preserving priority order.
return list(dict.fromkeys(value))
@field_validator("region_priority", "language_priority")
@classmethod
def validate_codes(cls, value: list[str]) -> list[str]:
# Region/language codes are open sets (provider-defined), so we only
# normalize to lowercase, trim blanks, and drop duplicates.
cleaned = [code.strip().lower() for code in value if code and code.strip()]
return list(dict.fromkeys(cleaned))
@field_validator("gamelist_thumbnail")
@classmethod
def validate_thumbnail(cls, value: MetadataMediaType) -> MetadataMediaType:
if value not in VALID_GAMELIST_THUMBNAIL_TYPES:
raise ValueError(
f"Invalid gamelist thumbnail; valid options: "
f"{sorted(o.value for o in VALID_GAMELIST_THUMBNAIL_TYPES)}"
)
return value
@field_validator("gamelist_image")
@classmethod
def validate_image(cls, value: MetadataMediaType) -> MetadataMediaType:
if value not in VALID_GAMELIST_IMAGE_TYPES:
raise ValueError(
f"Invalid gamelist image; valid options: "
f"{sorted(o.value for o in VALID_GAMELIST_IMAGE_TYPES)}"
)
return value
class ConvertoSettingsPayload(BaseModel):
"""Runtime-editable converto.* settings; `cache_ttl_hours` and `scan_metadata` stay in config.yml."""
download_conversion_enabled: bool
cache_max_size_gb: int = Field(ge=CONVERTO_INT_MINIMUMS["cache_max_size_gb"])
platform_formats: dict[str, str]
@field_validator("platform_formats")
@classmethod
def validate_platform_formats(cls, value: dict[str, str]) -> dict[str, str]:
return normalize_platform_formats(value)
@router.get("")
def get_config(request: Request) -> ConfigResponse:
"""Get RomM's configuration. Anonymous callers get no config parse error and no netplay ICE servers, which can hold a file path or credentials."""
cfg = cm.get_config()
return ConfigResponse(
CONFIG_FILE_MOUNTED=cfg.CONFIG_FILE_MOUNTED,
CONFIG_FILE_WRITABLE=cfg.CONFIG_FILE_WRITABLE,
# Raw parser error may leak the config file path, so only send when authenticated
CONFIG_FILE_PARSE_ERROR=(
cfg.CONFIG_FILE_PARSE_ERROR if request.user.is_authenticated else None
),
EXCLUDED_PLATFORMS=cfg.EXCLUDED_PLATFORMS,
EXCLUDED_SINGLE_EXT=cfg.EXCLUDED_SINGLE_EXT,
EXCLUDED_SINGLE_FILES=cfg.EXCLUDED_SINGLE_FILES,
EXCLUDED_MULTI_FILES=cfg.EXCLUDED_MULTI_FILES,
EXCLUDED_MULTI_PARTS_EXT=cfg.EXCLUDED_MULTI_PARTS_EXT,
EXCLUDED_MULTI_PARTS_FILES=cfg.EXCLUDED_MULTI_PARTS_FILES,
DEFAULT_EXCLUDED_PLATFORM_DIRS=list(DEFAULT_EXCLUDED_PLATFORM_DIRS),
DEFAULT_EXCLUDED_MULTI_FILE_DIRS=list(DEFAULT_EXCLUDED_MULTI_FILE_DIRS),
DEFAULT_EXCLUDED_FILES=list(DEFAULT_EXCLUDED_FILES),
DEFAULT_EXCLUDED_EXTENSIONS=list(DEFAULT_EXCLUDED_EXTENSIONS),
PLATFORMS_BINDING=cfg.PLATFORMS_BINDING,
PLATFORMS_VERSIONS=cfg.PLATFORMS_VERSIONS,
SKIP_HASH_CALCULATION=cfg.SKIP_HASH_CALCULATION,
EJS_DEBUG=cfg.EJS_DEBUG,
EJS_CACHE_ENABLED=cfg.EJS_CACHE_ENABLED,
EJS_CACHE_LIMIT=cfg.EJS_CACHE_LIMIT,
EJS_DISABLE_AUTO_UNLOAD=cfg.EJS_DISABLE_AUTO_UNLOAD,
EJS_DISABLE_BATCH_BOOTUP=cfg.EJS_DISABLE_BATCH_BOOTUP,
EJS_ENABLE_AUTO_SAVE_SYNC=cfg.EJS_ENABLE_AUTO_SAVE_SYNC,
EJS_NETPLAY_ENABLED=cfg.EJS_NETPLAY_ENABLED,
# Contains credentials, so only send when authenticated
EJS_NETPLAY_ICE_SERVERS=(
cfg.EJS_NETPLAY_ICE_SERVERS if request.user.is_authenticated else []
),
EJS_CONTROLS=cfg.EJS_CONTROLS,
EJS_DEFAULT_CORES=cfg.EJS_DEFAULT_CORES,
EJS_SETTINGS=cfg.EJS_SETTINGS,
SCAN_METADATA_PRIORITY=cfg.SCAN_METADATA_PRIORITY,
SCAN_ARTWORK_PRIORITY=cfg.SCAN_ARTWORK_PRIORITY,
SCAN_ARTWORK_PRIORITY_OVERRIDES=cfg.SCAN_ARTWORK_PRIORITY_OVERRIDES,
SCAN_REGION_PRIORITY=cfg.SCAN_REGION_PRIORITY,
SCAN_LANGUAGE_PRIORITY=cfg.SCAN_LANGUAGE_PRIORITY,
SCAN_MEDIA=cfg.SCAN_MEDIA,
GAMELIST_AUTO_EXPORT_ON_SCAN=cfg.GAMELIST_AUTO_EXPORT_ON_SCAN,
GAMELIST_MEDIA_THUMBNAIL=cfg.GAMELIST_MEDIA_THUMBNAIL,
GAMELIST_MEDIA_IMAGE=cfg.GAMELIST_MEDIA_IMAGE,
PEGASUS_AUTO_EXPORT_ON_SCAN=cfg.PEGASUS_AUTO_EXPORT_ON_SCAN,
CONVERTO=cfg.CONVERTO,
CONVERTO_LIBRARY_TARGETS={
slug: sorted(targets)
for slug, targets in LIBRARY_TARGETS_BY_PLATFORM.items()
},
)
async def _reject_ambiguous_folder(fs_slug: str) -> None:
"""Refuse a mapping that several folders on disk would share."""
ambiguous = await fs_platform_handler.find_ambiguous_folders(fs_slug)
if ambiguous:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=(
f"Folders {', '.join(ambiguous)} differ only by case and share a "
"single mapping. Rename one to map them to different platforms."
),
)
def _record_config(request: Request, setting: str, op: str, **values: str) -> None:
record(
AuditAction.CONFIG_UPDATE,
request,
AuditTarget(AuditTargetType.CONFIG, setting, None),
{"setting": setting, "op": op, **values},
)
@protected_route(
router.post,
"/system/platforms",
[Scope.PLATFORMS_WRITE],
responses={**AMBIGUOUS_FOLDER_RESPONSE, **CONFIG_NOT_WRITABLE_RESPONSE},
)
async def add_platform_binding(
request: Request, payload: PlatformBindingPayload
) -> None:
"""Add platform binding to the configuration"""
fs_slug = payload.fs_slug
slug = payload.slug
await _reject_ambiguous_folder(fs_slug)
try:
cm.add_platform_binding(fs_slug, slug)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "platform_binding", "add", fs_slug=fs_slug, slug=slug)
@protected_route(
router.delete,
"/system/platforms/{fs_slug}",
[Scope.PLATFORMS_WRITE],
responses=CONFIG_NOT_WRITABLE_RESPONSE,
)
async def delete_platform_binding(
request: Request,
fs_slug: Annotated[
str, PathVar(description="The name of the platform's folder in the library.")
],
) -> None:
"""Delete platform binding from the configuration"""
try:
cm.remove_platform_binding(fs_slug)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "platform_binding", "remove", fs_slug=fs_slug)
@protected_route(
router.post,
"/system/versions",
[Scope.PLATFORMS_WRITE],
responses={**AMBIGUOUS_FOLDER_RESPONSE, **CONFIG_NOT_WRITABLE_RESPONSE},
)
async def add_platform_version(
request: Request, payload: PlatformBindingPayload
) -> None:
"""Add platform version to the configuration"""
fs_slug = payload.fs_slug
slug = payload.slug
await _reject_ambiguous_folder(fs_slug)
try:
cm.add_platform_version(fs_slug, slug)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "platform_version", "add", fs_slug=fs_slug, slug=slug)
@protected_route(
router.delete,
"/system/versions/{fs_slug}",
[Scope.PLATFORMS_WRITE],
responses=CONFIG_NOT_WRITABLE_RESPONSE,
)
async def delete_platform_version(
request: Request,
fs_slug: Annotated[
str, PathVar(description="The name of the platform's folder in the library.")
],
) -> None:
"""Delete platform version from the configuration"""
try:
cm.remove_platform_version(fs_slug)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "platform_version", "remove", fs_slug=fs_slug)
@protected_route(
router.post,
"/exclude",
[Scope.PLATFORMS_WRITE],
responses=CONFIG_NOT_WRITABLE_RESPONSE,
)
async def add_exclusion(request: Request, payload: ExclusionPayload) -> None:
"""Add platform exclusion to the configuration"""
exclusion_value = payload.exclusion_value
exclusion_type = ExclusionType(payload.exclusion_type)
try:
cm.add_exclusion(exclusion_type, exclusion_value)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(
request, "exclusion", "add", type=exclusion_type, value=exclusion_value
)
@protected_route(
router.delete,
"/exclude/{exclusion_type}/{exclusion_value}",
[Scope.PLATFORMS_WRITE],
responses=CONFIG_NOT_WRITABLE_RESPONSE,
)
async def delete_exclusion(
request: Request,
exclusion_type: ExclusionType,
exclusion_value: str,
) -> None:
"""Delete platform binding from the configuration"""
try:
cm.remove_exclusion(exclusion_type, exclusion_value)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(
request, "exclusion", "remove", type=exclusion_type, value=exclusion_value
)
@protected_route(
router.put,
"/scan",
[Scope.PLATFORMS_WRITE],
responses=CONFIG_NOT_WRITABLE_RESPONSE,
)
async def update_scan_settings(request: Request, payload: ScanSettingsPayload) -> None:
"""Replace the scan.* section of the configuration"""
region_priority_changed = (
cm.get_config().SCAN_REGION_PRIORITY != payload.region_priority
)
try:
cm.update_scan_settings(
metadata_priority=payload.metadata_priority,
artwork_priority=payload.artwork_priority,
artwork_overrides={
"cover": payload.cover_priority,
"screenshot": payload.screenshot_priority,
"manual": payload.manual_priority,
},
region_priority=payload.region_priority,
language_priority=payload.language_priority,
media=[str(m) for m in payload.media],
gamelist_export=payload.gamelist_export,
gamelist_thumbnail=str(payload.gamelist_thumbnail),
gamelist_image=str(payload.gamelist_image),
pegasus_export=payload.pegasus_export,
)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "scan_settings", "update")
# Region priority picks the primary rom of each sibling group, so every
# cached gallery sidecar is stale the moment the order changes.
if region_priority_changed:
db_rom_handler.invalidate_filter_values_cache()
@protected_route(
router.put,
"/converto_settings",
[Scope.PLATFORMS_WRITE],
responses=CONFIG_NOT_WRITABLE_RESPONSE,
)
async def update_converto_settings(
request: Request, payload: ConvertoSettingsPayload
) -> None:
"""Update the runtime-editable converto.* settings"""
current = cm.get_config().CONVERTO
try:
cm.update_converto_settings(
ConvertoConfig(
**payload.model_dump(),
cache_ttl_hours=current.cache_ttl_hours,
scan_metadata=current.scan_metadata,
)
)
except ConfigNotWritableException as exc:
log.critical(exc.message)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=exc.message
) from exc
_record_config(request, "converto_settings", "update")