Files
PrimoPiarBot/code/media.py
2025-10-03 09:31:12 +07:00

38 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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