commit
This commit is contained in:
37
code/media.py
Normal file
37
code/media.py
Normal 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
|
||||
Reference in New Issue
Block a user