Files
mtusi2026lab4/app/attachments.py
T
Чиняева f39ae06ef3 Реализовать лабораторную 4 на оценку 5
Добавить приватное S3-хранилище, вложения, presigned upload, reconciler и доказательства.
2026-11-07 01:37:21 +03:00

136 lines
4.9 KiB
Python

"""Attachment upload validation and serialisation.
Design rules for laboratory 4:
* raw bytes are streamed to an ephemeral file, never accumulated in RAM;
* the declared MIME type must match the format sniffed from the bytes;
* the pixel budget is checked before the image is decoded;
* a small semaphore bounds how many full decodes run at once per API replica;
* the object key is generated by the server and never contains the filename.
"""
from __future__ import annotations
import hashlib
import os
import tempfile
import threading
import uuid
from datetime import datetime, timezone
from PIL import Image, UnidentifiedImageError
from .config import settings
from .errors import ApiError, payload_too_large, unsupported_media_type
# Pillow's own bomb guard is a second line of defence; the explicit product
# check below is the contract limit and runs before verify()/load().
Image.MAX_IMAGE_PIXELS = settings.max_image_pixels
_decoders = threading.BoundedSemaphore(settings.max_decode_concurrency)
ALLOWED = {"PNG": "image/png", "JPEG": "image/jpeg"}
def format_time(value: datetime) -> str:
return value.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
def object_key(chat_id: uuid.UUID, attachment_id: uuid.UUID) -> str:
# Random UUIDs make the key unguessable; the client filename is display
# metadata only. A stable prefix helps the reconciler and operators.
return f"chats/{chat_id}/{attachment_id}"
def attachment_json(row: dict) -> dict:
return {
"id": str(row["id"]),
"chat_id": str(row["chat_id"]),
"owner_id": str(row["owner_id"]),
"filename": row["filename"],
"content_type": row["content_type"],
"size_bytes": int(row["size_bytes"]),
"sha256": row["sha256"],
"status": row["status"],
"variants": [],
"error_code": row["error_code"],
"created_at": format_time(row["created_at"]),
"updated_at": format_time(row["updated_at"]),
}
def read_to_temp(stream) -> tuple[str, int, str]:
"""Stream the request body to a bounded temporary file.
The caller owns the returned path and must delete it in a finally block.
"""
digest = hashlib.sha256()
total = 0
handle = tempfile.NamedTemporaryFile(
mode="wb", prefix="upload-", suffix=".bin", dir=settings.upload_temp_dir, delete=False
)
path = handle.name
try:
while True:
try:
chunk = stream.read(64 * 1024)
except Exception as error:
# Flask raises RequestEntityTooLarge when MAX_CONTENT_LENGTH is
# exceeded on a chunked request.
if getattr(error, "code", None) == 413:
raise payload_too_large() from error
raise
if not chunk:
break
total += len(chunk)
if total > settings.max_upload_bytes:
raise payload_too_large("Файл больше 10 MiB")
digest.update(chunk)
handle.write(chunk)
finally:
handle.close()
if total == 0:
os.unlink(path)
raise unsupported_media_type("Пустой файл")
return path, total, digest.hexdigest()
def validate_image(path: str, declared_content_type: str) -> None:
"""Sniff the real format, enforce the pixel budget, then fully decode."""
try:
with Image.open(path) as image:
actual = ALLOWED.get(image.format or "")
if actual is None:
raise unsupported_media_type("Поддерживаются только PNG и JPEG")
if actual != declared_content_type:
raise unsupported_media_type("Content-Type не совпадает с содержимым")
width, height = image.size
if width < 1 or height < 1:
raise unsupported_media_type("Некорректные размеры изображения")
if width * height > settings.max_image_pixels:
raise payload_too_large("Слишком много пикселей")
# verify() parses the container and checks CRCs without keeping the
# decoded bitmap; load() below is the bounded full decode.
image.verify()
except ApiError:
raise
except (UnidentifiedImageError, OSError, ValueError, SyntaxError) as error:
raise unsupported_media_type("Повреждённое изображение") from error
with _decoders:
try:
with Image.open(path) as image:
image.load()
except (UnidentifiedImageError, OSError, ValueError, SyntaxError) as error:
raise unsupported_media_type("Повреждённое изображение") from error
def cleanup(path: str | None) -> None:
if not path:
return
try:
os.unlink(path)
except FileNotFoundError:
return
except OSError:
return