2
0

(s3) Improve storage management and type safety

Closes #756
This commit is contained in:
Baptiste Arnaud
2023-09-08 15:28:11 +02:00
parent 43be38cf50
commit fbb198af9d
47 changed files with 790 additions and 128 deletions

View File

@ -23,6 +23,6 @@
},
"dependencies": {
"got": "12.6.0",
"minio": "7.1.1"
"minio": "7.1.3"
}
}

View File

@ -0,0 +1,42 @@
import { env } from '@typebot.io/env'
import { Client } from 'minio'
type Props = {
folderPath: string
}
export const getFolderSize = async ({ folderPath }: Props) => {
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 new Promise<number>((resolve, reject) => {
let totalSize = 0
const stream = minioClient.listObjectsV2(
env.S3_BUCKET,
'public/' + folderPath,
true
)
stream.on('data', function (obj) {
totalSize += obj.size
})
stream.on('error', function (err) {
reject(err)
})
stream.on('end', function () {
resolve(totalSize)
})
})
}

View File

@ -1,49 +0,0 @@
import { sendRequest } from '../utils'
type UploadFileProps = {
basePath?: string
files: {
file: File
path: string
}[]
onUploadProgress?: (percent: number) => void
}
type UrlList = (string | null)[]
export const uploadFiles = async ({
basePath = '/api',
files,
onUploadProgress,
}: UploadFileProps): Promise<UrlList> => {
const urls = []
let i = 0
for (const { file, path } of files) {
onUploadProgress && onUploadProgress((i / files.length) * 100)
i += 1
const { data } = await sendRequest<{
presignedUrl: string
hasReachedStorageLimit: boolean
}>(
`${basePath}/storage/upload-url?filePath=${encodeURIComponent(
path
)}&fileType=${file.type}`
)
if (!data?.presignedUrl) continue
const url = data.presignedUrl
if (data.hasReachedStorageLimit) urls.push(null)
else {
const upload = await fetch(url, {
method: 'PUT',
body: file,
})
if (!upload.ok) continue
urls.push(url.split('?')[0])
}
}
return urls
}