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, job_max_attempts: int, ) -> None: self._repository = repository self._storage = storage self._max_upload_bytes = max_upload_bytes self._max_image_pixels = max_image_pixels self._job_max_attempts = job_max_attempts 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() job_id = uuid4() staging_key = f'staging/{attachment_id}/original' await self._storage.put(staging_key, source, size, actual_type) now = datetime.now(timezone.utc) try: return await self._repository.create_queued( attachment_id=attachment_id, job_id=job_id, chat_id=chat_id, owner_id=actor.id, filename=filename, staging_key=staging_key, content_type=actual_type, size_bytes=size, sha256=digest.hexdigest(), width=width, height=height, max_attempts=self._job_max_attempts, now=now, ) except Exception: try: await self._storage.remove(staging_key) except Unavailable: logger.warning('failed to compensate staging object key=%s', staging_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, variant: str ) -> tuple[Attachment, ObjectStream, str, int]: attachment = await self.get(actor, attachment_id) if attachment.status != 'ready': raise Conflict('вложение ещё не готово') if variant == 'original': key = attachment.original_key content_type = attachment.content_type size_bytes = attachment.size_bytes else: found = next((item for item in attachment.variants if item.name == variant), None) if found is None: raise NotFound('вариант вложения не найден') key = found.object_key content_type = found.content_type size_bytes = found.size_bytes if key is None: raise Unavailable('объект вложения отсутствует в метаданных') try: stream = await self._storage.get(key) except FileNotFoundError as error: raise Unavailable('объект вложения отсутствует в хранилище') from error return attachment, stream, content_type, size_bytes