From 7bb4ead48fe99d84b68c1a46d4608a9fb29c7941 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Mon, 26 Aug 2024 12:33:37 -0300 Subject: [PATCH 1/8] fix: improve file name generation in upload_file function to prevent files with the same name --- src/backend/base/langflow/api/v1/files.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/backend/base/langflow/api/v1/files.py b/src/backend/base/langflow/api/v1/files.py index 1e3d828b20e..9d8ed7ccd74 100644 --- a/src/backend/base/langflow/api/v1/files.py +++ b/src/backend/base/langflow/api/v1/files.py @@ -45,7 +45,9 @@ async def upload_file( try: flow_id_str = str(flow_id) file_content = await file.read() - file_name = file.filename or hashlib.sha256(file_content).hexdigest() + # get the extension of the file + file_extension = file.filename.split(".")[-1] + file_name = hashlib.sha256(file_content).hexdigest()+f".{file_extension}" folder = flow_id_str await storage_service.save_file(flow_id=folder, file_name=file_name, data=file_content) return UploadFileResponse(flowId=flow_id_str, file_path=f"{folder}/{file_name}") From 0c59b096a8c6e99ff218ece10c37b365e2719e05 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:36:53 +0000 Subject: [PATCH 2/8] [autofix.ci] apply automated fixes --- src/backend/base/langflow/api/v1/files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/base/langflow/api/v1/files.py b/src/backend/base/langflow/api/v1/files.py index 9d8ed7ccd74..0e657e04077 100644 --- a/src/backend/base/langflow/api/v1/files.py +++ b/src/backend/base/langflow/api/v1/files.py @@ -47,7 +47,7 @@ async def upload_file( file_content = await file.read() # get the extension of the file file_extension = file.filename.split(".")[-1] - file_name = hashlib.sha256(file_content).hexdigest()+f".{file_extension}" + file_name = hashlib.sha256(file_content).hexdigest() + f".{file_extension}" folder = flow_id_str await storage_service.save_file(flow_id=folder, file_name=file_name, data=file_content) return UploadFileResponse(flowId=flow_id_str, file_path=f"{folder}/{file_name}") From 0d8460bf9355e056b21d31aad1e868b3c7495348 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Mon, 26 Aug 2024 12:46:29 -0300 Subject: [PATCH 3/8] fix: improve file name generation in upload_file function --- src/backend/base/langflow/api/v1/files.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/backend/base/langflow/api/v1/files.py b/src/backend/base/langflow/api/v1/files.py index 0e657e04077..ca7af6d9123 100644 --- a/src/backend/base/langflow/api/v1/files.py +++ b/src/backend/base/langflow/api/v1/files.py @@ -1,3 +1,4 @@ +from datetime import datetime import hashlib from http import HTTPStatus from io import BytesIO @@ -45,9 +46,7 @@ async def upload_file( try: flow_id_str = str(flow_id) file_content = await file.read() - # get the extension of the file - file_extension = file.filename.split(".")[-1] - file_name = hashlib.sha256(file_content).hexdigest() + f".{file_extension}" + file_name = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "_" + (file.filename or hashlib.sha256(file_content).hexdigest()) folder = flow_id_str await storage_service.save_file(flow_id=folder, file_name=file_name, data=file_content) return UploadFileResponse(flowId=flow_id_str, file_path=f"{folder}/{file_name}") From ea156a300c89807d06558461761726eb4649a668 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:49:24 +0000 Subject: [PATCH 4/8] [autofix.ci] apply automated fixes --- src/backend/base/langflow/api/v1/files.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/backend/base/langflow/api/v1/files.py b/src/backend/base/langflow/api/v1/files.py index ca7af6d9123..87bc30b6423 100644 --- a/src/backend/base/langflow/api/v1/files.py +++ b/src/backend/base/langflow/api/v1/files.py @@ -46,7 +46,11 @@ async def upload_file( try: flow_id_str = str(flow_id) file_content = await file.read() - file_name = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + "_" + (file.filename or hashlib.sha256(file_content).hexdigest()) + file_name = ( + datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + + "_" + + (file.filename or hashlib.sha256(file_content).hexdigest()) + ) folder = flow_id_str await storage_service.save_file(flow_id=folder, file_name=file_name, data=file_content) return UploadFileResponse(flowId=flow_id_str, file_path=f"{folder}/{file_name}") From 9aacd1694b271b36822f224cac373e434c095588 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Mon, 26 Aug 2024 14:22:41 -0300 Subject: [PATCH 5/8] fix: improve file name generation in upload_file function --- src/backend/base/langflow/api/v1/files.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/backend/base/langflow/api/v1/files.py b/src/backend/base/langflow/api/v1/files.py index 87bc30b6423..5d9c89970ee 100644 --- a/src/backend/base/langflow/api/v1/files.py +++ b/src/backend/base/langflow/api/v1/files.py @@ -46,14 +46,12 @@ async def upload_file( try: flow_id_str = str(flow_id) file_content = await file.read() - file_name = ( - datetime.now().strftime("%Y-%m-%d_%H-%M-%S") - + "_" - + (file.filename or hashlib.sha256(file_content).hexdigest()) - ) + timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + file_name = file.filename or hashlib.sha256(file_content).hexdigest() + full_file_name = f"{timestamp}_{file_name}" folder = flow_id_str - await storage_service.save_file(flow_id=folder, file_name=file_name, data=file_content) - return UploadFileResponse(flowId=flow_id_str, file_path=f"{folder}/{file_name}") + await storage_service.save_file(flow_id=folder, file_name=full_file_name, data=file_content) + return UploadFileResponse(flowId=flow_id_str, file_path=f"{folder}/{full_file_name}") except Exception as e: raise HTTPException(status_code=500, detail=str(e)) From 1b29f36bafe746ab456b6a3c67f1db95ebb435f5 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Mon, 26 Aug 2024 14:36:39 -0300 Subject: [PATCH 6/8] fix: improve file path generation in test_upload_file function --- src/backend/tests/unit/test_files.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/backend/tests/unit/test_files.py b/src/backend/tests/unit/test_files.py index b3a85cf03c1..ef7cd1ecaad 100644 --- a/src/backend/tests/unit/test_files.py +++ b/src/backend/tests/unit/test_files.py @@ -1,3 +1,4 @@ +import re from unittest.mock import MagicMock import pytest @@ -29,10 +30,13 @@ def test_upload_file(client, mock_storage_service, created_api_key, flow): headers=headers, ) assert response.status_code == 201 - assert response.json() == { - "flowId": str(flow.id), - "file_path": f"{flow.id}/test.txt", - } + + response_json = response.json() + assert response_json["flowId"] == str(flow.id) + + # Check that the file_path matches the expected pattern + file_path_pattern = re.compile(rf"{flow.id}/\d{{4}}-\d{{2}}-\d{{2}}_\d{{2}}-\d{{2}}-\d{{2}}_test\.txt") + assert file_path_pattern.match(response_json["file_path"]) def test_download_file(client, mock_storage_service, created_api_key, flow): From f68f68188ca1c8b3a484f2b0a526d8dad2eddf10 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Mon, 26 Aug 2024 15:11:05 -0300 Subject: [PATCH 7/8] fix: improve file path and name generation in test_upload_file and upload_file functions --- src/backend/tests/unit/test_files.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/backend/tests/unit/test_files.py b/src/backend/tests/unit/test_files.py index ef7cd1ecaad..517275233d6 100644 --- a/src/backend/tests/unit/test_files.py +++ b/src/backend/tests/unit/test_files.py @@ -79,30 +79,33 @@ def test_file_operations(client, created_api_key, flow): headers=headers, ) assert response.status_code == 201 - assert response.json() == { - "flowId": str(flow_id), - "file_path": f"{flow_id}/{file_name}", - } + + response_json = response.json() + assert response_json["flowId"] == str(flow_id) + + # Check that the file_path matches the expected pattern + file_path_pattern = re.compile(rf"{flow_id}/\d{{4}}-\d{{2}}-\d{{2}}_\d{{2}}-\d{{2}}-\d{{2}}_{file_name}") + assert file_path_pattern.match(response_json["file_path"]) + + # Extract the full file name with timestamp from the response + full_file_name = response_json["file_path"].split('/')[-1] # Step 2: List files in the folder response = client.get(f"api/v1/files/list/{flow_id}", headers=headers) assert response.status_code == 200 - assert file_name in response.json()["files"] + assert full_file_name in response.json()["files"] # Step 3: Download the file and verify its content - - response = client.get(f"api/v1/files/download/{flow_id}/{file_name}", headers=headers) + response = client.get(f"api/v1/files/download/{flow_id}/{full_file_name}", headers=headers) assert response.status_code == 200 assert response.content == file_content - # the headers are application/octet-stream assert response.headers["content-type"] == "application/octet-stream" - # mime_type is inside media_type # Step 4: Delete the file - response = client.delete(f"api/v1/files/delete/{flow_id}/{file_name}", headers=headers) + response = client.delete(f"api/v1/files/delete/{flow_id}/{full_file_name}", headers=headers) assert response.status_code == 200 - assert response.json() == {"message": f"File {file_name} deleted successfully"} + assert response.json() == {"message": f"File {full_file_name} deleted successfully"} # Verify that the file is indeed deleted response = client.get(f"api/v1/files/list/{flow_id}", headers=headers) - assert file_name not in response.json()["files"] + assert full_file_name not in response.json()["files"] From 8e4725b5c34f4bf31125b233fa90c8d676f9f682 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:12:27 +0000 Subject: [PATCH 8/8] [autofix.ci] apply automated fixes --- src/backend/tests/unit/test_files.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/tests/unit/test_files.py b/src/backend/tests/unit/test_files.py index 517275233d6..4c4d9c73e4f 100644 --- a/src/backend/tests/unit/test_files.py +++ b/src/backend/tests/unit/test_files.py @@ -88,7 +88,7 @@ def test_file_operations(client, created_api_key, flow): assert file_path_pattern.match(response_json["file_path"]) # Extract the full file name with timestamp from the response - full_file_name = response_json["file_path"].split('/')[-1] + full_file_name = response_json["file_path"].split("/")[-1] # Step 2: List files in the folder response = client.get(f"api/v1/files/list/{flow_id}", headers=headers)