/
/
/
1"""
2Helper utilities for the Metadata Controller.
3
4Pure functions used by the controller and its mixins that do not need access to
5the controller instance.
6"""
7
8from __future__ import annotations
9
10import pathlib
11
12from .constants import _IMAGEPROXY_CONTENT_TYPES
13
14
15def _detect_image_format(path: str) -> str:
16 """Detect image format from file path extension, defaulting to jpg."""
17 # strip any query suffix (e.g. a cache-busting ?cs=) before extension detection
18 match pathlib.PurePath(path.split("?", 1)[0]).suffix.lower():
19 case ".svg":
20 return "svg"
21 case ".png":
22 return "png"
23 case _:
24 return "jpg"
25
26
27def _normalize_imageproxy_format(value: str | None) -> str | None:
28 """Return a validated, lowercase imageproxy format, or None when invalid."""
29 if not value:
30 return None
31 normalized = value.strip().lower()
32 if normalized in _IMAGEPROXY_CONTENT_TYPES:
33 return normalized
34 return None
35