diff --git a/src/backend/base/langflow/api/v1/folders.py b/src/backend/base/langflow/api/v1/folders.py index 7402881c7..d55f9bd15 100644 --- a/src/backend/base/langflow/api/v1/folders.py +++ b/src/backend/base/langflow/api/v1/folders.py @@ -1,5 +1,7 @@ from typing import List +from langflow.helpers.flow import generate_unique_flow_name +from langflow.helpers.folders import generate_unique_folder_name import orjson from fastapi import APIRouter, Depends, File, HTTPException, Response, UploadFile, status from sqlalchemy import or_, update @@ -203,16 +205,9 @@ async def upload_file( if not data: raise HTTPException(status_code=400, detail="No flows found in the file") - folder_results = session.exec( - select(Folder).where( - Folder.name == data["folder_name"], - Folder.user_id == current_user.id, - ) - ) - existing_folder_names = [folder.name for folder in folder_results] + folder_name = generate_unique_folder_name(data["folder_name"], current_user.id, session) - if existing_folder_names: - data["folder_name"] = f"{data['folder_name']} ({len(existing_folder_names) + 1})" + data["folder_name"] = folder_name folder = FolderCreate(name=data["folder_name"], description=data["folder_description"]) @@ -232,6 +227,8 @@ async def upload_file( raise HTTPException(status_code=400, detail="No flows found in the data") # Now we set the user_id for all flows for flow in flow_list.flows: + flow_name = generate_unique_flow_name(flow.name, current_user.id, session) + flow.name = flow_name flow.user_id = current_user.id flow.folder_id = new_folder.id diff --git a/src/backend/base/langflow/helpers/flow.py b/src/backend/base/langflow/helpers/flow.py index 7eb901274..6ce3b2048 100644 --- a/src/backend/base/langflow/helpers/flow.py +++ b/src/backend/base/langflow/helpers/flow.py @@ -259,3 +259,24 @@ def get_flow_by_id_or_endpoint_name( raise HTTPException(status_code=404, detail=f"Flow identifier {flow_id_or_name} not found") return flow + + +def generate_unique_flow_name(flow_name, user_id, session): + original_name = flow_name + n = 1 + while True: + # Check if a flow with the given name exists + existing_flow = session.exec( + select(Flow).where( + Flow.name == flow_name, + Flow.user_id == user_id, + ) + ).first() + + # If no flow with the given name exists, return the name + if not existing_flow: + return flow_name + + # If a flow with the name already exists, append (n) to the name and increment n + flow_name = f"{original_name} ({n})" + n += 1 \ No newline at end of file diff --git a/src/backend/base/langflow/helpers/folders.py b/src/backend/base/langflow/helpers/folders.py new file mode 100644 index 000000000..fa6f27fcc --- /dev/null +++ b/src/backend/base/langflow/helpers/folders.py @@ -0,0 +1,23 @@ +from langflow.services.database.models.folder.model import Folder +from sqlalchemy import select + + +def generate_unique_folder_name(folder_name, user_id, session): + original_name = folder_name + n = 1 + while True: + # Check if a folder with the given name exists + existing_folder = session.exec( + select(Folder).where( + Folder.name == folder_name, + Folder.user_id == user_id, + ) + ).first() + + # If no folder with the given name exists, return the name + if not existing_folder: + return folder_name + + # If a folder with the name already exists, append (n) to the name and increment n + folder_name = f"{original_name} ({n})" + n += 1 \ No newline at end of file