2
0

feat(bot): ️ Add custom file upload size limit

This commit is contained in:
Baptiste Arnaud
2022-06-21 16:53:45 +02:00
parent 1931a5c9c0
commit ea765640cf
17 changed files with 141 additions and 44 deletions

View File

@ -1,6 +1,8 @@
import { withSentry } from '@sentry/nextjs'
import prisma from 'libs/prisma'
import { InputBlockType, PublicTypebot } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { badRequest, generatePresignedUrl, methodNotAllowed } from 'utils'
import { badRequest, generatePresignedUrl, methodNotAllowed, byId } from 'utils'
const handler = async (
req: NextApiRequest,
@ -19,8 +21,25 @@ const handler = async (
)
const filePath = req.query.filePath as string | undefined
const fileType = req.query.fileType as string | undefined
const typebotId = req.query.typebotId as string
const blockId = req.query.blockId as string
if (!filePath || !fileType) return badRequest(res)
const presignedUrl = generatePresignedUrl({ fileType, filePath })
const typebot = (await prisma.publicTypebot.findFirst({
where: { typebotId },
})) as unknown as PublicTypebot
const fileUploadBlock = typebot.groups
.flatMap((g) => g.blocks)
.find(byId(blockId))
if (fileUploadBlock?.type !== InputBlockType.FILE) return badRequest(res)
const sizeLimit = fileUploadBlock.options.sizeLimit
? Math.min(fileUploadBlock.options.sizeLimit, 500)
: 10
const presignedUrl = generatePresignedUrl({
fileType,
filePath,
sizeLimit: sizeLimit * 1024 * 1024,
})
return res.status(200).send({ presignedUrl })
}

View File

@ -26,7 +26,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
const typebotId = req.query.typebotId as string
const result = await prisma.result.create({
data: { typebotId, isCompleted: false },
data: {
typebotId,
isCompleted: false,
},
})
return res.send(result)
}

View File

@ -1,14 +1,25 @@
import { withSentry } from '@sentry/nextjs'
import { Answer } from 'db'
import { got } from 'got'
import prisma from 'libs/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from 'utils'
import { isNotDefined, methodNotAllowed } from 'utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'PUT') {
const answer = (
const { uploadedFiles, ...answer } = (
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
) as Answer
) as Answer & { uploadedFiles?: boolean }
let storageUsed = 0
if (uploadedFiles && answer.content.includes('http')) {
const fileUrls = answer.content.split(', ')
for (const url of fileUrls) {
const { headers } = await got(url)
const size = headers['content-length']
if (isNotDefined(size)) return
storageUsed += parseInt(size, 10)
}
}
const result = await prisma.answer.upsert({
where: {
resultId_blockId_groupId: {
@ -17,8 +28,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
blockId: answer.blockId,
},
},
create: answer,
update: answer,
create: { ...answer, storageUsed: storageUsed > 0 ? storageUsed : null },
update: { ...answer, storageUsed: storageUsed > 0 ? storageUsed : null },
})
return res.send(result)
}