🚑 (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({
|
||||
|
||||
Reference in New Issue
Block a user