🚑 (fileUpload) Fix file upload in linked typebots
This commit is contained in:
@@ -5,6 +5,7 @@ import { generatePresignedPostPolicy } from '@typebot.io/lib/s3/generatePresigne
|
|||||||
import { env } from '@typebot.io/env'
|
import { env } from '@typebot.io/env'
|
||||||
import { InputBlockType, publicTypebotSchema } from '@typebot.io/schemas'
|
import { InputBlockType, publicTypebotSchema } from '@typebot.io/schemas'
|
||||||
import prisma from '@typebot.io/lib/prisma'
|
import prisma from '@typebot.io/lib/prisma'
|
||||||
|
import { getSession } from '@typebot.io/bot-engine/queries/getSession'
|
||||||
|
|
||||||
export const generateUploadUrl = publicProcedure
|
export const generateUploadUrl = publicProcedure
|
||||||
.meta({
|
.meta({
|
||||||
@@ -17,12 +18,19 @@ export const generateUploadUrl = publicProcedure
|
|||||||
})
|
})
|
||||||
.input(
|
.input(
|
||||||
z.object({
|
z.object({
|
||||||
filePathProps: z.object({
|
filePathProps: z
|
||||||
typebotId: z.string(),
|
.object({
|
||||||
blockId: z.string(),
|
typebotId: z.string(),
|
||||||
resultId: z.string(),
|
blockId: z.string(),
|
||||||
fileName: z.string(),
|
resultId: z.string(),
|
||||||
}),
|
fileName: z.string(),
|
||||||
|
})
|
||||||
|
.or(
|
||||||
|
z.object({
|
||||||
|
sessionId: z.string(),
|
||||||
|
fileName: z.string(),
|
||||||
|
})
|
||||||
|
),
|
||||||
fileType: z.string().optional(),
|
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',
|
'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({
|
const publicTypebot = await prisma.publicTypebot.findFirst({
|
||||||
where: {
|
where: {
|
||||||
typebotId: filePathProps.typebotId,
|
typebotId,
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
groups: true,
|
groups: true,
|
||||||
@@ -63,12 +135,14 @@ export const generateUploadUrl = publicProcedure
|
|||||||
message: "Can't find workspaceId",
|
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
|
const fileUploadBlock = publicTypebotSchema._def.schema.shape.groups
|
||||||
.parse(publicTypebot.groups)
|
.parse(publicTypebot.groups)
|
||||||
.flatMap((group) => group.blocks)
|
.flatMap((group) => group.blocks)
|
||||||
.find((block) => block.id === filePathProps.blockId)
|
.find((block) => block.id === session.state.currentBlock?.blockId)
|
||||||
|
|
||||||
if (fileUploadBlock?.type !== InputBlockType.FILE)
|
if (fileUploadBlock?.type !== InputBlockType.FILE)
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@typebot.io/js",
|
"name": "@typebot.io/js",
|
||||||
"version": "0.1.30",
|
"version": "0.1.31",
|
||||||
"description": "Javascript library to display typebots on your website",
|
"description": "Javascript library to display typebots on your website",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
|
|||||||
@@ -58,9 +58,7 @@ export const FileUploadForm = (props: Props) => {
|
|||||||
{
|
{
|
||||||
file,
|
file,
|
||||||
input: {
|
input: {
|
||||||
resultId: props.context.resultId,
|
sessionId: props.context.sessionId,
|
||||||
typebotId: props.context.typebot.id,
|
|
||||||
blockId: props.block.id,
|
|
||||||
fileName: file.name,
|
fileName: file.name,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -86,9 +84,7 @@ export const FileUploadForm = (props: Props) => {
|
|||||||
files: files.map((file) => ({
|
files: files.map((file) => ({
|
||||||
file: file,
|
file: file,
|
||||||
input: {
|
input: {
|
||||||
resultId,
|
sessionId: props.context.sessionId,
|
||||||
typebotId: props.context.typebot.id,
|
|
||||||
blockId: props.block.id,
|
|
||||||
fileName: file.name,
|
fileName: file.name,
|
||||||
},
|
},
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -5,9 +5,7 @@ type UploadFileProps = {
|
|||||||
files: {
|
files: {
|
||||||
file: File
|
file: File
|
||||||
input: {
|
input: {
|
||||||
typebotId: string
|
sessionId: string
|
||||||
blockId: string
|
|
||||||
resultId: string
|
|
||||||
fileName: string
|
fileName: string
|
||||||
}
|
}
|
||||||
}[]
|
}[]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@typebot.io/nextjs",
|
"name": "@typebot.io/nextjs",
|
||||||
"version": "0.1.30",
|
"version": "0.1.31",
|
||||||
"description": "Convenient library to display typebots on your Next.js website",
|
"description": "Convenient library to display typebots on your Next.js website",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@typebot.io/react",
|
"name": "@typebot.io/react",
|
||||||
"version": "0.1.30",
|
"version": "0.1.31",
|
||||||
"description": "Convenient library to display typebots on your React app",
|
"description": "Convenient library to display typebots on your React app",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
|||||||
Reference in New Issue
Block a user