cloudairambo / utils /file_utils.py
rihancloud's picture
Deploy clean FastAPI backend without node_modules
99fdb5f
Raw History Blame Contribute Delete
1.02 kB
import os
import uuid
from core.config import settings
# -------------------------------------------------
# LOW-LEVEL BYTE SAVER (NEW, CORRECT)
# -------------------------------------------------
def save_bytes_to_file(data: bytes, original_name: str):
os.makedirs(settings.TEMP_DIR, exist_ok=True)
filename = f"{uuid.uuid4().hex}_{original_name}"
path = os.path.join(settings.TEMP_DIR, filename)
with open(path, "wb") as f:
f.write(data)
if os.path.getsize(path) == 0:
raise RuntimeError("Saved file is empty")
return path
# -------------------------------------------------
# BACKWARD-COMPAT WRAPPER (IMPORTANT)
# -------------------------------------------------
def save_upload_file(file):
"""
Compatibility wrapper for existing routers.
DO NOT remove — many routers depend on this.
"""
data = file.file.read()
if not data:
raise RuntimeError("Uploaded file stream is empty")
return save_bytes_to_file(data, file.filename)