Initial commit: svg backend
This commit is contained in:
31
backend/app/services/svg_inspector.py
Normal file
31
backend/app/services/svg_inspector.py
Normal file
@@ -0,0 +1,31 @@
|
||||
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
|
||||
Reference in New Issue
Block a user