2022-02-14 10:57:57 +01:00
|
|
|
import { withSentry } from '@sentry/nextjs'
|
2022-03-02 17:35:57 +01:00
|
|
|
import { Client } from 'minio'
|
2021-12-27 15:59:32 +01:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
|
|
|
import { getSession } from 'next-auth/react'
|
2022-03-02 17:35:57 +01:00
|
|
|
import { badRequest, methodNotAllowed } from 'utils'
|
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') {
|
|
|
|
const session = await getSession({ req })
|
|
|
|
if (!session) {
|
|
|
|
res.status(401)
|
|
|
|
return
|
|
|
|
}
|
2022-03-02 17:35:57 +01:00
|
|
|
|
|
|
|
if (
|
|
|
|
!process.env.S3_ENDPOINT ||
|
|
|
|
!process.env.S3_ACCESS_KEY ||
|
|
|
|
!process.env.S3_SECRET_KEY ||
|
|
|
|
!process.env.S3_BUCKET
|
|
|
|
)
|
|
|
|
return res.send({
|
|
|
|
message:
|
|
|
|
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_ACCESS_KEY, S3_BUCKET',
|
|
|
|
})
|
|
|
|
|
|
|
|
const s3 = new Client({
|
|
|
|
endPoint: process.env.S3_ENDPOINT,
|
|
|
|
port: process.env.S3_PORT ? Number(process.env.S3_PORT) : undefined,
|
|
|
|
useSSL:
|
|
|
|
process.env.S3_SSL && process.env.S3_SSL === 'false' ? false : true,
|
|
|
|
accessKey: process.env.S3_ACCESS_KEY,
|
|
|
|
secretKey: process.env.S3_SECRET_KEY,
|
2022-02-14 07:52:12 +01:00
|
|
|
region: process.env.S3_REGION,
|
2022-01-20 16:14:47 +01:00
|
|
|
})
|
2021-12-27 15:59:32 +01:00
|
|
|
|
2022-03-02 17:35:57 +01:00
|
|
|
const filePath = req.query.filePath as string | undefined
|
|
|
|
if (!filePath) return badRequest(res)
|
|
|
|
const presignedUrl = await s3.presignedPutObject(
|
|
|
|
process.env.S3_BUCKET as string,
|
|
|
|
filePath
|
|
|
|
)
|
2021-12-27 15:59:32 +01:00
|
|
|
|
2022-03-02 17:35:57 +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)
|