mirror of
https://github.com/langgenius/dify.git
synced 2026-04-30 15:38:08 +08:00
- Replace FormData file upload with presigned URL two-step upload - Add batch-upload contract for folder uploads (reduces N+M to 1+M requests) - Remove deprecated createFile contract and useCreateAppAssetFile hook - Remove checksum field from AppAssetNode and AppAssetTreeView types - Add upload-to-presigned-url utility for direct storage uploads
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
type UploadToPresignedUrlOptions = {
|
|
file: File
|
|
uploadUrl: string
|
|
onProgress?: (progress: number) => void
|
|
signal?: AbortSignal
|
|
}
|
|
|
|
export async function uploadToPresignedUrl({
|
|
file,
|
|
uploadUrl,
|
|
onProgress,
|
|
signal,
|
|
}: UploadToPresignedUrlOptions): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
const xhr = new XMLHttpRequest()
|
|
|
|
if (signal) {
|
|
signal.addEventListener('abort', () => {
|
|
xhr.abort()
|
|
reject(new DOMException('Upload aborted', 'AbortError'))
|
|
})
|
|
}
|
|
|
|
xhr.open('PUT', uploadUrl)
|
|
xhr.setRequestHeader('Content-Type', file.type || 'application/octet-stream')
|
|
|
|
xhr.upload.onprogress = (e) => {
|
|
if (e.lengthComputable && onProgress)
|
|
onProgress(Math.round((e.loaded / e.total) * 100))
|
|
}
|
|
|
|
xhr.onload = () => {
|
|
if (xhr.status >= 200 && xhr.status < 300)
|
|
resolve()
|
|
else
|
|
reject(new Error(`Upload failed with status ${xhr.status}`))
|
|
}
|
|
|
|
xhr.onerror = () => reject(new Error('Upload network error'))
|
|
xhr.ontimeout = () => reject(new Error('Upload timeout'))
|
|
|
|
xhr.send(file)
|
|
})
|
|
}
|