From 5ad0bc6b65354063eec1baa48571faeb34d36481 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 21 Jun 2023 10:27:56 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(endpoints.py):=20add=20try-e?= =?UTF-8?q?xcept=20block=20to=20handle=20exceptions=20when=20saving=20uplo?= =?UTF-8?q?aded=20files=20=E2=9C=A8=20feat(utils.py):=20remove=20unused=20?= =?UTF-8?q?functions=20and=20add=20docstring=20to=20save=5Fuploaded=5Ffile?= =?UTF-8?q?=20function=20The=20try-except=20block=20in=20the=20create=5Fup?= =?UTF-8?q?load=5Ffile=20function=20handles=20exceptions=20that=20may=20oc?= =?UTF-8?q?cur=20when=20saving=20uploaded=20files.=20The=20save=5Fuploaded?= =?UTF-8?q?=5Ffile=20function=20now=20has=20a=20docstring=20that=20explain?= =?UTF-8?q?s=20its=20purpose=20and=20returns=20the=20path=20to=20the=20sav?= =?UTF-8?q?ed=20file.=20The=20unused=20functions=20save=5Fcache=20and=20lo?= =?UTF-8?q?ad=5Fcache=20have=20been=20removed.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/endpoints.py | 13 ++++++---- src/backend/langflow/cache/utils.py | 30 ++++++++---------------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/backend/langflow/api/v1/endpoints.py b/src/backend/langflow/api/v1/endpoints.py index e822bdf31..4a1cf8eae 100644 --- a/src/backend/langflow/api/v1/endpoints.py +++ b/src/backend/langflow/api/v1/endpoints.py @@ -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__} - diff --git a/src/backend/langflow/cache/utils.py b/src/backend/langflow/cache/utils.py index 9fe5a5c41..3fa95a3d6 100644 --- a/src/backend/langflow/cache/utils.py +++ b/src/backend/langflow/cache/utils.py @@ -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