2
0

(s3) Improve storage management and type safety

Closes #756
This commit is contained in:
Baptiste Arnaud
2023-09-08 15:28:11 +02:00
parent 43be38cf50
commit fbb198af9d
47 changed files with 790 additions and 128 deletions

View File

@@ -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/s3/uploadFiles'
import { uploadFiles } from '../helpers/uploadFiles'
type Props = {
block: FileInputBlock

View File

@@ -0,0 +1,49 @@
import { sendRequest } from '@typebot.io/lib/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
}