import io from PIL import Image from fastapi import HTTPException MAX_SIZE = 10 * 1024 * 1024 # 10 мбайт MAX_PIXELS = 40_000_000 def validate_image(content: bytes) -> str: if len(content) > MAX_SIZE: raise HTTPException(413, "File too large") try: img = Image.open(io.BytesIO(content)) img.verify() img = Image.open(io.BytesIO(content)) except Exception: raise HTTPException(415, "Invalid image format") if img.format not in ("PNG", "JPEG"): raise HTTPException(415, "Unsupported media type") width, height = img.size if width * height > MAX_PIXELS: raise HTTPException(413, "Image too large in pixels") return "image/png" if img.format == "PNG" else "image/jpeg"