♻️ Move s3-related files to specific lib folder
This commit is contained in:
31
packages/lib/s3/deleteFilesFromBucket.ts
Normal file
31
packages/lib/s3/deleteFilesFromBucket.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { Client } from 'minio'
|
||||
|
||||
export const deleteFilesFromBucket = async ({
|
||||
urls,
|
||||
}: {
|
||||
urls: string[]
|
||||
}): Promise<void> => {
|
||||
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 bucket = env.S3_BUCKET
|
||||
|
||||
return minioClient.removeObjects(
|
||||
bucket,
|
||||
urls
|
||||
.filter((url) => url.includes(env.S3_ENDPOINT as string))
|
||||
.map((url) => url.split(`/${bucket}/`)[1])
|
||||
)
|
||||
}
|
32
packages/lib/s3/generatePresignedUrl.ts
Normal file
32
packages/lib/s3/generatePresignedUrl.ts
Normal file
@ -0,0 +1,32 @@
|
||||
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,
|
||||
})
|
||||
}
|
36
packages/lib/s3/uploadFileToBucket.ts
Normal file
36
packages/lib/s3/uploadFileToBucket.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { Client } from 'minio'
|
||||
|
||||
type Props = {
|
||||
fileName: string
|
||||
file: Buffer
|
||||
mimeType: string
|
||||
}
|
||||
|
||||
export const uploadFileToBucket = async ({
|
||||
fileName,
|
||||
file,
|
||||
mimeType,
|
||||
}: Props): 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,
|
||||
})
|
||||
|
||||
await minioClient.putObject(env.S3_BUCKET, fileName, file, {
|
||||
'Content-Type': mimeType,
|
||||
})
|
||||
|
||||
return `http${env.S3_SSL ? 's' : ''}://${env.S3_ENDPOINT}${
|
||||
env.S3_PORT ? `:${env.S3_PORT}` : ''
|
||||
}/${env.S3_BUCKET}/${fileName}`
|
||||
}
|
49
packages/lib/s3/uploadFiles.ts
Normal file
49
packages/lib/s3/uploadFiles.ts
Normal file
@ -0,0 +1,49 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user