File size: 2,395 Bytes
623e14e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61b4298
623e14e
 
 
 
 
 
 
 
 
61b4298
623e14e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
"""
File handling utilities for the DOCX to PDF converter
"""

import os
import tempfile
import shutil
import logging
from pathlib import Path
from typing import Optional

logger = logging.getLogger(__name__)

class FileHandler:
    """Handle file operations for the converter"""
    
    def __init__(self, base_temp_dir: str = "/tmp/conversions"):
        self.base_temp_dir = base_temp_dir
        os.makedirs(self.base_temp_dir, exist_ok=True)
    
    def create_temp_directory(self) -> str:
        """Create a temporary directory for file processing"""
        try:
            temp_dir = tempfile.mkdtemp(dir=self.base_temp_dir)
            logger.info(f"Created temporary directory: {temp_dir}")
            return temp_dir
        except Exception as e:
            logger.error(f"Failed to create temporary directory: {e}")
            raise
    
    def save_uploaded_file(self, temp_dir: str, filename: str, content: bytes) -> str:
        """Save uploaded file to temporary directory"""
        try:
            file_path = os.path.join(temp_dir, filename)
            with open(file_path, "wb") as f:
                f.write(content)
            logger.info(f"Saved file: {file_path}")
            return file_path
        except Exception as e:
            logger.error(f"Failed to save file {filename}: {e}")
            raise
    
    def cleanup_temp_directory(self, temp_dir: str):
        """Clean up temporary directory"""
        try:
            if os.path.exists(temp_dir):
                shutil.rmtree(temp_dir)
                logger.info(f"Cleaned up temporary directory: {temp_dir}")
        except Exception as e:
            logger.error(f"Failed to cleanup directory {temp_dir}: {e}")
    
    def get_file_size(self, file_path: str) -> int:
        """Get file size in bytes"""
        try:
            return os.path.getsize(file_path)
        except Exception as e:
            logger.error(f"Failed to get file size for {file_path}: {e}")
            return 0
    
    def validate_file_extension(self, filename: str, allowed_extensions: list) -> bool:
        """Validate file extension"""
        try:
            ext = Path(filename).suffix.lower()
            return ext in allowed_extensions
        except Exception as e:
            logger.error(f"Failed to validate file extension for {filename}: {e}")
            return False