2
0

♻️ Move s3-related files to specific lib folder

This commit is contained in:
Baptiste Arnaud
2023-08-30 09:36:45 +02:00
parent 9a79bc38ee
commit 23b629f82c
12 changed files with 9 additions and 40 deletions

View File

@@ -1,31 +0,0 @@
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 ?? 'typebot'
return minioClient.removeObjects(
bucket,
urls
.filter((url) => url.includes(env.S3_ENDPOINT as string))
.map((url) => url.split(`/${bucket}/`)[1])
)
}

View File

@@ -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,
})
}

View File

@@ -1,7 +1,6 @@
import { env } from '@typebot.io/env'
import { Prisma, PrismaClient } from '@typebot.io/prisma'
import { InputBlockType, Typebot } from '@typebot.io/schemas'
import { Client } from 'minio'
import { deleteFilesFromBucket } from '../../s3/deleteFilesFromBucket'
type ArchiveResultsProps = {
typebot: Pick<Typebot, 'groups'>
@@ -90,32 +89,3 @@ export const archiveResults =
return { success: true }
}
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 ?? true,
accessKey: env.S3_ACCESS_KEY,
secretKey: env.S3_SECRET_KEY,
region: env.S3_REGION,
})
const bucket = env.S3_BUCKET ?? 'typebot'
return minioClient.removeObjects(
bucket,
urls
.filter((url) => url.includes(env.S3_ENDPOINT as string))
.map((url) => url.split(`/${bucket}/`)[1])
)
}

View File

@@ -1,3 +1,2 @@
export * from './utils'
export * from './generatePresignedUrl'
export * from './encryption'

View File

@@ -1,36 +0,0 @@
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}`
}