🚑 (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 { 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({
|
||||
|
@ -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",
|
||||
|
@ -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,
|
||||
},
|
||||
})),
|
||||
|
@ -5,9 +5,7 @@ type UploadFileProps = {
|
||||
files: {
|
||||
file: File
|
||||
input: {
|
||||
typebotId: string
|
||||
blockId: string
|
||||
resultId: string
|
||||
sessionId: string
|
||||
fileName: string
|
||||
}
|
||||
}[]
|
||||
|
@ -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",
|
||||
|
@ -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",
|
||||
|
Reference in New Issue
Block a user