2
0
Files
bot/apps/builder/pages/api/storage/upload-url.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-02-14 10:57:57 +01:00
import { withSentry } from '@sentry/nextjs'
2021-12-27 15:59:32 +01:00
import aws from 'aws-sdk'
import { NextApiRequest, NextApiResponse } from 'next'
import { getSession } from 'next-auth/react'
import { methodNotAllowed } from 'utils'
2021-12-27 15:59:32 +01:00
const maxUploadFileSize = 10485760 // 10 MB
const handler = async (
req: NextApiRequest,
res: NextApiResponse
): Promise<void> => {
2022-01-20 16:14:47 +01:00
res.setHeader('Access-Control-Allow-Origin', '*')
if (req.method === 'GET') {
const session = await getSession({ req })
if (!session) {
res.status(401)
return
}
aws.config.update({
2022-02-14 07:52:12 +01:00
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY,
region: process.env.S3_REGION,
2022-01-20 16:14:47 +01:00
signatureVersion: 'v4',
})
2021-12-27 15:59:32 +01:00
2022-01-20 16:14:47 +01:00
const s3 = new aws.S3()
const post = s3.createPresignedPost({
2022-02-14 07:52:12 +01:00
Bucket: process.env.S3_BUCKET,
2022-01-20 16:14:47 +01:00
Fields: {
ACL: 'public-read',
key: req.query.key,
'Content-Type': req.query.fileType,
},
Expires: 120, // seconds
Conditions: [['content-length-range', 0, maxUploadFileSize]],
})
2021-12-27 15:59:32 +01:00
2022-01-20 16:14:47 +01:00
return res.status(200).json(post)
2021-12-27 15:59:32 +01:00
}
2022-01-20 16:14:47 +01:00
return methodNotAllowed(res)
2021-12-27 15:59:32 +01:00
}
2022-02-14 10:57:57 +01:00
export default withSentry(handler)