2
0

fix(results): 🐛 Remove storage on result delete

This commit is contained in:
Baptiste Arnaud
2022-06-26 18:17:50 +02:00
parent fc4db575ac
commit 56dca868c9
5 changed files with 2382 additions and 2782 deletions

View File

@@ -58,6 +58,7 @@
"kbar": "^0.1.0-beta.34", "kbar": "^0.1.0-beta.34",
"micro": "^9.3.4", "micro": "^9.3.4",
"micro-cors": "^0.1.1", "micro-cors": "^0.1.1",
"minio": "^7.0.28",
"models": "*", "models": "*",
"next": "^12.1.6", "next": "^12.1.6",
"next-auth": "4.3.4", "next-auth": "4.3.4",
@@ -91,6 +92,7 @@
"@types/google-spreadsheet": "^3.2.1", "@types/google-spreadsheet": "^3.2.1",
"@types/jsonwebtoken": "8.5.8", "@types/jsonwebtoken": "8.5.8",
"@types/micro-cors": "^0.1.2", "@types/micro-cors": "^0.1.2",
"@types/minio": "^7.0.13",
"@types/node": "^17.0.33", "@types/node": "^17.0.33",
"@types/nodemailer": "^6.4.4", "@types/nodemailer": "^6.4.4",
"@types/nprogress": "^0.2.0", "@types/nprogress": "^0.2.0",

View File

