Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for upload flows to a folder #2844

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/backend/base/langflow/api/v1/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,25 @@ async def upload_file(
session: Session = Depends(get_session),
file: UploadFile = File(...),
current_user: User = Depends(get_current_active_user),
folder_id: UUID | None = None,
):
"""Upload flows from a file."""
contents = await file.read()
data = orjson.loads(contents)
response_list = []
if "flows" in data:
flow_list = FlowListCreate(**data)
else:
flow_list = FlowListCreate(flows=[FlowCreate(**data)])
# Now we set the user_id for all flows
for flow in flow_list.flows:
flow.user_id = current_user.id
if folder_id:
flow.folder_id = folder_id
response = create_flow(session=session, flow=flow, current_user=current_user)
response_list.append(response)

return create_flows(session=session, flow_list=flow_list, current_user=current_user)
return response_list


@router.get("/download/", response_model=FlowListRead, status_code=200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
WRONG_FILE_ERROR_ALERT,
} from "../../../constants/alerts_constants";
import { updateFlowInDatabase } from "../../../controllers/API";
import { uploadFlowsFromFolders } from "../../../pages/MainPage/services";
import {
uploadFlowToFolder,
uploadFlowsFromFolders,
} from "../../../pages/MainPage/services";
import useAlertStore from "../../../stores/alertStore";
import useFlowsManagerStore from "../../../stores/flowsManagerStore";
import { useFolderStore } from "../../../stores/foldersStore";
Expand Down Expand Up @@ -131,7 +134,8 @@ const useFileDrop = (
formData.append("file", data);
setFolderDragging(false);
setFolderIdDragging("");
uploadFlowsFromFolders(formData).then(() => {

uploadFlowToFolder(formData, folderId).then(() => {
refreshFolders();
triggerFolderChange(folderId);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import ShortUniqueId from "short-unique-id";
import {
ALLOWED_IMAGE_INPUT_EXTENSIONS,
FS_ERROR_TEXT,
SN_ERROR_TEXT,
} from "../../../../../../constants/constants";
// import useFileUpload from "./use-file-upload";

const useDragAndDrop = (
setIsDragging: (value: boolean) => void,
setFiles: (value: any) => void,
currentFlowId: string,
setErrorData: (value: any) => void,
) => {
const useDragAndDrop = (setIsDragging: (value: boolean) => void) => {
const dragOver = (e) => {
e.preventDefault();
if (e.dataTransfer.types.some((type) => type === "Files")) {
Expand Down
19 changes: 19 additions & 0 deletions src/frontend/src/pages/MainPage/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,22 @@ export async function moveFlowToFolder(
throw error;
}
}

export async function uploadFlowToFolder(
flows: FormData,
folderId: string,
): Promise<FlowType[]> {
try {
const url = `${BASE_URL_API}flows/upload/?folder_id=${encodeURIComponent(folderId)}`;

const response = await api.post(url, flows);

if (response?.status !== 201) {
throw new Error(`HTTP error! status: ${response?.status}`);
}
return response.data;
} catch (error) {
console.error(error);
throw error;
}
}