Skip to content

Commit

Permalink
🚑 (fileUpload) Fix file upload in linked typebots
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Sep 26, 2023
1 parent b81fcf0 commit 7b3cbdb
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 21 deletions.
92 changes: 83 additions & 9 deletions apps/viewer/src/features/fileUpload/api/generateUploadUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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(),
})
)
Expand All @@ -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,
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand All @@ -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,
},
})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ type UploadFileProps = {
files: {
file: File
input: {
typebotId: string
blockId: string
resultId: string
sessionId: string
fileName: string
}
}[]
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

4 comments on commit 7b3cbdb

@vercel
Copy link

@vercel vercel bot commented on 7b3cbdb Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

bot.boston-voip.com
bot.cabinpromos.com
bot.carnaval.studio
bot.digitalbled.com
bot.dsignagency.com
bot.enthrallart.com
bot.eventhub.com.au
bot.febredojogo.com
bot.gravityatoms.in
bot.jepierre.com.br
bot.jogodoandre.com
bot.jogomoderno.com
infomakeracademy.com
kuiz.sistemniaga.com
leoborges-app.online
linspecteuremma.site
malayanboosterhq.com
menukb.wpwakanda.com
offer.botscientis.us
ore.barrettamario.it
sellmycarglasgow.com
site100seguro.online
stephanesampa.online
superglicemia.com.br
talkbot.agfunnel.com
tenorioadvogados.com
uppity.wpwakanda.com
web.whatisappweb.com
www.acordo-certo.com
83701274.21000000.lol
87186327.21000000.one
90945247.21000000.one
97320578.21000000.one
98650901.21000000.one
abutton.wpwakanda.com
acelera.maxbot.com.br
agendaestrategica.com
aidigitalmarketing.kr
atendimento.vrauu.com
bbutton.wpwakanda.com
bot.anovaerarb.online
bot.coachayongzul.com
bot.digitalpointer.id
bot.ds-leadmagnet.com
bot.eikju.photography
bot.eymaleggingsg.com
bot.gamesimples.store
bot.incusservices.com
bot.jogomoderno.store
bot.mejoralasalud.fun
bot.meuesocial.com.br
bot.mycompany.reviews
bot.outstandbrand.com
bot.ramonmatos.com.br
bot.sharemyreview.net
bot.synapsegameia.com
bot.truongnguyen.live
bots.baptistearno.com
botz.cloudsiteapp.com
cdd.searchcube.com.sg
chat.jogonobrasil.com
viewer-v2-typebot-io.vercel.app
mdb.assessoria.fernanda.progenbr.com
mdb.assessoria.jbatista.progenbr.com
mdb.assessoria.mauricio.progenbr.com
mdb.evento.autocadastro.progenbr.com
form.shopmercedesbenzsouthorlando.com
mdb.evento.equipeinterna.progenbr.com
bot.studiotecnicoimmobiliaremerelli.it
mdb.assessoria.boaventura.progenbr.com
mdb.assessoria.jtrebesqui.progenbr.com
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
gabinete.baleia.formulario.progenbr.com
mdb.assessoria.carreirinha.progenbr.com
chrome-os-inquiry-system.itschromeos.com
mdb.assessoria.paulomarques.progenbr.com
viewer-v2-git-main-typebot-io.vercel.app
main-menu-for-itschromeos.itschromeos.com
mdb.assessoria.qrcode.ademir.progenbr.com
mdb.assessoria.qrcode.arthur.progenbr.com
mdb.assessoria.qrcode.danilo.progenbr.com
mdb.assessoria.qrcode.marcao.progenbr.com
mdb.assessoria.qrcode.marcio.progenbr.com
mdb.assessoria.qrcode.aloisio.progenbr.com
mdb.assessoria.qrcode.girotto.progenbr.com
mdb.assessoria.qrcode.marinho.progenbr.com
mdb.assessoria.qrcode.rodrigo.progenbr.com
mdb.assessoria.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.desideri.progenbr.com
mdb.assessoria.qrcode.fernanda.progenbr.com
mdb.assessoria.qrcode.jbatista.progenbr.com
mdb.assessoria.qrcode.mauricio.progenbr.com
mdb.assessoria.fernanda.regional.progenbr.com
mdb.assessoria.qrcode.boaventura.progenbr.com
mdb.assessoria.qrcode.jtrebesqui.progenbr.com
mdb.assessoria.qrcode.carreirinha.progenbr.com
mdb.assessoria.qrcode.paulomarques.progenbr.com
mdb.assessoria.qrcode.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.fernanda.regional.progenbr.com

@vercel
Copy link

@vercel vercel bot commented on 7b3cbdb Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-typebot-io.vercel.app
docs.typebot.io
docs-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 7b3cbdb Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 7b3cbdb Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.