Spaces:
Running
Running
Download app/utils/file_handler.py from Dama12/scientific-backend: direct link, hf CLI and curl.
- Browser
- Download file 975 Bytes
-
https://huggingface.co/spaces/Dama12/scientific-backend/resolve/main/app/utils/file_handler.py
- Command line
-
hf download hf://spaces/Dama12/scientific-backend/app/utils/file_handler.py
-
curl -L -o file_handler.py https://huggingface.co/spaces/Dama12/scientific-backend/resolve/main/app/utils/file_handler.py
975 Bytes
| """ | |
| Utilitaires fichiers | |
| """ | |
| import os | |
| from typing import Optional | |
| from app.core.settings import settings | |
| class FileHandler: | |
| """Gestion des fichiers""" | |
| def ensure_upload_dir(): | |
| """Crée le répertoire d'upload s'il n'existe pas""" | |
| os.makedirs(settings.UPLOAD_DIR, exist_ok=True) | |
| def save_uploaded_file(file_content: bytes, filename: str) -> str: | |
| """Sauvegarde un fichier uploadé""" | |
| FileHandler.ensure_upload_dir() | |
| file_path = os.path.join(settings.UPLOAD_DIR, filename) | |
| with open(file_path, 'wb') as f: | |
| f.write(file_content) | |
| return file_path | |
| def delete_file(file_path: str) -> bool: | |
| """Supprime un fichier""" | |
| try: | |
| if os.path.exists(file_path): | |
| os.remove(file_path) | |
| return True | |
| except Exception: | |
| pass | |
| return False | |