🛂 (fileUpload) Improve file upload size limit enforcement
Closes #799, closes #797
This commit is contained in:
40
packages/lib/s3/generatePresignedPostPolicy.ts
Normal file
40
packages/lib/s3/generatePresignedPostPolicy.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { Client, PostPolicyResult } from 'minio'
|
||||
|
||||
type Props = {
|
||||
filePath: string
|
||||
fileType?: string
|
||||
maxFileSize?: number
|
||||
}
|
||||
|
||||
const tenMinutes = 10 * 60
|
||||
|
||||
export const generatePresignedPostPolicy = async ({
|
||||
filePath,
|
||||
fileType,
|
||||
maxFileSize,
|
||||
}: Props): Promise<PostPolicyResult> => {
|
||||
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
|
||||
throw new Error(
|
||||
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
|
||||
)
|
||||
|
||||
const minioClient = new Client({
|
||||
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,
|
||||
})
|
||||
|
||||
const postPolicy = minioClient.newPostPolicy()
|
||||
if (maxFileSize)
|
||||
postPolicy.setContentLengthRange(0, maxFileSize * 1024 * 1024)
|
||||
postPolicy.setKey(filePath)
|
||||
postPolicy.setBucket(env.S3_BUCKET)
|
||||
postPolicy.setExpires(new Date(Date.now() + tenMinutes))
|
||||
if (fileType) postPolicy.setContentType(fileType)
|
||||
|
||||
return minioClient.presignedPostPolicy(postPolicy)
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { Client } from 'minio'
|
||||
|
||||
type GeneratePresignedUrlProps = {
|
||||
filePath: string
|
||||
fileType?: string
|
||||
}
|
||||
|
||||
const tenMinutes = 10 * 60
|
||||
|
||||
export const generatePresignedUrl = async ({
|
||||
filePath,
|
||||
fileType,
|
||||
}: GeneratePresignedUrlProps): Promise<string> => {
|
||||
if (!env.S3_ENDPOINT || !env.S3_ACCESS_KEY || !env.S3_SECRET_KEY)
|
||||
throw new Error(
|
||||
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
|
||||
)
|
||||
|
||||
const minioClient = new Client({
|
||||
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,
|
||||
})
|
||||
|
||||
return minioClient.presignedUrl('PUT', env.S3_BUCKET, filePath, tenMinutes, {
|
||||
'Content-Type': fileType,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user