🐛 fix(endpoints.py): add try-except block to handle exceptions when saving uploaded files

 feat(utils.py): remove unused functions and add docstring to save_uploaded_file function
The try-except block in the create_upload_file function handles exceptions that may occur when saving uploaded files. The save_uploaded_file function now has a docstring that explains its purpose and returns the path to the saved file. The unused functions save_cache and load_cache have been removed.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-21 10:27:56 -03:00
commit 5ad0bc6b65
2 changed files with 19 additions and 24 deletions

View file

@ -1,3 +1,4 @@
from langflow.cache.utils import save_uploaded_file
from langflow.database.models.flow import Flow
from langflow.processing.process import process_graph_cached, process_tweaks
from langflow.utils.logger import logger
@ -55,12 +56,17 @@ async def predict_flow(
logger.exception(e)
raise HTTPException(status_code=500, detail=str(e)) from e
@router.post("/upload/{client_id}")
@router.post("/upload/{client_id}", response_model=dict, status_code=201)
async def create_upload_file(file: UploadFile, client_id: str):
# Cache file
file_path = save_uploaded_file(file.file, file_name=client_id)
try:
file_path = save_uploaded_file(file.file, file_name=client_id)
return {"file_path": file_path}
return {"file_path": file_path}
except Exception as exc:
logger.error(f"Error saving file: {exc}")
raise HTTPException(status_code=500, detail=str(exc)) from exc
# get endpoint to return version of langflow
@ -69,4 +75,3 @@ def get_version():
from langflow import __version__
return {"version": __version__}

View file

@ -134,9 +134,18 @@ def save_binary_file(content: str, file_name: str, accepted_types: list[str]) ->
return file_path
@create_cache_folder
def save_uploaded_file(file, file_name):
"""
Save an uploaded file to the specified folder.
Args:
file: The uploaded file object.
file_name: The name of the file, including its extension.
Returns:
The path to the saved file.
"""
cache_path = Path(tempfile.gettempdir()) / PREFIX
file_path = cache_path / file_name
@ -146,22 +155,3 @@ def save_uploaded_file(file, file_name):
new_file.write(chunk)
return file_path
@create_cache_folder
def save_cache(hash_val: str, chat_data, clean_old_cache_files: bool):
cache_path = Path(tempfile.gettempdir()) / PREFIX / f"{hash_val}.dill"
with cache_path.open("wb") as cache_file:
dill.dump(chat_data, cache_file)
if clean_old_cache_files:
clear_old_cache_files()
@create_cache_folder
def load_cache(hash_val):
cache_path = Path(tempfile.gettempdir()) / PREFIX / f"{hash_val}.dill"
if cache_path.exists():
with cache_path.open("rb") as cache_file:
return dill.load(cache_file)
return None