Fix thread safety, implement prompt, fix resource leaks, add history rotation

- Add threading.Lock around asr_pipeline calls (thread-safe inference on shared GPU)
- Implement prompt parameter: propagate through process_file → transcribe → generate_kwargs
- Fix prompt_ids device mismatch (.to(self.device) for CUDA)
- Replace mkdtemp() with create_temp_file() in sources.py (no more leaked /tmp dirs)
- Add max_history_days (default 30) with automatic cleanup of old history directories
- Add intentional comments for SSRF and /config decisions (internal service)
- Lower normalize_audio log level from INFO to DEBUG

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Serge Zaigraeff
2026-03-31 00:43:06 +03:00
co-authored by Claude Sonnet 4.6
parent 2138651474
commit 1e778cae86
7 changed files with 171 additions and 87 deletions
+22 -16
View File
@@ -4,8 +4,6 @@
"""
import os
import uuid
import tempfile
import base64
from urllib.parse import urlparse
import magic
@@ -13,6 +11,8 @@ import requests
from typing import Tuple, Optional
import logging
from ..infrastructure.storage import create_temp_file
logger = logging.getLogger('app.audio_sources')
@@ -23,12 +23,6 @@ def _check_size(size_bytes: int, max_size_mb: int) -> Optional[str]:
return None
def _make_temp_path(suffix: str = ".wav") -> str:
"""Создаёт путь для временного файла."""
temp_dir = tempfile.mkdtemp()
return os.path.join(temp_dir, f"{uuid.uuid4()}{suffix}")
def get_uploaded_file(request_files, max_file_size_mb: int = 100) -> Tuple[Optional[str], Optional[str], Optional[str]]:
"""
Получает аудиофайл из загруженных файлов Flask.
@@ -54,7 +48,7 @@ def get_uploaded_file(request_files, max_file_size_mb: int = 100) -> Tuple[Optio
return None, None, error
# Сохраняем во временный файл
temp_path = _make_temp_path()
temp_path = create_temp_file()
file.save(temp_path)
return temp_path, file.filename, None
@@ -72,6 +66,8 @@ def get_url_file(url: str, max_file_size_mb: int = 100) -> Tuple[Optional[str],
if parsed.scheme not in ('http', 'https'):
return None, None, f"Unsupported URL scheme: {parsed.scheme}. Only http/https allowed"
# SSRF-защита (валидация private IP) не добавлена сознательно:
# сервис работает во внутренней сети и недоступен извне
response = requests.get(url, stream=True, timeout=30)
response.raise_for_status()
@@ -92,15 +88,23 @@ def get_url_file(url: str, max_file_size_mb: int = 100) -> Tuple[Optional[str],
if url_path:
original_name = os.path.basename(url_path)
temp_path = _make_temp_path()
temp_path = create_temp_file()
max_bytes = max_file_size_mb * 1024 * 1024
total_size = 0
with open(temp_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
total_size += len(chunk)
if total_size > max_bytes:
f.close()
os.remove(temp_path)
return None, None, f"File exceeds maximum size of {max_file_size_mb}MB"
f.write(chunk)
return temp_path, original_name or os.path.basename(temp_path), None
except Exception as e:
logger.error(f"Ошибка при получении файла по URL {url}: {e}")
logger.error("Ошибка при получении файла по URL %s: %s", url, e)
return None, None, f"Error retrieving file from URL: {str(e)}"
@@ -112,12 +116,14 @@ def get_base64_file(base64_data: str, max_file_size_mb: int = 100) -> Tuple[Opti
Кортеж (путь к temp-файлу, имя файла, сообщение об ошибке).
"""
try:
audio_data = base64.b64decode(base64_data)
error = _check_size(len(audio_data), max_file_size_mb)
# Оценка размера до декодирования: base64 кодирует 3 байта в 4 символа
estimated_size = len(base64_data) * 3 // 4
error = _check_size(estimated_size, max_file_size_mb)
if error:
return None, None, error
audio_data = base64.b64decode(base64_data)
# Определяем формат по содержимому
mime_to_ext = {
"audio/mpeg": ".mp3",
@@ -131,12 +137,12 @@ def get_base64_file(base64_data: str, max_file_size_mb: int = 100) -> Tuple[Opti
detected_mime = magic.from_buffer(audio_data[:1024], mime=True)
suffix = mime_to_ext.get(detected_mime, ".wav")
temp_path = _make_temp_path(suffix)
temp_path = create_temp_file(suffix)
with open(temp_path, 'wb') as f:
f.write(audio_data)
return temp_path, os.path.basename(temp_path), None
except Exception as e:
logger.error(f"Ошибка при декодировании base64 данных: {e}")
logger.error("Ошибка при декодировании base64 данных: %s", e)
return None, None, f"Error decoding base64 data: {str(e)}"