Files
mtusi2026lab6/src/messenger/attachments/domain/values.py
T
ArtyomandClaude Opus 5.5 5ae3d77152 Carry over the lab 5 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.

This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 03:51:58 +03:00

47 lines
1.5 KiB
Python

import unicodedata
from dataclasses import dataclass
from enum import StrEnum
from messenger.attachments.domain.errors import InvalidFilename, UnsupportedImage
FILENAME_MAX = 255
SEPARATORS = frozenset("/\\")
@dataclass(frozen=True, slots=True)
class Filename:
value: str
def __post_init__(self) -> None:
if not self.value.strip():
raise InvalidFilename("параметр filename обязателен и не может быть пустым")
if len(self.value) > FILENAME_MAX:
raise InvalidFilename(f"параметр filename длиннее {FILENAME_MAX} символов Unicode")
if any(c in SEPARATORS or unicodedata.category(c) == "Cc" for c in self.value):
raise InvalidFilename(
"параметр filename содержит разделитель пути или управляющий символ"
)
def __str__(self) -> str:
return self.value
class ImageKind(StrEnum):
PNG = "image/png"
JPEG = "image/jpeg"
@classmethod
def declared(cls, content_type: str | None) -> ImageKind:
media = (content_type or "").partition(";")[0].strip().lower()
try:
return cls(media)
except ValueError as error:
raise UnsupportedImage("Content-Type должен быть image/png или image/jpeg") from error
@dataclass(frozen=True, slots=True)
class ImageInfo:
kind: ImageKind
width: int
height: int