diff --git a/apps/viewer/src/features/fileUpload/api/generateUploadUrl.ts b/apps/viewer/src/features/fileUpload/api/generateUploadUrl.ts index d60d2a521..1ef6061d5 100644 --- a/apps/viewer/src/features/fileUpload/api/generateUploadUrl.ts +++ b/apps/viewer/src/features/fileUpload/api/generateUploadUrl.ts @@ -5,6 +5,7 @@ import { generatePresignedPostPolicy } from '@typebot.io/lib/s3/generatePresigne import { env } from '@typebot.io/env' import { InputBlockType, publicTypebotSchema } from '@typebot.io/schemas' import prisma from '@typebot.io/lib/prisma' +import { getSession } from '@typebot.io/bot-engine/queries/getSession' export const generateUploadUrl = publicProcedure .meta({ @@ -17,12 +18,19 @@ export const generateUploadUrl = publicProcedure }) .input( z.object({ - filePathProps: z.object({ - typebotId: z.string(), - blockId: z.string(), - resultId: z.string(), - fileName: z.string(), - }), + filePathProps: z + .object({ + typebotId: z.string(), + blockId: z.string(), + resultId: z.string(), + fileName: z.string(), + }) + .or( + z.object({ + sessionId: z.string(), + fileName: z.string(), + }) + ), fileType: z.string().optional(), }) ) @@ -41,9 +49,73 @@ export const generateUploadUrl = publicProcedure 'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY', }) + // TODO: Remove (deprecated) + if ('typebotId' in filePathProps) { + const publicTypebot = await prisma.publicTypebot.findFirst({ + where: { + typebotId: filePathProps.typebotId, + }, + select: { + groups: true, + typebot: { + select: { + workspaceId: true, + }, + }, + }, + }) + + const workspaceId = publicTypebot?.typebot.workspaceId + + if (!workspaceId) + throw new TRPCError({ + code: 'BAD_REQUEST', + message: "Can't find workspaceId", + }) + + const filePath = `public/workspaces/${workspaceId}/typebots/${filePathProps.typebotId}/results/${filePathProps.resultId}/${filePathProps.fileName}` + + const fileUploadBlock = publicTypebotSchema._def.schema.shape.groups + .parse(publicTypebot.groups) + .flatMap((group) => group.blocks) + .find((block) => block.id === filePathProps.blockId) + + if (fileUploadBlock?.type !== InputBlockType.FILE) + throw new TRPCError({ + code: 'BAD_REQUEST', + message: "Can't find file upload block", + }) + + const presignedPostPolicy = await generatePresignedPostPolicy({ + fileType, + filePath, + maxFileSize: + fileUploadBlock.options.sizeLimit ?? + env.NEXT_PUBLIC_BOT_FILE_UPLOAD_MAX_SIZE, + }) + + return { + presignedUrl: presignedPostPolicy.postURL, + formData: presignedPostPolicy.formData, + fileUrl: env.S3_PUBLIC_CUSTOM_DOMAIN + ? `${env.S3_PUBLIC_CUSTOM_DOMAIN}/${filePath}` + : `${presignedPostPolicy.postURL}/${presignedPostPolicy.formData.key}`, + } + } + + const session = await getSession(filePathProps.sessionId) + + if (!session) + throw new TRPCError({ + code: 'BAD_REQUEST', + message: "Can't find session", + }) + + const typebotId = session.state.typebotsQueue[0].typebot.id + const publicTypebot = await prisma.publicTypebot.findFirst({ where: { - typebotId: filePathProps.typebotId, + typebotId, }, select: { groups: true, @@ -63,12 +135,14 @@ export const generateUploadUrl = publicProcedure message: "Can't find workspaceId", }) - const filePath = `public/workspaces/${workspaceId}/typebots/${filePathProps.typebotId}/results/${filePathProps.resultId}/${filePathProps.fileName}` + const resultId = session.state.typebotsQueue[0].resultId + + const filePath = `public/workspaces/${workspaceId}/typebots/${typebotId}/results/${resultId}/${filePathProps.fileName}` const fileUploadBlock = publicTypebotSchema._def.schema.shape.groups .parse(publicTypebot.groups) .flatMap((group) => group.blocks) - .find((block) => block.id === filePathProps.blockId) + .find((block) => block.id === session.state.currentBlock?.blockId) if (fileUploadBlock?.type !== InputBlockType.FILE) throw new TRPCError({ diff --git a/packages/embeds/js/package.json b/packages/embeds/js/package.json index a2b7321a4..7b3e77b16 100644 --- a/packages/embeds/js/package.json +++ b/packages/embeds/js/package.json @@ -1,6 +1,6 @@ { "name": "@typebot.io/js", - "version": "0.1.30", + "version": "0.1.31", "description": "Javascript library to display typebots on your website", "type": "module", "main": "dist/index.js", diff --git a/packages/embeds/js/src/features/blocks/inputs/fileUpload/components/FileUploadForm.tsx b/packages/embeds/js/src/features/blocks/inputs/fileUpload/components/FileUploadForm.tsx index 3bcd29e05..5d98d449c 100644 --- a/packages/embeds/js/src/features/blocks/inputs/fileUpload/components/FileUploadForm.tsx +++ b/packages/embeds/js/src/features/blocks/inputs/fileUpload/components/FileUploadForm.tsx @@ -58,9 +58,7 @@ export const FileUploadForm = (props: Props) => { { file, input: { - resultId: props.context.resultId, - typebotId: props.context.typebot.id, - blockId: props.block.id, + sessionId: props.context.sessionId, fileName: file.name, }, }, @@ -86,9 +84,7 @@ export const FileUploadForm = (props: Props) => { files: files.map((file) => ({ file: file, input: { - resultId, - typebotId: props.context.typebot.id, - blockId: props.block.id, + sessionId: props.context.sessionId, fileName: file.name, }, })), diff --git a/packages/embeds/js/src/features/blocks/inputs/fileUpload/helpers/uploadFiles.ts b/packages/embeds/js/src/features/blocks/inputs/fileUpload/helpers/uploadFiles.ts index 2792739c6..d94bb09e6 100644 --- a/packages/embeds/js/src/features/blocks/inputs/fileUpload/helpers/uploadFiles.ts +++ b/packages/embeds/js/src/features/blocks/inputs/fileUpload/helpers/uploadFiles.ts @@ -5,9 +5,7 @@ type UploadFileProps = { files: { file: File input: { - typebotId: string - blockId: string - resultId: string + sessionId: string fileName: string } }[] diff --git a/packages/embeds/nextjs/package.json b/packages/embeds/nextjs/package.json index 6ce9722c8..e5f3503d3 100644 --- a/packages/embeds/nextjs/package.json +++ b/packages/embeds/nextjs/package.json @@ -1,6 +1,6 @@ { "name": "@typebot.io/nextjs", - "version": "0.1.30", + "version": "0.1.31", "description": "Convenient library to display typebots on your Next.js website", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/embeds/react/package.json b/packages/embeds/react/package.json index bb8405942..eeaec2afc 100644 --- a/packages/embeds/react/package.json +++ b/packages/embeds/react/package.json @@ -1,6 +1,6 @@ { "name": "@typebot.io/react", - "version": "0.1.30", + "version": "0.1.31", "description": "Convenient library to display typebots on your React app", "main": "dist/index.js", "types": "dist/index.d.ts",