🐛 fix(endpoints.py): fix argument passed to save_uploaded_file function to correctly save the uploaded file
🐛 fix(utils.py): fix argument type in save_uploaded_file function to correctly handle UploadFile object
This commit is contained in:
parent
758d5ac9d3
commit
de4262faa6
2 changed files with 10 additions and 7 deletions
|
|
@ -159,7 +159,7 @@ async def process_flow(
|
||||||
async def create_upload_file(file: UploadFile, flow_id: str):
|
async def create_upload_file(file: UploadFile, flow_id: str):
|
||||||
# Cache file
|
# Cache file
|
||||||
try:
|
try:
|
||||||
file_path = save_uploaded_file(file.file, folder_name=flow_id)
|
file_path = save_uploaded_file(file, folder_name=flow_id)
|
||||||
|
|
||||||
return UploadFileResponse(
|
return UploadFileResponse(
|
||||||
flowId=flow_id,
|
flowId=flow_id,
|
||||||
|
|
|
||||||
15
src/backend/langflow/services/cache/utils.py
vendored
15
src/backend/langflow/services/cache/utils.py
vendored
|
|
@ -8,6 +8,7 @@ from collections import OrderedDict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
from appdirs import user_cache_dir
|
from appdirs import user_cache_dir
|
||||||
|
from fastapi import UploadFile
|
||||||
from langflow.services.database.models.base import orjson_dumps
|
from langflow.services.database.models.base import orjson_dumps
|
||||||
|
|
||||||
CACHE: Dict[str, Any] = {}
|
CACHE: Dict[str, Any] = {}
|
||||||
|
|
@ -152,7 +153,7 @@ def save_binary_file(content: str, file_name: str, accepted_types: list[str]) ->
|
||||||
|
|
||||||
|
|
||||||
@create_cache_folder
|
@create_cache_folder
|
||||||
def save_uploaded_file(file, folder_name):
|
def save_uploaded_file(file: UploadFile, folder_name):
|
||||||
"""
|
"""
|
||||||
Save an uploaded file to the specified folder with a hash of its content as the file name.
|
Save an uploaded file to the specified folder with a hash of its content as the file name.
|
||||||
|
|
||||||
|
|
@ -165,6 +166,8 @@ def save_uploaded_file(file, folder_name):
|
||||||
"""
|
"""
|
||||||
cache_path = Path(CACHE_DIR)
|
cache_path = Path(CACHE_DIR)
|
||||||
folder_path = cache_path / folder_name
|
folder_path = cache_path / folder_name
|
||||||
|
file_extension = Path(file.filename).suffix
|
||||||
|
file_object = file.file
|
||||||
|
|
||||||
# Create the folder if it doesn't exist
|
# Create the folder if it doesn't exist
|
||||||
if not folder_path.exists():
|
if not folder_path.exists():
|
||||||
|
|
@ -173,22 +176,22 @@ def save_uploaded_file(file, folder_name):
|
||||||
# Create a hash of the file content
|
# Create a hash of the file content
|
||||||
sha256_hash = hashlib.sha256()
|
sha256_hash = hashlib.sha256()
|
||||||
# Reset the file cursor to the beginning of the file
|
# Reset the file cursor to the beginning of the file
|
||||||
file.seek(0)
|
file_object.seek(0)
|
||||||
# Iterate over the uploaded file in small chunks to conserve memory
|
# Iterate over the uploaded file in small chunks to conserve memory
|
||||||
while chunk := file.read(8192): # Read 8KB at a time (adjust as needed)
|
while chunk := file_object.read(8192): # Read 8KB at a time (adjust as needed)
|
||||||
sha256_hash.update(chunk)
|
sha256_hash.update(chunk)
|
||||||
|
|
||||||
# Use the hex digest of the hash as the file name
|
# Use the hex digest of the hash as the file name
|
||||||
hex_dig = sha256_hash.hexdigest()
|
hex_dig = sha256_hash.hexdigest()
|
||||||
file_name = hex_dig
|
file_name = f"{hex_dig}{file_extension}"
|
||||||
|
|
||||||
# Reset the file cursor to the beginning of the file
|
# Reset the file cursor to the beginning of the file
|
||||||
file.seek(0)
|
file_object.seek(0)
|
||||||
|
|
||||||
# Save the file with the hash as its name
|
# Save the file with the hash as its name
|
||||||
file_path = folder_path / file_name
|
file_path = folder_path / file_name
|
||||||
with open(file_path, "wb") as new_file:
|
with open(file_path, "wb") as new_file:
|
||||||
while chunk := file.read(8192):
|
while chunk := file_object.read(8192):
|
||||||
new_file.write(chunk)
|
new_file.write(chunk)
|
||||||
|
|
||||||
return file_path
|
return file_path
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue