Source code for satorbis_kit.pgstac.uploader.utils
"""Shared helpers for STAC upload handlers."""
from __future__ import annotations
import re
from ...storage import StorageError
from ..models.constants import FILENAME_PATTERNS
[docs]
def sanitize_collection(collection: str) -> str:
if not collection:
raise StorageError("collection cannot be empty when building STAC paths")
if not re.match(r"^[a-zA-Z0-9_-]+$", collection):
raise StorageError(
"collection contains invalid characters. Only alphanumeric characters, hyphens, and underscores are allowed."
)
return collection
[docs]
def validate_stac_filename(filename: str) -> None:
# Accept both .tif and .tiff extensions to align with validation inputs
if not (filename.endswith(".tif") or filename.endswith(".tiff")):
raise StorageError("Raster filenames must end with .tif or .tiff")
for pattern in FILENAME_PATTERNS:
if re.match(pattern, filename):
return
raise StorageError(
"Filename must follow STAC GenericMetaExtractor formats "
"(e.g., YYYYMMDD.tif or YYYYMMDD_TILE.tif)"
)