| import os |
| import shutil |
|
|
| TEMP_DIR = "temp" |
|
|
| os.makedirs(TEMP_DIR, exist_ok=True) |
|
|
|
|
| def save_uploaded_file(file): |
| if file is None: |
| return None |
|
|
| file_path = getattr(file, "name", file) |
| if not os.path.exists(file_path): |
| return None |
|
|
| destination = os.path.join( |
| TEMP_DIR, |
| os.path.basename(file_path) |
| ) |
|
|
| shutil.copy(file_path, destination) |
|
|
| return destination |
|
|
|
|
| def cleanup_temp_files(): |
| """Removes temporary files from temp/ directory.""" |
| if os.path.exists(TEMP_DIR): |
| for item in os.listdir(TEMP_DIR): |
| if item != ".gitkeep": |
| item_path = os.path.join(TEMP_DIR, item) |
| try: |
| if os.path.isfile(item_path): |
| os.remove(item_path) |
| elif os.path.isdir(item_path): |
| shutil.rmtree(item_path) |
| except Exception: |
| pass |
|
|