File size: 1,016 Bytes
99fdb5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)