Download src/utils/file_handler.py from fokan/pdf-4: direct link, hf CLI and curl.
- Browser
- Download file 2.4 kB
-
https://huggingface.co/spaces/fokan/pdf-4/resolve/main/src/utils/file_handler.py
- Command line
-
hf download hf://spaces/fokan/pdf-4/src/utils/file_handler.py
-
curl -L -o file_handler.py https://huggingface.co/spaces/fokan/pdf-4/resolve/main/src/utils/file_handler.py
2.4 kB
| #!/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 |