File size: 941 Bytes
959c484 | 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 | 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
|