mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* fix(platforms): count only the roms the caller can see in rom_count and fs_size_bytes Platform.rom_count and fs_size_bytes are column properties over every rom row on the platform, and the platform endpoints only filter hidden platforms. So the platform header's "In library" count and "On disk" size included roms hidden from the caller (directly, or by an age limit), while the gallery left them out. Server Stats was off the same way: its per-platform metadata coverage divides /api/stats' visibility-filtered match counts by the unfiltered rom_count, so a restricted user saw coverage too low and platform counts and sizes too high. For a restricted caller, the list, detail and update endpoints now subtract the totals of the roms RomVisibilityFilter hides (DBStatsHandler.get_hidden_rom_totals_by_platform, next to the other per-platform aggregates). Direct hides are matched by primary key, so a few hidden roms cost a few lookups rather than a pass over every rom on the platform. Admins and other unrestricted callers keep the model's values and skip the query. On a generated library of 50 platforms and 100k roms (MariaDB), the platform list takes ~44 ms; this adds ~4 ms with 100 roms hidden, ~44 ms with 5,000, and ~60 ms with an age limit (min_age isn't indexed, so that case scans the platforms' roms). Fixes #5312 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(platforms): read the hidden roms' totals in the platform query Addresses Greptile's review of #5313: - Counts can become negative: the hidden totals came from a second query, so a rom added between the two could be subtracted without having been counted. They now come from a grouped subquery outer-joined into the statement that computes the full totals, so both read one snapshot (on PostgreSQL's READ COMMITTED too, which a shared transaction wouldn't give). - Counting rules live in endpoints: the endpoint helper is gone. get_platform and get_platforms take the caller's RomVisibilityFilter (unrestricted by default) and return platforms with the visible totals; DBStatsHandler.get_hidden_rom_totals_by_platform is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(platforms): code-review fixes for visible platform totals - Re-read rows with populate_existing so a shared session never subtracts twice - Narrow the hidden-totals subquery to the one platform get_platform reads - Re-read after update only for restricted callers, and 404 instead of leaking full totals - Test the update endpoint's restricted totals and the shared-session read Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(platforms): simplify pass on visible platform totals Gate the row clause in one condition with a why comment, fold the MariaDB narrowing reason into the docstring, and drop restating comments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(platforms): count only visible roms in /supported and RetroArch listings - Move the hidden-ROM predicate into RomVisibilityFilter.hidden_on_visible_platforms_clause, with the age rule spelled as ranges an index on min_age can serve - get_platforms reads hidden platforms from the visibility filter instead of a second hidden_platform_ids argument - /api/platforms/supported lists a hidden platform as unmatched and counts only the caller's ROMs on the rest - RetroArch WebDAV skips a platform whose ROMs are all hidden from the caller Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(roms): index platform_id, min_age, fs_size_bytes for age-limited totals On a 73k-ROM copy of a real library (MariaDB 13), GET /api/platforms for an age-limited user went from 208 ms to 55 ms: the hidden-totals aggregate is now an index-only scan instead of a full scan of the wide roms rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
139 lines
4.5 KiB
Python
139 lines
4.5 KiB
Python
"""Read-only WebDAV browsing (PROPFIND) of the rom library, for generic WebDAV
|
|
clients mounting the Cloud Sync URL. RetroArch itself never browses."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from html import escape
|
|
from urllib.parse import quote
|
|
|
|
from handler.auth.permissions import ResolvedPermissions
|
|
from handler.database import db_platform_handler, db_rom_handler
|
|
from models.platform import Platform
|
|
from models.rom import Rom, apply_file_stats
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class PropfindEntry:
|
|
"""One `<D:response>` entry, with an unencoded `href` relative to the mount."""
|
|
|
|
href: str
|
|
is_collection: bool
|
|
display_name: str
|
|
content_length: int | None = None
|
|
last_modified: datetime | None = None
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RomFile:
|
|
"""A rom as it appears over WebDAV, as one file even when multi-file."""
|
|
|
|
rom_id: int
|
|
display_name: str
|
|
size_bytes: int
|
|
updated_at: datetime
|
|
|
|
|
|
# Clients expect absolute <D:href>s; relative ones make them render the
|
|
# requested collection as an endlessly nested subfolder of itself.
|
|
WEBDAV_MOUNT_PREFIX = "/api/sync/retroarch"
|
|
|
|
|
|
def _href_escape(path: str) -> str:
|
|
return quote(f"{WEBDAV_MOUNT_PREFIX}/{path}", safe="/")
|
|
|
|
|
|
def _response_xml(entry: PropfindEntry) -> str:
|
|
resource_type = "<D:collection/>" if entry.is_collection else ""
|
|
extra = (
|
|
""
|
|
if entry.is_collection
|
|
else (
|
|
f"<D:getcontentlength>{entry.content_length or 0}</D:getcontentlength>"
|
|
"<D:getcontenttype>application/octet-stream</D:getcontenttype>"
|
|
)
|
|
)
|
|
last_modified = (
|
|
f"<D:getlastmodified>{entry.last_modified.strftime('%a, %d %b %Y %H:%M:%S GMT')}</D:getlastmodified>"
|
|
if entry.last_modified
|
|
else ""
|
|
)
|
|
|
|
return (
|
|
"<D:response>"
|
|
f"<D:href>{escape(_href_escape(entry.href), quote=False)}</D:href>"
|
|
"<D:propstat><D:prop>"
|
|
f"<D:resourcetype>{resource_type}</D:resourcetype>"
|
|
f"<D:displayname>{escape(entry.display_name, quote=False)}</D:displayname>"
|
|
f"{extra}{last_modified}"
|
|
"</D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat>"
|
|
"</D:response>"
|
|
)
|
|
|
|
|
|
def build_multistatus(entries: list[PropfindEntry]) -> str:
|
|
body = "".join(_response_xml(entry) for entry in entries)
|
|
return (
|
|
'<?xml version="1.0" encoding="utf-8"?>'
|
|
'<D:multistatus xmlns:D="DAV:">' + body + "</D:multistatus>"
|
|
)
|
|
|
|
|
|
def _display_name(rom: Rom) -> str:
|
|
"""The name the content endpoint downloads a rom as: a zip when multi-file."""
|
|
if rom.has_multiple_files:
|
|
return f"{rom.fs_name_no_ext}.zip"
|
|
# A nested single file's `fs_name` is its folder; the file has the real name.
|
|
files = sorted(rom.files, key=lambda f: f.file_name)
|
|
return files[0].file_name if files else rom.fs_name
|
|
|
|
|
|
def list_platforms(permissions: ResolvedPermissions) -> list[Platform]:
|
|
platforms = db_platform_handler.get_platforms(visibility=permissions.rom_visibility)
|
|
return [p for p in platforms if p.rom_count > 0]
|
|
|
|
|
|
def find_platform(fs_slug: str, permissions: ResolvedPermissions) -> Platform | None:
|
|
"""The platform behind `fs_slug`, if the caller can see it and any of its ROMs."""
|
|
platform = db_platform_handler.get_platform_by_fs_slug(fs_slug)
|
|
if not platform or not permissions.can_see_platform(platform.id):
|
|
return None
|
|
platform = db_platform_handler.get_platform(
|
|
platform.id, visibility=permissions.rom_visibility
|
|
)
|
|
return platform if platform and platform.rom_count > 0 else None
|
|
|
|
|
|
def list_rom_files(
|
|
platform: Platform, permissions: ResolvedPermissions
|
|
) -> list[RomFile]:
|
|
roms = db_rom_handler.get_roms_scalar(
|
|
platform_ids=[platform.id],
|
|
include_files=True,
|
|
visibility=permissions.rom_visibility,
|
|
)
|
|
rom_files = []
|
|
for rom in roms:
|
|
apply_file_stats(rom, rom.files)
|
|
rom_files.append(
|
|
RomFile(
|
|
rom_id=rom.id,
|
|
display_name=_display_name(rom),
|
|
size_bytes=rom.fs_size_bytes,
|
|
updated_at=rom.updated_at,
|
|
)
|
|
)
|
|
|
|
return rom_files
|
|
|
|
|
|
def find_rom_file(
|
|
platform_fs_slug: str, file_name: str, permissions: ResolvedPermissions
|
|
) -> RomFile | None:
|
|
platform = db_platform_handler.get_platform_by_fs_slug(platform_fs_slug)
|
|
if not platform:
|
|
return None
|
|
files = list_rom_files(platform, permissions)
|
|
return next((f for f in files if f.display_name == file_name), None)
|