Files
dify/web/service/upload-to-presigned-url.ts
yyh f8438704a6 refactor(app-asset): migrate file upload to presigned URL and batch upload
- 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
2026-01-23 15:11:04 +08:00

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)
})
}