From 7196fe603a03c752c091d06e4c343771dbb37e7d Mon Sep 17 00:00:00 2001 From: dddennnisss Date: Fri, 14 Aug 2026 11:49:25 +0700 Subject: [PATCH] =?UTF-8?q?handlers.py:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20import=20smtplib,=20=D0=B4=D0=B5=D1=82?= =?UTF-8?q?=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B3?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- photo_bot/handlers.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/photo_bot/handlers.py b/photo_bot/handlers.py index f8f8a3d..033576b 100644 --- a/photo_bot/handlers.py +++ b/photo_bot/handlers.py @@ -2,6 +2,7 @@ import os import asyncio import logging +import smtplib import time from datetime import datetime from email.mime.text import MIMEText @@ -102,6 +103,7 @@ def log_and_notify_photo_error(user_id: str, action: str, error_details: str): async def handle_photo_error(msg: Message, user_id: str, action: str, error_details: str): """Вызывает системное логирование и выдает заглушку пользователю""" + logger.error(f"🔥 Ошибка фото-бота для {user_id}, действие: {action}: {error_details}") await asyncio.to_thread(log_and_notify_photo_error, user_id, action, error_details) await msg.answer(photo_error_handling(), parse_mode="html") @@ -218,20 +220,34 @@ async def photo_module_handler(msg: Message): # --- ПРИЕМ НОВОГО ФОТО --- if is_attachment: file_id = getattr(msg.content, "file_id", None) - raw_path = None + raw_bytes = None await msg.answer(PHOTO_PROCESSING_FACE_TEXT, parse_mode="html") try: downloaded_file = await states.tc_bot.download_file_by_id(file_id) - raw_path = downloaded_file + logger.info(f"Скачан файл для {user_login}: type={type(downloaded_file)}, len={len(downloaded_file) if isinstance(downloaded_file, (bytes, str)) else 'N/A'}") - # Читаем байты - with open(raw_path, 'rb') as f: - image_bytes = f.read() + # Скачиваем в байты напрямую + if isinstance(downloaded_file, bytes): + raw_bytes = downloaded_file + elif isinstance(downloaded_file, str) and os.path.exists(downloaded_file): + with open(downloaded_file, 'rb') as f: + raw_bytes = f.read() + else: + # Пробуем как путь + raw_path = downloaded_file if isinstance(downloaded_file, str) else None + if raw_path and os.path.exists(raw_path): + with open(raw_path, 'rb') as f: + raw_bytes = f.read() + else: + raise Exception(f"Не удалось получить байты файла: {type(downloaded_file)}") + + logger.info(f"Прочитано {len(raw_bytes)} байт для {user_login}") # Ищем лицо и получаем координаты - coords = await asyncio.to_thread(get_crop_coordinates, image_bytes) + coords = await asyncio.to_thread(get_crop_coordinates, raw_bytes) + logger.info(f"get_crop_coordinates вернул: {coords} для {user_login}") if not coords: await msg.answer( @@ -239,8 +255,6 @@ async def photo_module_handler(msg: Message): f"{EMOJI_DIGITS['0']} — В главное меню", parse_mode="html" ) - if os.path.exists(raw_path): - os.remove(raw_path) return # Генерируем превью @@ -290,11 +304,6 @@ async def photo_module_handler(msg: Message): parse_mode="html" ) except Exception as e: + logger.error(f"🔥 Полная ошибка: {e}", exc_info=True) await handle_photo_error(msg, user_id, "Скачивание и обработка файла", str(e)) - finally: - if raw_path and os.path.exists(raw_path): - try: - os.remove(raw_path) - except Exception as cleanup_err: - await asyncio.to_thread(log_and_notify_photo_error, user_id, "Очистка временных файлов", str(cleanup_err)) return