2
0

Use minio for presigned urls and remove aws sdk

This commit is contained in:
Baptiste Arnaud
2023-08-30 09:08:05 +02:00
parent 5d402d9a38
commit 9a79bc38ee
16 changed files with 236 additions and 327 deletions

View File

@@ -196,57 +196,6 @@ export const generateId = (idDesiredLength: number): string => {
.join('')
}
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: { url: string; fields: any }
hasReachedStorageLimit: boolean
}>(
`${basePath}/storage/upload-url?filePath=${encodeURIComponent(
path
)}&fileType=${file.type}`
)
if (!data?.presignedUrl) continue
const { url, fields } = data.presignedUrl
if (data.hasReachedStorageLimit) urls.push(null)
else {
const formData = new FormData()
Object.entries({ ...fields, file }).forEach(([key, value]) => {
formData.append(key, value as string | Blob)
})
const upload = await fetch(url, {
method: 'POST',
body: formData,
})
if (!upload.ok) continue
urls.push(`${url.split('?')[0]}/${path}`)
}
}
return urls
}
export const hasValue = (
value: string | undefined | null
): value is NonNullable<string> =>