139 lines
5.7 KiB
Python
139 lines
5.7 KiB
Python
"""lab5: API только принимает staging + пишет job. Ресайз ушёл в worker."""
|
|
import hashlib
|
|
import io
|
|
import logging
|
|
import uuid
|
|
|
|
from PIL import Image, UnidentifiedImageError
|
|
|
|
from . import config, db, jobs, repo, s3
|
|
from .errors import ApiError, bad_request, not_found
|
|
|
|
log = logging.getLogger("attachments")
|
|
|
|
ALLOWED_FORMATS = {"PNG": "image/png", "JPEG": "image/jpeg"}
|
|
Image.MAX_IMAGE_PIXELS = config.MAX_IMAGE_PIXELS
|
|
|
|
|
|
def validate_filename(filename):
|
|
if not isinstance(filename, str) or not (1 <= len(filename) <= 255):
|
|
raise bad_request("Некорректное имя файла")
|
|
for c in filename:
|
|
if c in ("/", "\\", "\x00") or ord(c) < 0x20 or ord(c) == 0x7F:
|
|
raise bad_request("Недопустимое имя файла")
|
|
return filename
|
|
|
|
|
|
def inspect_image_header(fileobj, total_bytes):
|
|
"""Только заголовок: формат и размеры. Полное декодирование — в worker.
|
|
|
|
load() в API не вызываем — дорого и не нужно для 202.
|
|
"""
|
|
if total_bytes == 0:
|
|
raise bad_request("Пустой файл")
|
|
if total_bytes > config.MAX_UPLOAD_BYTES:
|
|
raise ApiError(413, "payload_too_large", "Файл больше 10 MiB")
|
|
fileobj.seek(0)
|
|
try:
|
|
with Image.open(fileobj) as img:
|
|
fmt = img.format
|
|
width, height = img.size
|
|
except UnidentifiedImageError:
|
|
raise ApiError(415, "unsupported_media_type", "Неизвестный формат изображения")
|
|
except Image.DecompressionBombError:
|
|
raise ApiError(413, "payload_too_large", "Слишком много пикселей")
|
|
finally:
|
|
fileobj.seek(0)
|
|
|
|
if fmt not in ALLOWED_FORMATS:
|
|
raise ApiError(415, "unsupported_media_type", "Ожидается PNG или JPEG")
|
|
if width <= 0 or height <= 0:
|
|
raise ApiError(415, "unsupported_media_type", "Повреждённое изображение")
|
|
if width * height > config.MAX_IMAGE_PIXELS:
|
|
raise ApiError(413, "payload_too_large", "Слишком много пикселей")
|
|
return ALLOWED_FORMATS[fmt]
|
|
|
|
|
|
def upload_to_staging(chat_id, owner_id, filename, fileobj, total_bytes, digest, content_type):
|
|
"""POST /attachments: staging в S3 → INSERT attachments(queued) + job + outbox → 202."""
|
|
validate_filename(filename)
|
|
staging_key = "staging/%s/%s" % (chat_id, uuid.uuid4().hex)
|
|
|
|
with db.connection() as conn:
|
|
if not repo.is_member(conn, chat_id, owner_id):
|
|
raise not_found()
|
|
|
|
# 1. staging в S3 — до INSERT, чтобы не было queued без байтов.
|
|
try:
|
|
fileobj.seek(0)
|
|
s3.put_object(staging_key, fileobj, content_type, content_length=total_bytes)
|
|
except Exception as exc:
|
|
log.warning("staging PUT failed: %s", type(exc).__name__)
|
|
raise ApiError(503, "unavailable", "Хранилище временно недоступно")
|
|
|
|
# 2. Одна транзакция: attachments + job + outbox.
|
|
try:
|
|
with db.connection() as conn:
|
|
row = repo.insert_attachment_queued(
|
|
conn, chat_id, owner_id, filename, content_type,
|
|
total_bytes, digest, staging_key)
|
|
jobs.create_job_for_attachment(conn, row["id"], config.PIPELINE_VERSION)
|
|
except Exception:
|
|
# Компенсация: удалить staging, чтобы не оставить orphan.
|
|
s3.delete_object(staging_key)
|
|
raise
|
|
|
|
# 3. Публикуем outbox сразу — если брокер жив, latency короче.
|
|
# Если упадёт — worker или API-поток подхватит на следующей итерации.
|
|
try:
|
|
with db.connection() as conn:
|
|
jobs.publish_pending_outbox(conn, _broker_publish)
|
|
except Exception:
|
|
log.warning("inline outbox publish failed, будет повторено")
|
|
|
|
return row
|
|
|
|
|
|
def _broker_publish(payload):
|
|
from . import broker
|
|
broker.publish(payload)
|
|
|
|
|
|
def download_original(attachment_id, user_id):
|
|
with db.connection() as conn:
|
|
row = repo.get_attachment_for_member(conn, attachment_id, user_id)
|
|
if row is None:
|
|
raise not_found()
|
|
if row["status"] in ("queued", "processing"):
|
|
raise ApiError(409, "conflict", "Вложение ещё не готово")
|
|
if row["status"] == "failed":
|
|
raise ApiError(409, "conflict", "Вложение недоступно")
|
|
if not row["object_key"]:
|
|
raise ApiError(409, "conflict", "Вложение ещё не готово")
|
|
try:
|
|
body = s3.get_object(row["object_key"])
|
|
except Exception as exc:
|
|
log.warning("S3 GET original failed: %s", type(exc).__name__)
|
|
raise ApiError(503, "unavailable", "Хранилище временно недоступно")
|
|
return row, body
|
|
|
|
|
|
def download_variant(attachment_id, user_id, variant_name):
|
|
with db.connection() as conn:
|
|
row = repo.get_attachment_for_member(conn, attachment_id, user_id)
|
|
if row is None:
|
|
raise not_found()
|
|
if row["status"] in ("queued", "processing"):
|
|
raise ApiError(409, "conflict", "Вложение ещё не готово")
|
|
if row["status"] == "failed":
|
|
raise ApiError(409, "conflict", "Вложение недоступно")
|
|
with db.connection() as conn:
|
|
v = repo.get_variant(conn, attachment_id, variant_name)
|
|
if v is None:
|
|
raise not_found()
|
|
try:
|
|
body = s3.get_object(v["object_key"])
|
|
except Exception as exc:
|
|
log.warning("S3 GET variant failed: %s", type(exc).__name__)
|
|
raise ApiError(503, "unavailable", "Хранилище временно недоступно")
|
|
return v, body |