chore: copy lab4 solution (api, nginx, migrations, tests, s3)

This commit is contained in:
2026-11-11 21:31:19 +03:00
parent 276dbdad8a
commit c9dbf7d71e
33 changed files with 1792 additions and 21 deletions
+25
View File
@@ -0,0 +1,25 @@
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"