50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
def _ensure_dir(path: str) -> Path:
|
|
dir_path = Path(path)
|
|
dir_path.mkdir(parents=True, exist_ok=True)
|
|
return dir_path
|
|
|
|
|
|
def save_original_svg(*, filename: str, content: bytes) -> tuple[str, str]:
|
|
upload_id = uuid4().hex
|
|
target_dir = _ensure_dir(f"{settings.storage_original_dir}/{upload_id}")
|
|
target_path = target_dir / filename
|
|
target_path.write_bytes(content)
|
|
return upload_id, str(target_path)
|
|
|
|
|
|
def save_sanitized_svg(*, upload_id: str, filename: str, content: bytes) -> str:
|
|
target_dir = _ensure_dir(f"{settings.storage_sanitized_dir}/{upload_id}")
|
|
target_path = target_dir / filename
|
|
target_path.write_bytes(content)
|
|
return str(target_path)
|
|
|
|
|
|
def save_normalized_json(*, upload_id: str, filename: str, content: str) -> str:
|
|
target_dir = _ensure_dir(f"{settings.storage_normalized_dir}/{upload_id}")
|
|
target_path = target_dir / f"{Path(filename).stem}.normalized.json"
|
|
target_path.write_text(content, encoding="utf-8")
|
|
return str(target_path)
|
|
|
|
|
|
def save_display_svg(*, upload_id: str, filename: str, content: bytes) -> str:
|
|
target_dir = _ensure_dir(f"{settings.storage_display_dir}/{upload_id}")
|
|
target_path = target_dir / f"{Path(filename).stem}.display.svg"
|
|
target_path.write_bytes(content)
|
|
return str(target_path)
|
|
|
|
|
|
def load_normalized_json(upload_id: str) -> str:
|
|
target_dir = Path(f"{settings.storage_normalized_dir}/{upload_id}")
|
|
files = sorted(target_dir.glob("*.normalized.json"))
|
|
if not files:
|
|
raise FileNotFoundError(f"Normalized payload not found for upload_id={upload_id}")
|
|
return files[-1].read_text(encoding="utf-8")
|