Skip to content

Commit 5dd72a1

Browse files
Ron RadtkeRon Radtke
authored andcommitted
implementing delta from RonRadtke#261 with small changes
1 parent 8df0134 commit 5dd72a1

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,9 @@ private void releaseTaskResource() {
638638
*/
639639
private void done(Response resp) {
640640
boolean isBlobResp = isBlobResponse(resp);
641+
WritableMap respmap = getResponseInfo(resp,isBlobResp);
642+
emitStateEvent(respmap.copy());
643+
641644
emitStateEvent(getResponseInfo(resp, isBlobResp));
642645
switch (responseType) {
643646
case KeepInMemory:
@@ -656,7 +659,7 @@ private void done(Response resp) {
656659
ins.close();
657660
os.flush();
658661
os.close();
659-
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest);
662+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest, respmap.copy());
660663
}
661664
// response data directly pass to JS context as string.
662665
else {
@@ -678,11 +681,11 @@ private void done(Response resp) {
678681
invoke_callback("Error from file transformer:" + e.getLocalizedMessage(), null);
679682
return;
680683
}
681-
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
684+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath, respmap.copy());
682685
return;
683686
}
684687
if (responseFormat == ResponseFormat.BASE64) {
685-
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
688+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP), respmap.copy());
686689
return;
687690
}
688691
try {
@@ -699,9 +702,9 @@ private void done(Response resp) {
699702
catch (CharacterCodingException ignored) {
700703
if (responseFormat == ResponseFormat.UTF8) {
701704
String utf8 = new String(b);
702-
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
705+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8, respmap.copy());
703706
} else {
704-
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
707+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP), respmap.copy());
705708
}
706709
}
707710
}
@@ -746,16 +749,16 @@ private void done(Response resp) {
746749
}
747750

748751
if (ReactNativeBlobUtilFileResp != null && !ReactNativeBlobUtilFileResp.isDownloadComplete()) {
749-
invoke_callback("Download interrupted.", null);
752+
invoke_callback("Download interrupted.", null, respmap.copy());
750753
} else {
751754
this.destPath = this.destPath.replace("?append=true", "");
752-
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
755+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath, respmap.copy());
753756
}
754757

755758
break;
756759
default:
757760
try {
758-
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"));
761+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"), respmap.copy());
759762
} catch (IOException e) {
760763
invoke_callback("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null);
761764
}

fetch.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export function fetch(...args: any): Promise {
163163
let taskId = getUUID();
164164
let options = this || {};
165165
let subscription, subscriptionUpload, stateEvent, partEvent;
166-
let respInfo = {};
166+
let respInfo = {'uninit': true};
167167
let [method, url, headers, body] = [...args];
168168

169169
// # 241 normalize null or undefined headers, in case nil or null string
@@ -242,8 +242,9 @@ export function fetch(...args: any): Promise {
242242
* in JS context, and this parameter indicates which one
243243
* dose the response data presents.
244244
* @param data {string} Response data or its reference.
245+
* @param responseInfo {Object.<>}
245246
*/
246-
req(options, taskId, method, url, headers || {}, body, (err, rawType, data) => {
247+
req(options, taskId, method, url, headers || {}, body, (err, rawType, data, responseInfo) => {
247248

248249
// task done, remove event listeners
249250
subscription.remove();
@@ -269,6 +270,8 @@ export function fetch(...args: any): Promise {
269270
fs.session(options.session).add(data);
270271
}
271272
respInfo.rnfbEncode = rawType;
273+
if (uninit in respInfo && respInfo.uninit) // event didn't fire yet so we override it here
274+
respInfo = responseInfo;
272275
resolve(new FetchBlobResponse(taskId, respInfo, data));
273276
}
274277

ios/ReactNativeBlobUtilRequest.mm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,16 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
464464
}
465465
}
466466

467+
NSHTTPURLResponse *response = (NSHTTPURLResponse *) [task response];
467468

468469
callback(@[
469470
errMsg ?: [NSNull null],
470471
rnfbRespType ?: @"",
471-
respStr ?: [NSNull null]
472+
respStr ?: [NSNull null],
473+
@{
474+
@"status": [NSNumber numberWithInteger:[response statusCode]]
475+
}
476+
]);
472477
]);
473478

474479
respData = nil;

0 commit comments

Comments
 (0)