chatdoc / app /utils /file_handler.py
celiolr's picture
Deploy: 51 arquivos (116.9 MB) - 2025-12-18 13:52:22
4dc3b79
Raw History Blame Contribute Delete
1.33 kB
import os
import tempfile
from pathlib import Path
from typing import Optional, BinaryIO
import shutil
import logging
from app.core.config import settings
logger = logging.getLogger(__name__)
class FileHandler:
def __init__(self):
self.supported_extensions = settings.supported_extensions
def is_supported_file(self, file_path: Path) -> bool:
"""Check if file extension is supported"""
return file_path.suffix.lower() in self.supported_extensions
def save_uploaded_file(self, file: BinaryIO, filename: str) -> Path:
"""Save uploaded file to temporary location"""
temp_dir = Path(tempfile.mkdtemp())
file_path = temp_dir / filename
with open(file_path, "wb") as f:
shutil.copyfileobj(file, f)
logger.debug(f"Saved uploaded file to: {file_path}")
return file_path
def cleanup_temp_files(self, file_path: Path):
"""Clean up temporary files"""
try:
if file_path.exists():
temp_dir = file_path.parent
shutil.rmtree(temp_dir)
logger.debug(f"Cleaned up temp directory: {temp_dir}")
except Exception as e:
logger.warning(f"Error cleaning up temp files: {e}")