@@ -55,25 +55,24 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
id: ids.length > 0 ? { in: ids } : undefined, id: ids.length > 0 ? { in: ids } : undefined,
typebot: canWriteTypebot(typebotId, user), typebot: canWriteTypebot(typebotId, user),
} }
// Weird bug waiting for https://github.com/aws/aws-sdk-js/issues/4137 const typebot = await prisma.typebot.findFirst({
// const typebot = await prisma.typebot.findFirst({ where: canWriteTypebot(typebotId, user),
// where: canWriteTypebot(typebotId, user), select: { groups: true },
// select: { groups: true }, })
// }) if (!typebot) return forbidden(res)
// if (!typebot) return forbidden(res) const fileUploadBlockIds = (typebot as Typebot).groups
// const fileUploadBlockIds = (typebot as Typebot).groups .flatMap((g) => g.blocks)
// .flatMap((g) => g.blocks) .filter((b) => b.type === InputBlockType.FILE)
// .filter((b) => b.type === InputBlockType.FILE) .map((b) => b.id)
// .map((b) => b.id) if (fileUploadBlockIds.length > 0) {
// if (fileUploadBlockIds.length > 0) { const filesToDelete = await prisma.answer.findMany({
// const filesToDelete = await prisma.answer.findMany({ where: { result: resultsFilter, blockId: { in: fileUploadBlockIds } },
// where: { result: resultsFilter, blockId: { in: fileUploadBlockIds } }, })
// }) if (filesToDelete.length > 0)
// if (filesToDelete.length > 0) await deleteFiles({
// await deleteFiles({ urls: filesToDelete.flatMap((a) => a.content.split(', ')),
// urls: filesToDelete.flatMap((a) => a.content.split(', ')), })
// }) }
// }
await prisma.log.deleteMany({ await prisma.log.deleteMany({
where: { where: {
result: resultsFilter, result: resultsFilter,

View File

@@ -1,11 +1,10 @@
import { AWSError, config, Endpoint, S3 } from 'aws-sdk' import { Client } from 'minio'
import { PromiseResult } from 'aws-sdk/lib/request'
export const deleteFiles = async ({ export const deleteFiles = async ({
urls, urls,
}: { }: {
urls: string[] urls: string[]
}): Promise<PromiseResult<S3.DeleteObjectsOutput, AWSError>> => { }): Promise<void> => {
if ( if (
!process.env.S3_ENDPOINT || !process.env.S3_ENDPOINT ||
!process.env.S3_ACCESS_KEY || !process.env.S3_ACCESS_KEY ||
@@ -15,30 +14,23 @@ export const deleteFiles = async ({
'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY' 'S3 not properly configured. Missing one of those variables: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY'
) )
const sslEnabled = const useSSL =
process.env.S3_SSL && process.env.S3_SSL === 'false' ? false : true process.env.S3_SSL && process.env.S3_SSL === 'false' ? false : true
config.update({ const minioClient = new Client({
accessKeyId: process.env.S3_ACCESS_KEY, endPoint: process.env.S3_ENDPOINT,
secretAccessKey: process.env.S3_SECRET_KEY, port: process.env.S3_PORT ? parseInt(process.env.S3_PORT) : undefined,
useSSL,
accessKey: process.env.S3_ACCESS_KEY,
secretKey: process.env.S3_SECRET_KEY,
region: process.env.S3_REGION, region: process.env.S3_REGION,
sslEnabled,
})
const protocol = sslEnabled ? 'https' : 'http'
const s3 = new S3({
endpoint: new Endpoint(
`${protocol}://${process.env.S3_ENDPOINT}${
process.env.S3_PORT ? `:${process.env.S3_PORT}` : ''
}`
),
}) })
const Bucket = process.env.S3_BUCKET ?? 'typebot' const bucket = process.env.S3_BUCKET ?? 'typebot'
return s3
.deleteObjects({ return minioClient.removeObjects(
Bucket, bucket,
Delete: { urls
Objects: urls.map((url) => ({ Key: url.split(`/${Bucket}/`)[1] })), .filter((url) => url.includes(process.env.S3_ENDPOINT as string))
}, .map((url) => url.split(`/${bucket}/`)[1])
}) )
.promise()
} }

View File

@@ -5,8 +5,9 @@ import { parse } from 'papaparse'
import { typebotViewer } from '../services/selectorUtils' import { typebotViewer } from '../services/selectorUtils'
import { importTypebotInDatabase } from '../services/database' import { importTypebotInDatabase } from '../services/database'
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import { isDefined } from 'utils'
test('should work as expected', async ({ page }) => { test('should work as expected', async ({ page, browser }) => {
const typebotId = cuid() const typebotId = cuid()
await importTypebotInDatabase( await importTypebotInDatabase(
path.join(__dirname, '../fixtures/typebots/fileUpload.json'), path.join(__dirname, '../fixtures/typebots/fileUpload.json'),
@@ -51,24 +52,33 @@ test('should work as expected', async ({ page }) => {
expect(data).toHaveLength(2) expect(data).toHaveLength(2)
expect((data[1] as unknown[])[1]).toContain('http://localhost:9000') expect((data[1] as unknown[])[1]).toContain('http://localhost:9000')
// Waiting for https://github.com/aws/aws-sdk-js/issues/4137 const urls = (
// const urls = ( await Promise.all(
// await Promise.all( [
// [ page.locator('text="api.json"'),
// page.locator('text="api.json"'), page.locator('text="fileUpload.json"'),
// page.locator('text="fileUpload.json"'), page.locator('text="hugeGroup.json"'),
// page.locator('text="hugeGroup.json"'), ].map((elem) => elem.getAttribute('href'))
// ].map((elem) => elem.getAttribute('href')) )
// ) ).filter(isDefined)
// ).filter(isDefined)
// const page2 = await browser.newPage() const page2 = await browser.newPage()
// await page2.goto(urls[0]) await page2.goto(urls[0])
// await expect(page2.locator('pre')).toBeVisible() await expect(page2.locator('pre')).toBeVisible()
// await page.locator('button >> text="Delete"').click() await page.locator('button >> text="Delete"').click()
// await page.locator('button >> text="Delete" >> nth=1').click() await page.locator('button >> text="Delete" >> nth=1').click()
// await expect(page.locator('text="api.json"')).toBeHidden() await expect(page.locator('text="api.json"')).toBeHidden()
// await page2.goto(urls[0]) await page2.goto(urls[0])
// await expect(page2.locator('text="grkwobnowrk')).toBeVisible() await expect(
page2.locator('span:has-text("The specified key does not exist.")')
).toBeVisible()
await page2.goto(urls[1])
await expect(
page2.locator('span:has-text("The specified key does not exist.")')
).toBeVisible()
await page2.goto(urls[2])
await expect(
page2.locator('span:has-text("The specified key does not exist.")')
).toBeVisible()
}) })

5035
yarn.lock

File diff suppressed because it is too large Load Diff