Download app/utils/file_handler.py from aionaion/chatdoc: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/spaces/aionaion/chatdoc/resolve/main/app/utils/file_handler.py
- Command line
-
hf download hf://spaces/aionaion/chatdoc/app/utils/file_handler.py
-
curl -L -o file_handler.py https://huggingface.co/spaces/aionaion/chatdoc/resolve/main/app/utils/file_handler.py
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}") | |