2023-08-28 09:13:53 +02:00
|
|
|
import { env } from '@typebot.io/env'
|
2022-06-26 18:17:50 +02:00
|
|
|
import { Client } from 'minio'
|
2022-06-24 14:06:06 +02:00
|
|
|
|
2023-08-29 10:01:28 +02:00
|
|
|
type Props = {
|
2023-11-20 10:32:38 +01:00
|
|
|
key: string
|
2023-08-29 10:01:28 +02:00
|
|
|
file: Buffer
|
|
|
|
|
mimeType: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const uploadFileToBucket = async ({
|
2023-11-20 10:32:38 +01:00
|
|
|
key,
|
2023-08-29 10:01:28 +02:00
|
|
|
file,
|
|
|
|
|
mimeType,
|
|
|
|
|
}: Props): Promise<string> => {
|
2023-08-28 09:13:53 +02:00
|
|
|
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
|
2022-06-24 14:06:06 +02:00
|
|
|
throw new Error(
|
|
|
|
|
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
|
|
|
|
|
)
|
|
|
|
|
|
2022-06-26 18:17:50 +02:00
|
|
|
const minioClient = new Client({
|
2023-08-28 09:13:53 +02:00
|
|
|
endPoint: env.S3_ENDPOINT,
|
|
|
|
|
port: env.S3_PORT,
|
|
|
|
|
useSSL: env.S3_SSL,
|
|
|
|
|
accessKey: env.S3_ACCESS_KEY,
|
|
|
|
|
secretKey: env.S3_SECRET_KEY,
|
|
|
|
|
region: env.S3_REGION,
|
2022-06-24 14:06:06 +02:00
|
|
|
})
|
|
|
|
|
|
2023-11-20 10:32:38 +01:00
|
|
|
await minioClient.putObject(env.S3_BUCKET, 'public/' + key, file, {
|
2023-08-29 10:01:28 +02:00
|
|
|
'Content-Type': mimeType,
|
2023-11-20 10:32:38 +01:00
|
|
|
'Cache-Control': 'public, max-age=86400',
|
2023-08-29 10:01:28 +02:00
|
|
|
})
|
2022-06-26 18:17:50 +02:00
|
|
|
|
2023-11-20 10:32:38 +01:00
|
|
|
return env.S3_PUBLIC_CUSTOM_DOMAIN
|
|
|
|
|
? `${env.S3_PUBLIC_CUSTOM_DOMAIN}/public/${key}`
|
|
|
|
|
: `http${env.S3_SSL ? 's' : ''}://${env.S3_ENDPOINT}${
|
|
|
|
|
env.S3_PORT ? `:${env.S3_PORT}` : ''
|
|
|
|
|
}/${env.S3_BUCKET}/public/${key}`
|
2022-06-24 14:06:06 +02:00
|
|
|
}
|