- Remove dead code: cache.py, context_managers.py, decorators.py, flask-limiter - Replace AudioSource ABC + FakeFile (5 classes) with 4 plain functions - Replace TempFileManager class with create_temp_file/cleanup_temp_files functions - Simplify RequestLogger from 233 to 65 lines, remove file reading side-effect - Convert HistoryLogger and AudioUtils classes to module-level functions - Remove unused speed_up_audio and audio_speed_factor config - Flatten single-file directories: shared/, api/, storage/, validation/, async_tasks/, logging/ - Merge logging config + request_logger into single infrastructure/log.py - Fix request_logging config key (was request_logger) - Trim CLAUDE.md to high-level only Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
"""
|
|
Утилиты для управления временными файлами.
|
|
"""
|
|
|
|
import os
|
|
import uuid
|
|
import tempfile
|
|
import logging
|
|
|
|
logger = logging.getLogger('app.file_manager')
|
|
|
|
|
|
def create_temp_file(suffix: str = ".wav") -> str:
|
|
"""
|
|
Создаёт временный файл с уникальным именем.
|
|
|
|
Args:
|
|
suffix: Расширение временного файла.
|
|
|
|
Returns:
|
|
Путь к временному файлу.
|
|
"""
|
|
temp_dir = tempfile.mkdtemp()
|
|
temp_path = os.path.join(temp_dir, f"{uuid.uuid4()}{suffix}")
|
|
logger.debug(f"Создан временный файл: {temp_path}")
|
|
return temp_path
|
|
|
|
|
|
def cleanup_temp_files(file_paths: list) -> None:
|
|
"""
|
|
Удаляет временные файлы и их директории.
|
|
|
|
Args:
|
|
file_paths: Список путей к файлам для удаления.
|
|
"""
|
|
for path in file_paths:
|
|
try:
|
|
if os.path.exists(path):
|
|
os.remove(path)
|
|
logger.debug(f"Удалён временный файл: {path}")
|
|
|
|
temp_dir = os.path.dirname(path)
|
|
if os.path.exists(temp_dir) and not os.listdir(temp_dir):
|
|
os.rmdir(temp_dir)
|
|
except Exception as e:
|
|
logger.warning(f"Не удалось очистить временный файл {path}: {e}")
|