Spaces:
Sleeping
Sleeping
Edit to log when uploading and create upload folder
Browse files- app/utils/file_utils.py +23 -22
app/utils/file_utils.py
CHANGED
|
@@ -4,8 +4,9 @@ import tempfile
|
|
| 4 |
|
| 5 |
from fastapi import UploadFile
|
| 6 |
|
|
|
|
| 7 |
# UPLOAD_DIR = "uploads"
|
| 8 |
-
|
| 9 |
|
| 10 |
# def save_upload_file(file: UploadFile) -> str:
|
| 11 |
# file_path = os.path.join(UPLOAD_DIR, file.filename)
|
|
@@ -20,30 +21,30 @@ from fastapi import UploadFile
|
|
| 20 |
# os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 21 |
|
| 22 |
|
| 23 |
-
# def save_upload_file(file: UploadFile) -> str:
|
| 24 |
-
# """
|
| 25 |
-
# Save an uploaded file to the UPLOAD_DIR
|
| 26 |
-
# Returns the full file path
|
| 27 |
-
# """
|
| 28 |
-
# file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 29 |
-
#
|
| 30 |
-
# # Save file
|
| 31 |
-
# with open(file_path, "wb") as buffer:
|
| 32 |
-
# shutil.copyfileobj(file.file, buffer)
|
| 33 |
-
#
|
| 34 |
-
# return file_path
|
| 35 |
-
|
| 36 |
-
|
| 37 |
def save_upload_image(file: UploadFile) -> str:
|
| 38 |
"""
|
| 39 |
-
Save an uploaded file to
|
| 40 |
-
|
| 41 |
"""
|
| 42 |
-
|
| 43 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f"_{file.filename}")
|
| 44 |
|
| 45 |
-
# Save
|
| 46 |
-
with
|
| 47 |
shutil.copyfileobj(file.file, buffer)
|
| 48 |
|
| 49 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
from fastapi import UploadFile
|
| 6 |
|
| 7 |
+
UPLOAD_DIR = "./data/uploads"
|
| 8 |
# UPLOAD_DIR = "uploads"
|
| 9 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 10 |
|
| 11 |
# def save_upload_file(file: UploadFile) -> str:
|
| 12 |
# file_path = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
| 21 |
# os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def save_upload_image(file: UploadFile) -> str:
|
| 25 |
"""
|
| 26 |
+
Save an uploaded file to the UPLOAD_DIR
|
| 27 |
+
Returns the full file path
|
| 28 |
"""
|
| 29 |
+
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
|
|
|
| 30 |
|
| 31 |
+
# Save file
|
| 32 |
+
with open(file_path, "wb") as buffer:
|
| 33 |
shutil.copyfileobj(file.file, buffer)
|
| 34 |
|
| 35 |
+
return file_path
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# def save_upload_image(file: UploadFile) -> str:
|
| 39 |
+
# """
|
| 40 |
+
# Save an uploaded file to a temporary folder and return the file path.
|
| 41 |
+
# The file will exist only during the app runtime and does not persist.
|
| 42 |
+
# """
|
| 43 |
+
# # Create a temporary file
|
| 44 |
+
# temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f"_{file.filename}")
|
| 45 |
+
#
|
| 46 |
+
# # Save the uploaded file
|
| 47 |
+
# with temp_file as buffer:
|
| 48 |
+
# shutil.copyfileobj(file.file, buffer)
|
| 49 |
+
#
|
| 50 |
+
# return temp_file.name
|