File size: 891 Bytes
efcf0a3
 
 
 
 
 
 
 
2f91d7e
efcf0a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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