🧑💻 (s3) Correctly delete the files when deleting resources
This commit is contained in:
@ -1,43 +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
|
||||
|
||||
const keys = urls.reduce<string[]>(
|
||||
(keys, url) => [
|
||||
...keys,
|
||||
...addKeyIfIncludesPublicCustomDomain(url),
|
||||
...addKeyIfIncludesDefaultEndpoint(url, bucket),
|
||||
],
|
||||
[]
|
||||
)
|
||||
|
||||
return minioClient.removeObjects(bucket, keys)
|
||||
}
|
||||
|
||||
const addKeyIfIncludesPublicCustomDomain = (url: string) =>
|
||||
env.S3_PUBLIC_CUSTOM_DOMAIN && url.includes(env.S3_PUBLIC_CUSTOM_DOMAIN)
|
||||
? [url.split(env.S3_PUBLIC_CUSTOM_DOMAIN + '/')[1]]
|
||||
: []
|
||||
|
||||
const addKeyIfIncludesDefaultEndpoint = (url: string, bucket: string) =>
|
||||
url.includes(env.S3_ENDPOINT as string) ? [url.split(`/${bucket}/`)[1]] : []
|
@ -1,5 +1,6 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { Client, PostPolicyResult } from 'minio'
|
||||
import { PostPolicyResult } from 'minio'
|
||||
import { initClient } from './initClient'
|
||||
|
||||
type Props = {
|
||||
filePath: string
|
||||
@ -14,19 +15,7 @@ export const generatePresignedPostPolicy = async ({
|
||||
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 minioClient = initClient()
|
||||
|
||||
const postPolicy = minioClient.newPostPolicy()
|
||||
if (maxFileSize)
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { Client } from 'minio'
|
||||
import { initClient } from './initClient'
|
||||
|
||||
type Props = {
|
||||
key: string
|
||||
@ -9,19 +9,6 @@ export const getFileTempUrl = async ({
|
||||
key,
|
||||
expires,
|
||||
}: 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,
|
||||
})
|
||||
|
||||
const minioClient = initClient()
|
||||
return minioClient.presignedGetObject(env.S3_BUCKET, key, expires ?? 3600)
|
||||
}
|
||||
|
@ -1,24 +1,12 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { Client } from 'minio'
|
||||
import { initClient } from './initClient'
|
||||
|
||||
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,
|
||||
})
|
||||
const minioClient = initClient()
|
||||
|
||||
return new Promise<number>((resolve, reject) => {
|
||||
let totalSize = 0
|
||||
|
20
packages/lib/s3/initClient.ts
Normal file
20
packages/lib/s3/initClient.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { Client } from 'minio'
|
||||
|
||||
export const initClient = () => {
|
||||
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
|
||||
}
|
67
packages/lib/s3/removeObjectsRecursively.ts
Normal file
67
packages/lib/s3/removeObjectsRecursively.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { initClient } from './initClient'
|
||||
|
||||
const removeObjectsRecursively = async (prefix: string) => {
|
||||
const minioClient = initClient()
|
||||
|
||||
const bucketName = env.S3_BUCKET
|
||||
|
||||
const objectsStream = minioClient.listObjectsV2(bucketName, prefix, true)
|
||||
|
||||
for await (const obj of objectsStream) {
|
||||
try {
|
||||
await minioClient.removeObject(bucketName, obj.name)
|
||||
} catch (err) {
|
||||
console.error(`Error removing ${obj.name}:`, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const removeObjectsFromWorkspace = async (workspaceId: string) => {
|
||||
await removeObjectsRecursively(`public/workspaces/${workspaceId}/`)
|
||||
await removeObjectsRecursively(`private/workspaces/${workspaceId}/`)
|
||||
}
|
||||
|
||||
export const removeObjectsFromResult = async ({
|
||||
workspaceId,
|
||||
resultIds,
|
||||
typebotId,
|
||||
}: {
|
||||
workspaceId: string
|
||||
resultIds: string[]
|
||||
typebotId: string
|
||||
}) => {
|
||||
for (const resultId of resultIds) {
|
||||
await removeObjectsRecursively(
|
||||
`public/workspaces/${workspaceId}/typebots/${typebotId}/results/${resultId}/`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export const removeAllObjectsFromResult = async ({
|
||||
workspaceId,
|
||||
typebotId,
|
||||
}: {
|
||||
workspaceId: string
|
||||
typebotId: string
|
||||
}) => {
|
||||
await removeObjectsRecursively(
|
||||
`public/workspaces/${workspaceId}/typebots/${typebotId}/results/`
|
||||
)
|
||||
}
|
||||
|
||||
export const removeObjectsFromTypebot = async ({
|
||||
typebotId,
|
||||
workspaceId,
|
||||
}: {
|
||||
typebotId: string
|
||||
workspaceId: string
|
||||
}) => {
|
||||
await removeObjectsRecursively(
|
||||
`public/workspaces/${workspaceId}/typebots/${typebotId}/`
|
||||
)
|
||||
}
|
||||
|
||||
export const removeObjectsFromUser = async (userId: string) => {
|
||||
await removeObjectsRecursively(`public/users/${userId}/`)
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import { env } from '@typebot.io/env'
|
||||
import { Client } from 'minio'
|
||||
import { initClient } from './initClient'
|
||||
|
||||
type Props = {
|
||||
key: string
|
||||
@ -12,19 +12,7 @@ export const uploadFileToBucket = async ({
|
||||
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,
|
||||
})
|
||||
const minioClient = initClient()
|
||||
|
||||
await minioClient.putObject(env.S3_BUCKET, 'public/' + key, file, {
|
||||
'Content-Type': mimeType,
|
||||
|
Reference in New Issue
Block a user