32 lines
906 B
Python
32 lines
906 B
Python
from defusedxml import ElementTree as DefusedET
|
|
from fastapi import HTTPException, status
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
def inspect_svg_bytes(content: bytes) -> int:
|
|
try:
|
|
root = DefusedET.fromstring(content)
|
|
except Exception as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"Invalid SVG XML: {exc.__class__.__name__}",
|
|
) from exc
|
|
|
|
tag = root.tag or ""
|
|
if not tag.endswith("svg"):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Root element is not <svg>",
|
|
)
|
|
|
|
element_count = sum(1 for _ in root.iter())
|
|
|
|
if element_count > settings.svg_max_elements:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="SVG element count exceeds configured limit",
|
|
)
|
|
|
|
return element_count
|