Skip to content

Commit 756c74d

Browse files
committed
feat(ios): replicate Android chunked download reporting
If a download is determined to be using [chunked transfer encoding][1] (through [`NSURLResponse.expectedContentLength`][2] returning [`NSURLResponseUnknownLength`][3]), replicate the behaviour in the Android native component of the plugin; reporting: * (0,-1) for each intermediate progress event, and * (total,total) for the final progress event [1]: https://datatracker.ietf.org/doc/html/rfc9112#section-7.1 [2]: https://developer.apple.com/documentation/foundation/nsurlresponse/1413507-expectedcontentlength [3]: https://developer.apple.com/documentation/foundation/nsurlresponseunknownlength
1 parent b6c7f6f commit 756c74d

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

ios/ReactNativeBlobUtilRequest.mm

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,35 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
349349
return;
350350
}
351351

352-
NSNumber * now =[NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
352+
// For non-chunked download, progress is received / expected
353+
// For chunked download, progress can be either 0 (started) or 1 (ended)
354+
NSNumber *now;
355+
if (expectedBytes != NSURLResponseUnknownLength) {
356+
now = [NSNumber numberWithFloat:((float)receivedBytes/(float)expectedBytes)];
357+
} else {
358+
now = @0;
359+
}
353360

354361
if ([self.progressConfig shouldReport:now]) {
355-
[self.baseModule emitEventDict :EVENT_PROGRESS
356-
body:@{
362+
NSDictionary *body;
363+
if (expectedBytes == NSURLResponseUnknownLength) {
364+
// For chunked downloads
365+
body = @{
366+
@"taskId": taskId,
367+
@"written": [NSString stringWithFormat:@"%d", 0],
368+
@"total": [NSString stringWithFormat:@"%lld", (long long) expectedBytes],
369+
@"chunk": chunkString,
370+
};
371+
} else {
372+
// For non-chunked downloads
373+
body = @{
357374
@"taskId": taskId,
358375
@"written": [NSString stringWithFormat:@"%lld", (long long) receivedBytes],
359376
@"total": [NSString stringWithFormat:@"%lld", (long long) expectedBytes],
360-
@"chunk": chunkString
361-
}
362-
];
377+
@"chunk": chunkString,
378+
};
379+
}
380+
[self.baseModule emitEventDict:EVENT_PROGRESS body:body];
363381
}
364382
}
365383

@@ -392,6 +410,14 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
392410
} else {
393411
errMsg = [error localizedDescription];
394412
}
413+
} else if ([self.progressConfig shouldReport:@1] && expectedBytes == NSURLResponseUnknownLength) {
414+
// For chunked downloads
415+
[self.baseModule emitEventDict:EVENT_PROGRESS body:@{
416+
@"taskId": taskId,
417+
@"written": [NSString stringWithFormat:@"%lld", (long long) receivedBytes],
418+
@"total": [NSString stringWithFormat:@"%lld", (long long) receivedBytes],
419+
@"chunk": @"",
420+
}];
395421
}
396422

397423
if (respFile) {

0 commit comments

Comments
 (0)