forked from mtusi2026/mtusi2026lab4
feat: ЛР4 - S3 хранилище MinIO, загрузка вложений и защита от атак
This commit is contained in:
+49
-6
@@ -5,7 +5,7 @@ import re
|
||||
from enum import Enum
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
|
||||
|
||||
def now_utc_iso() -> str:
|
||||
@@ -134,18 +134,60 @@ class ChatPageResponse(BaseModel):
|
||||
next_cursor: str | None = None
|
||||
|
||||
|
||||
class VariantResponse(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
name: str
|
||||
content_type: str
|
||||
size_bytes: int
|
||||
width: int
|
||||
height: int
|
||||
sha256: str
|
||||
|
||||
|
||||
class AttachmentResponse(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
id: str
|
||||
chat_id: str
|
||||
owner_id: str
|
||||
filename: str
|
||||
content_type: str
|
||||
size_bytes: int
|
||||
sha256: str
|
||||
status: str = "ready"
|
||||
variants: list[VariantResponse] = Field(default_factory=list)
|
||||
error_code: str | None = None
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
|
||||
class CreateMessageRequest(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
text: str = Field(min_length=1, max_length=4000)
|
||||
text: str = Field(default="", max_length=4000)
|
||||
attachment_ids: list[str] = Field(default_factory=list, max_length=10)
|
||||
|
||||
@field_validator("text")
|
||||
@field_validator("attachment_ids")
|
||||
@classmethod
|
||||
def validate_text(cls, v: str) -> str:
|
||||
if not v.strip():
|
||||
raise ValueError("Текст сообщения не может состоять только из пробелов")
|
||||
def validate_attachment_ids(cls, v: list[str]) -> list[str]:
|
||||
if len(v) != len(set(v)):
|
||||
raise ValueError("attachment_ids must contain unique items")
|
||||
for item in v:
|
||||
try:
|
||||
UUID(item)
|
||||
except ValueError:
|
||||
raise ValueError(f"Идентификатор вложения '{item}' не является валидным UUID")
|
||||
return v
|
||||
|
||||
@model_validator(mode="after")
|
||||
def check_text_or_attachment(self) -> CreateMessageRequest:
|
||||
has_text = bool(self.text and self.text.strip())
|
||||
has_attachments = bool(self.attachment_ids)
|
||||
if not has_text and not has_attachments:
|
||||
raise ValueError("Сообщение должно содержать непустой текст или хотя бы одно вложение")
|
||||
return self
|
||||
|
||||
|
||||
class MessageResponse(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
@@ -155,6 +197,7 @@ class MessageResponse(BaseModel):
|
||||
sender_id: str
|
||||
text: str
|
||||
created_at: str
|
||||
attachment_ids: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class MessagePageResponse(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user