forked from mtusi2026/mtusi2026lab2
Add the adapters behind the attachments ports: PostgreSQL for the pending and ready rows, with a foreign key violation on the chat member turned into 404; Pillow that opens only PNG and JPEG, refuses a type that differs from Content-Type, checks the pixel count from the header before any decoding, verifies PNG chunks and decodes JPEG at 1/8 scale in a worker thread under a semaphore; and S3 on aiobotocore with path style, SigV4, short timeouts and checksums only when required. botocore sends Expect: 100-continue for streamed bodies and RustFS behind the Docker port proxy never answers it, so the PUT hung until the read timeout; the handler that adds the header is unregistered. The HTTP layer streams the raw body into the use case, returns the contract Attachment and streams the original back with its content type and length and no redirect. The request pipeline takes a body limit per route, so uploads may carry 10 MiB while JSON stays at 1 MiB. main.py wires the zone and answers its membership port from the messaging chat directory. Tests run the API with an in-memory object store: download by another member, 404 for strangers, the exact 10 MiB limit and one byte over it with Content-Length and chunked, 40 MP and above, spoofed and truncated images, unsupported types, dangerous names, the variant, a failing store and a lost object. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
66 lines
1.9 KiB
Python
66 lines
1.9 KiB
Python
from collections.abc import AsyncIterator
|
|
from datetime import datetime, timedelta
|
|
from typing import Any
|
|
|
|
from dishka import Provider, Scope, provide
|
|
|
|
from messenger.attachments.application.errors import ObjectMissingError
|
|
from messenger.attachments.application.ports import ObjectContent, ObjectStorage
|
|
from messenger.shared.application.errors import Unavailable
|
|
|
|
|
|
class NullLogger:
|
|
__slots__ = ()
|
|
|
|
def info(self, event: str, /, **fields: Any) -> None: ...
|
|
|
|
def warning(self, event: str, /, **fields: Any) -> None: ...
|
|
|
|
def error(self, event: str, /, **fields: Any) -> None: ...
|
|
|
|
|
|
class FrozenClock:
|
|
__slots__ = ("_current",)
|
|
|
|
def __init__(self, start: datetime) -> None:
|
|
self._current = start
|
|
|
|
def now(self) -> datetime:
|
|
self._current += timedelta(microseconds=1)
|
|
return self._current
|
|
|
|
|
|
async def pieces(data: bytes, size: int = 65_536) -> AsyncIterator[bytes]:
|
|
for start in range(0, len(data), size):
|
|
yield data[start : start + size]
|
|
|
|
|
|
class MemoryObjectStorage:
|
|
__slots__ = ("failing", "objects")
|
|
|
|
def __init__(self) -> None:
|
|
self.objects: dict[str, tuple[bytes, str]] = {}
|
|
self.failing = False
|
|
|
|
async def put(self, key: str, data: bytes, content_type: str) -> None:
|
|
if self.failing:
|
|
raise Unavailable("S3 недоступно")
|
|
self.objects[key] = (bytes(data), content_type)
|
|
|
|
async def open(self, key: str) -> ObjectContent:
|
|
if self.failing:
|
|
raise Unavailable("S3 недоступно")
|
|
if key not in self.objects:
|
|
raise ObjectMissingError(key)
|
|
data = self.objects[key][0]
|
|
return ObjectContent(size=len(data), chunks=pieces(data))
|
|
|
|
async def delete(self, key: str) -> None:
|
|
self.objects.pop(key, None)
|
|
|
|
|
|
class MemoryStorageProvider(Provider):
|
|
@provide(scope=Scope.APP)
|
|
def storage(self) -> ObjectStorage:
|
|
return MemoryObjectStorage()
|