( target: Storage.UploadTarget, file: UploadFile, onProgress?: (progress: UploadProgress) => void, )
| 398 | }; |
| 399 | |
| 400 | export const uploadToTarget = async ( |
| 401 | target: Storage.UploadTarget, |
| 402 | file: UploadFile, |
| 403 | onProgress?: (progress: UploadProgress) => void, |
| 404 | ) => { |
| 405 | if (target.type === "s3Post") { |
| 406 | if (isNativeUploadUri(file.uri)) { |
| 407 | await uploadNativeFile( |
| 408 | "POST", |
| 409 | target.url, |
| 410 | file, |
| 411 | { |
| 412 | fieldName: "file", |
| 413 | mimeType: file.type, |
| 414 | parameters: target.fields, |
| 415 | uploadType: FileSystem.FileSystemUploadType.MULTIPART, |
| 416 | }, |
| 417 | onProgress, |
| 418 | ); |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | const formData = new FormData(); |
| 423 | for (const [key, value] of Object.entries(target.fields)) { |
| 424 | formData.append(key, value); |
| 425 | } |
| 426 | formData.append("file", { |
| 427 | uri: file.uri, |
| 428 | name: file.name, |
| 429 | type: file.type, |
| 430 | } as unknown as Blob); |
| 431 | await uploadWithXhr( |
| 432 | "POST", |
| 433 | target.url, |
| 434 | new Headers(), |
| 435 | formData, |
| 436 | onProgress, |
| 437 | ); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | const headers = { ...target.headers }; |
| 442 | let size = file.size; |
| 443 | if ( |
| 444 | target.type === "driveResumable" && |
| 445 | typeof size === "number" && |
| 446 | size > 0 |
| 447 | ) { |
| 448 | headers["Content-Range"] = `bytes 0-${size - 1}/${size}`; |
| 449 | } |
| 450 | |
| 451 | if (isNativeUploadUri(file.uri)) { |
| 452 | if (target.type === "driveResumable" && !size) { |
| 453 | size = await getLocalFileSize(file); |
| 454 | if (size > 0) { |
| 455 | headers["Content-Range"] = `bytes 0-${size - 1}/${size}`; |
| 456 | } |
| 457 | } |
no test coverage detected