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

40 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 { NextApiRequest, NextApiResponse } from 'next'
2022-08-08 08:21:36 +02:00
import { getAuthenticatedUser } from 'services/api/utils'
import {
badRequest,
generatePresignedUrl,
methodNotAllowed,
notAuthenticated,
} from 'utils/api'
2021-12-27 15:59:32 +01:00
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') {
2022-08-08 08:21:36 +02:00
const user = await getAuthenticatedUser(req)
if (!user) return notAuthenticated(res)
if (
!process.env.S3_ENDPOINT ||
!process.env.S3_ACCESS_KEY ||
!process.env.S3_SECRET_KEY
)
return badRequest(
res,
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
)
const filePath = req.query.filePath as string | undefined
const fileType = req.query.fileType as string | undefined
if (!filePath || !fileType) return badRequest(res)
const presignedUrl = generatePresignedUrl({ fileType, filePath })
2021-12-27 15:59:32 +01:00
return res.status(200).send({ presignedUrl })
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)