File size: 1,458 Bytes
c954778
 
d682eb6
c954778
 
 
4e74ede
60a515a
4e74ede
60a515a
 
 
 
 
 
 
 
 
 
2c199ae
 
c954778
59904b6
983f3dc
60a515a
4e74ede
 
60a515a
5db014f
4e74ede
60a515a
4e74ede
 
c954778
60a515a
4e74ede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
import os
import shutil
import tempfile

from fastapi import UploadFile

UPLOAD_DIR = "./data/uploads"
# UPLOAD_DIR = "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)

# def save_upload_file(file: UploadFile) -> str:
#     file_path = os.path.join(UPLOAD_DIR, file.filename)
#     with open(file_path, "wb") as buffer:
#         shutil.copyfileobj(file.file, buffer)
#     # print(f"File path: {file_path.split(',')[0]}{file_path.split(',')[-1]}")
#     return file_path


# Use ./data/uploads for writable folder inside Hugging Face Spaces
# UPLOAD_DIR = "./uploads"
# os.makedirs(UPLOAD_DIR, exist_ok=True)


def save_upload_image(file: UploadFile) -> str:
    """
    Save an uploaded file to the UPLOAD_DIR
    Returns the full file path
    """
    print("Running save upload image")
    file_path = os.path.join(UPLOAD_DIR, file.filename)

    # Save file
    with open(file_path, "wb") as buffer:
        shutil.copyfileobj(file.file, buffer)

    return file_path


# def save_upload_image(file: UploadFile) -> str:
#     """
#     Save an uploaded file to a temporary folder and return the file path.
#     The file will exist only during the app runtime and does not persist.
#     """
#     # Create a temporary file
#     temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f"_{file.filename}")
#
#     # Save the uploaded file
#     with temp_file as buffer:
#         shutil.copyfileobj(file.file, buffer)
#
#     return temp_file.name