File size: 1,325 Bytes
4dc3b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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}")