Skip to content

Commit

Permalink
♻️ Move s3-related files to specific lib folder
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Aug 30, 2023
1 parent 9a79bc3 commit 23b629f
Show file tree
Hide file tree
Showing 12 changed files with 9 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useToast } from '@/hooks/useToast'
import { Button, ButtonProps, chakra } from '@chakra-ui/react'
import { ChangeEvent, useState } from 'react'
import { uploadFiles } from '@typebot.io/lib/uploadFiles'
import { uploadFiles } from '@typebot.io/lib/s3/uploadFiles'
import { compressFile } from '@/helpers/compressFile'

type UploadButtonProps = {
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/src/pages/api/storage/upload-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { getAuthenticatedUser } from '@/features/auth/helpers/getAuthenticatedUser'
import {
badRequest,
generatePresignedUrl,
methodNotAllowed,
notAuthenticated,
} from '@typebot.io/lib/api'
import { generatePresignedUrl } from '@typebot.io/lib/s3/generatePresignedUrl'
import { env } from '@typebot.io/env'

const handler = async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@typebot.io/schemas'
import { byId, isDefined } from '@typebot.io/lib'
import { z } from 'zod'
import { generatePresignedUrl } from '@typebot.io/lib/api/generatePresignedUrl'
import { generatePresignedUrl } from '@typebot.io/lib/s3/generatePresignedUrl'
import { env } from '@typebot.io/env'

export const getUploadUrl = publicProcedure
Expand Down
2 changes: 1 addition & 1 deletion apps/viewer/src/features/whatsApp/helpers/downloadMedia.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import got from 'got'
import { TRPCError } from '@trpc/server'
import { uploadFileToBucket } from '@typebot.io/lib/api/uploadFileToBucket'
import { uploadFileToBucket } from '@typebot.io/lib/s3/uploadFileToBucket'

type Props = {
mediaId: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTypebot } from '@/providers/TypebotProvider'
import { InputSubmitContent } from '@/types'
import { defaultFileInputOptions, FileInputBlock } from '@typebot.io/schemas'
import React, { ChangeEvent, FormEvent, useState, DragEvent } from 'react'
import { uploadFiles } from '@typebot.io/lib/uploadFiles'
import { uploadFiles } from '@typebot.io/lib/s3/uploadFiles'

type Props = {
block: FileInputBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { guessApiHost } from '@/utils/guessApiHost'
import { FileInputBlock } from '@typebot.io/schemas'
import { defaultFileInputOptions } from '@typebot.io/schemas/features/blocks/inputs/file'
import { createSignal, Match, Show, Switch } from 'solid-js'
import { uploadFiles } from '@typebot.io/lib/uploadFiles'
import { uploadFiles } from '@typebot.io/lib/s3/uploadFiles'
import { Button } from '@/components/Button'
import { Spinner } from '@/components/Spinner'

Expand Down
32 changes: 1 addition & 31 deletions packages/lib/api/helpers/archiveResults.ts
Original file line number Diff line number Diff line change
@@ -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'>
Expand Down Expand Up @@ -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])
)
}
1 change: 0 additions & 1 deletion packages/lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './utils'
export * from './generatePresignedUrl'
export * from './encryption'
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const deleteFilesFromBucket = async ({
region: env.S3_REGION,
})

const bucket = env.S3_BUCKET ?? 'typebot'
const bucket = env.S3_BUCKET

return minioClient.removeObjects(
bucket,
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sendRequest } from './utils'
import { sendRequest } from '../utils'

type UploadFileProps = {
basePath?: string
Expand Down

0 comments on commit 23b629f

Please sign in to comment.