This commit is contained in:
2025-10-03 09:31:12 +07:00
commit 26c59ffb82
15 changed files with 573 additions and 0 deletions

37
code/media.py Normal file
View File

@@ -0,0 +1,37 @@
from glob import glob
from loguru import logger
from typing import Optional
from .config import settings
__all__ = ("find_photo",)
class PhotoCache:
_cache: Optional[bytes] = None
@classmethod
async def find_photo(cls, file: str = None) -> bytes:
"""
Загружает фото в память и возвращает его как байты.
"""
if cls._cache:
return cls._cache
pattern: str = file or settings.DEFAULT_PHOTO
files: list[str] = glob(pattern)
if not files:
logger.bind(user="@Console").error(f"Файл {pattern} не найден.")
raise FileNotFoundError(f"Файл {pattern} не найден.")
chosen_file: str = files[0]
logger.bind(user="@Console").info(f"Выбран файл: {chosen_file}")
with open(chosen_file, "rb") as f:
cls._cache = f.read()
return cls._cache
# Создаем функцию для обратной совместимости
find_photo = PhotoCache.find_photo