Files
relay-bot/app/attachments/service.py
T
2026-07-24 22:36:04 +03:00

31 lines
1.0 KiB
Python

from pathlib import Path
from app.attachments.extractors import SUPPORTED_EXTENSIONS, extract_text
def explicitly_requested(
*,
is_private: bool,
caption: str | None,
replied_with_request: bool,
command: bool,
bot_username: str | None,
) -> bool:
mention = bool(caption and bot_username and f"@{bot_username.lower()}" in caption.lower())
return is_private or mention or replied_with_request or command
class AttachmentService:
def __init__(self, max_bytes: int):
self.max_bytes = max_bytes
def validate(self, filename: str, size: int) -> None:
if size > self.max_bytes:
raise ValueError("Файл слишком большой")
if Path(filename).suffix.lower() not in SUPPORTED_EXTENSIONS:
raise ValueError("Неподдерживаемый или небезопасный файл")
def extract(self, filename: str, content: bytes) -> str:
self.validate(filename, len(content))
return extract_text(filename, content)