Files
mtusi2026lab4/app/services/attachments.py
T

148 lines
5.8 KiB
Python

from __future__ import annotations
import asyncio
import hashlib
import logging
from datetime import datetime, timezone
from tempfile import SpooledTemporaryFile
from typing import AsyncIterator, BinaryIO
import unicodedata
from uuid import UUID, uuid4
from PIL import Image, UnidentifiedImageError
from ..domain.errors import (
Conflict,
Invalid,
NotFound,
PayloadTooLarge,
Unavailable,
UnsupportedMediaType,
)
from ..domain.models import Attachment, User
from ..repositories import AttachmentRepository
from ..storage import ObjectStream, S3Storage
logger = logging.getLogger('app.attachments')
MIME_BY_FORMAT = {'PNG': 'image/png', 'JPEG': 'image/jpeg'}
def _validate_filename(filename: str) -> None:
if not 1 <= len(filename) <= 255:
raise Invalid('filename должен содержать от 1 до 255 символов')
if any(
character in {'/', '\\'} or unicodedata.category(character) == 'Cc'
for character in filename
):
raise Invalid('filename содержит запрещённые символы')
def _inspect_image(source: BinaryIO, declared_type: str, max_pixels: int) -> tuple[str, int, int]:
try:
source.seek(0)
with Image.open(source) as image:
actual_type = MIME_BY_FORMAT.get(image.format or '')
if actual_type is None or actual_type != declared_type:
raise UnsupportedMediaType('MIME не соответствует содержимому PNG/JPEG')
width, height = image.size
if width <= 0 or height <= 0:
raise UnsupportedMediaType('изображение имеет неверные размеры')
if width * height > max_pixels:
raise PayloadTooLarge('изображение превышает лимит декодированных пикселей')
image.verify()
except Image.DecompressionBombError as error:
raise PayloadTooLarge('изображение превышает лимит декодированных пикселей') from error
except (UnidentifiedImageError, OSError, SyntaxError) as error:
raise UnsupportedMediaType('повреждённое или неподдерживаемое изображение') from error
finally:
source.seek(0)
return actual_type, width, height
class AttachmentService:
def __init__(
self,
repository: AttachmentRepository,
storage: S3Storage,
max_upload_bytes: int,
max_image_pixels: int,
) -> None:
self._repository = repository
self._storage = storage
self._max_upload_bytes = max_upload_bytes
self._max_image_pixels = max_image_pixels
async def upload(
self,
actor: User,
chat_id: UUID,
filename: str,
declared_type: str,
chunks: AsyncIterator[bytes],
) -> Attachment:
_validate_filename(filename)
if declared_type not in MIME_BY_FORMAT.values():
raise UnsupportedMediaType('поддерживаются только image/png и image/jpeg')
if not await self._repository.chat_exists_for_member(chat_id, actor.id):
raise NotFound('чат не найден')
digest = hashlib.sha256()
size = 0
with SpooledTemporaryFile(max_size=1024 * 1024, mode='w+b') as source:
async for chunk in chunks:
size += len(chunk)
if size > self._max_upload_bytes:
raise PayloadTooLarge('файл превышает лимит 10 MiB')
digest.update(chunk)
source.write(chunk)
if size == 0:
raise UnsupportedMediaType('пустой файл не является изображением')
actual_type, width, height = await asyncio.to_thread(
_inspect_image,
source,
declared_type,
self._max_image_pixels,
)
attachment_id = uuid4()
object_key = f'originals/{attachment_id}'
await self._storage.put(object_key, source, size, actual_type)
now = datetime.now(timezone.utc)
try:
return await self._repository.create(
attachment_id=attachment_id,
chat_id=chat_id,
owner_id=actor.id,
filename=filename,
object_key=object_key,
content_type=actual_type,
size_bytes=size,
sha256=digest.hexdigest(),
width=width,
height=height,
now=now,
)
except Exception:
try:
await self._storage.remove(object_key)
except Unavailable:
logger.warning('failed to compensate orphan object key=%s', object_key)
raise
async def get(self, actor: User, attachment_id: UUID) -> Attachment:
attachment = await self._repository.get_for_member(attachment_id, actor.id)
if attachment is None:
raise NotFound('вложение не найдено')
return attachment
async def content(self, actor: User, attachment_id: UUID) -> tuple[Attachment, ObjectStream]:
attachment = await self.get(actor, attachment_id)
if attachment.status != 'ready':
raise Conflict('оригинал вложения недоступен')
try:
stream = await self._storage.get(attachment.object_key)
except FileNotFoundError as error:
raise Unavailable('оригинал вложения отсутствует в хранилище') from error
return attachment, stream