"""Shared image upload utilities used by both the edit and serve servers.""" from __future__ import annotations from pathlib import Path ALLOWED_IMAGE_TYPES: frozenset[str] = frozenset({ "image/jpeg", "image/png", "image/webp", "image/gif", }) MAX_IMAGE_BYTES: int = 10 * 1024 * 1024 # 10 MB def unique_image_name(directory: Path, filename: str) -> str: """Return a filename that does not collide with existing files in directory.""" stem, suffix = Path(filename).stem, Path(filename).suffix candidate = filename counter = 1 while (directory / candidate).exists(): candidate = f"{stem}_{counter}{suffix}" counter += 1 return candidate