mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
CPython's int() raises past 4300 digits. The bare-digit branch of scene_id_or_none already caught that, but the URL parsers, the filename tag extractors and the Pouet 302 Location parser all called int() on an unbounded (\d+) capture, so a pasted URL or a scanned filename carrying a long digit run surfaced as a 500. Promote pouet_handler's private _int_or_none to utils.int_or_none, next to valid_youtube_id, and route every scene id conversion through it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
import re
|
|
import uuid
|
|
from typing import Any, Final
|
|
|
|
from __version__ import __version__
|
|
|
|
# Embedded straight into a YouTube URL, so anything outside this must be dropped.
|
|
YOUTUBE_ID_RE: Final = re.compile(r"[A-Za-z0-9_-]{11}")
|
|
|
|
|
|
def get_version() -> str:
|
|
"""Returns current version tag"""
|
|
if __version__ != "<version>":
|
|
return __version__
|
|
|
|
return "development"
|
|
|
|
|
|
def is_valid_uuid(uuid_str: str) -> bool:
|
|
"""Check if a string is a valid UUID."""
|
|
try:
|
|
uuid.UUID(uuid_str, version=4)
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
|
|
|
|
def int_or_none(value: Any) -> int | None:
|
|
"""An int, or None when it is not one (``int()`` raises past 4300 digits)."""
|
|
if value is None or value == "":
|
|
return None
|
|
try:
|
|
return int(value)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
|
|
def valid_youtube_id(value: Any) -> str | None:
|
|
"""A video id, or None when it is not one."""
|
|
text = str(value or "")
|
|
return text if YOUTUBE_ID_RE.fullmatch(text) else None
|