from __future__ import annotations from pathlib import Path from typing import Iterable MAX_DXF_MB = 10 MAX_GEOJSON_MB = 5 MAX_KML_MB = 8 MAX_PDF_REFERENCE_MB = 15 MAX_BOARD_PREVIEW_MB = 12 def validate_upload( path: str | Path, *, allowed_suffixes: Iterable[str], max_mb: int, label: str, ) -> Path: source = Path(path) if not source.exists() or not source.is_file(): raise ValueError(f"{label} upload is missing or could not be read.") suffixes = {suffix.lower() for suffix in allowed_suffixes} if source.suffix.lower() not in suffixes: allowed = ", ".join(sorted(suffixes)) raise ValueError(f"{label} must be one of: {allowed}.") size_mb = source.stat().st_size / (1024 * 1024) if size_mb > max_mb: raise ValueError(f"{label} is {size_mb:.1f} MB. Limit is {max_mb} MB for this Space.") return source