Skip to content

Commit 3738cac

Browse files
Ron RadtkeRon Radtke
Ron Radtke
authored and
Ron Radtke
committed
Hopefully fixes #113
1 parent 6582ae8 commit 3738cac

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

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

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ enum ResponseFormat {
9595

9696
private boolean shouldTransformFile() {
9797
return this.options.transformFile &&
98-
// Can only process if it's written to a file
99-
(this.options.fileCache || this.options.path != null);
98+
// Can only process if it's written to a file
99+
(this.options.fileCache || this.options.path != null);
100100
}
101101

102102
public static HashMap<String, Call> taskTable = new HashMap<>();
@@ -114,6 +114,7 @@ private boolean shouldTransformFile() {
114114
ReadableArray rawRequestBodyArray;
115115
ReadableMap headers;
116116
Callback callback;
117+
Boolean callbackfired = false;
117118
long contentLength;
118119
long downloadManagerId;
119120
ReactNativeBlobUtilBody requestBody;
@@ -138,7 +139,7 @@ public ReactNativeBlobUtilReq(ReadableMap options, String taskId, String method,
138139

139140
// If transformFile is specified, we first want to get the response back in memory so we can
140141
// encrypt it wholesale and at that point, write it into the file storage.
141-
if((this.options.fileCache || this.options.path != null) && !this.shouldTransformFile())
142+
if ((this.options.fileCache || this.options.path != null) && !this.shouldTransformFile())
142143
responseType = ResponseType.FileStorage;
143144
else
144145
responseType = ResponseType.KeepInMemory;
@@ -167,6 +168,11 @@ public static void cancelTask(String taskId) {
167168
}
168169
}
169170

171+
private void invoke_callback(Object... args) {
172+
if (this.callbackfired) return;
173+
this.callback.invoke(args);
174+
}
175+
170176
private final int QUERY = 1314;
171177
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
172178
private Future<?> future;
@@ -301,7 +307,7 @@ public void run() {
301307
File file = new File(ReactNativeBlobUtilFS.getTmpPath(cacheKey) + ext);
302308

303309
if (file.exists()) {
304-
callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, file.getAbsolutePath());
310+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, file.getAbsolutePath());
305311
return;
306312
}
307313
}
@@ -355,7 +361,7 @@ else if (this.options.fileCache)
355361
}
356362

357363
if (!found) {
358-
callback.invoke("No available WiFi connections.", null, null);
364+
invoke_callback("No available WiFi connections.", null, null);
359365
releaseTaskResource();
360366
return;
361367
}
@@ -551,9 +557,9 @@ public void onFailure(@NonNull Call call, @NonNull IOException e) {
551557
// check if this error caused by socket timeout
552558
if (e.getClass().equals(SocketTimeoutException.class)) {
553559
respInfo.putBoolean("timeout", true);
554-
callback.invoke("The request timed out.", null, null);
560+
invoke_callback("The request timed out.", null, null);
555561
} else
556-
callback.invoke(e.getLocalizedMessage(), null, null);
562+
invoke_callback(e.getLocalizedMessage(), null, null);
557563
releaseTaskResource();
558564
}
559565

@@ -586,7 +592,7 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO
586592
} catch (Exception error) {
587593
error.printStackTrace();
588594
releaseTaskResource();
589-
callback.invoke("ReactNativeBlobUtil request error: " + error.getMessage() + error.getCause());
595+
invoke_callback("ReactNativeBlobUtil request error: " + error.getMessage() + error.getCause());
590596
}
591597
}
592598

@@ -631,7 +637,7 @@ private void done(Response resp) {
631637
ins.close();
632638
os.flush();
633639
os.close();
634-
callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest);
640+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, dest);
635641
}
636642
// response data directly pass to JS context as string.
637643
else {
@@ -649,15 +655,15 @@ private void done(Response resp) {
649655
}
650656
try (FileOutputStream fos = new FileOutputStream(file)) {
651657
fos.write(ReactNativeBlobUtilFileTransformer.sharedFileTransformer.onWriteFile(b));
652-
} catch(Exception e) {
653-
callback.invoke("Error from file transformer:" + e.getLocalizedMessage(), null);
658+
} catch (Exception e) {
659+
invoke_callback("Error from file transformer:" + e.getLocalizedMessage(), null);
654660
return;
655661
}
656-
callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
662+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
657663
return;
658664
}
659665
if (responseFormat == ResponseFormat.BASE64) {
660-
callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
666+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
661667
return;
662668
}
663669
try {
@@ -667,21 +673,21 @@ private void done(Response resp) {
667673
decoder.decode(ByteBuffer.wrap(b));
668674
// If the data contains invalid characters the following lines will be skipped.
669675
String utf8 = new String(b, charSet);
670-
callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
676+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
671677
}
672678
// This usually means the data contains invalid unicode characters but still valid data,
673679
// it's binary data, so send it as a normal string
674680
catch (CharacterCodingException ignored) {
675681
if (responseFormat == ResponseFormat.UTF8) {
676682
String utf8 = new String(b);
677-
callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
683+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, utf8);
678684
} else {
679-
callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
685+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
680686
}
681687
}
682688
}
683689
} catch (IOException e) {
684-
callback.invoke("ReactNativeBlobUtil failed to encode response data to BASE64 string.", null);
690+
invoke_callback("ReactNativeBlobUtil failed to encode response data to BASE64 string.", null);
685691
}
686692
break;
687693
case FileStorage:
@@ -713,26 +719,26 @@ private void done(Response resp) {
713719
} catch (IOException exception) {
714720
exception.printStackTrace();
715721
}
716-
callback.invoke("Unexpected FileStorage response file: " + responseBodyString, null);
722+
invoke_callback("Unexpected FileStorage response file: " + responseBodyString, null);
717723
} else {
718-
callback.invoke("Unexpected FileStorage response with no file.", null);
724+
invoke_callback("Unexpected FileStorage response with no file.", null);
719725
}
720726
return;
721727
}
722728

723729
if (ReactNativeBlobUtilFileResp != null && !ReactNativeBlobUtilFileResp.isDownloadComplete()) {
724-
callback.invoke("Download interrupted.", null);
730+
invoke_callback("Download interrupted.", null);
725731
} else {
726732
this.destPath = this.destPath.replace("?append=true", "");
727-
callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
733+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, this.destPath);
728734
}
729735

730736
break;
731737
default:
732738
try {
733-
callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"));
739+
invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_UTF8, new String(resp.body().bytes(), "UTF-8"));
734740
} catch (IOException e) {
735-
callback.invoke("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null);
741+
invoke_callback("ReactNativeBlobUtil failed to encode response data to UTF8 string.", null);
736742
}
737743
break;
738744
}
@@ -856,7 +862,7 @@ public void onReceive(Context context, Intent intent) {
856862
Cursor c = dm.query(query);
857863
// #236 unhandled null check for DownloadManager.query() return value
858864
if (c == null) {
859-
this.callback.invoke("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null);
865+
this.invoke_callback("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null);
860866
return;
861867
}
862868

@@ -867,7 +873,7 @@ public void onReceive(Context context, Intent intent) {
867873
// #297 handle failed request
868874
int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
869875
if (statusCode == DownloadManager.STATUS_FAILED) {
870-
this.callback.invoke("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
876+
this.invoke_callback("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
871877
return;
872878
}
873879
String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
@@ -896,17 +902,17 @@ public void onReceive(Context context, Intent intent) {
896902
if (!exists)
897903
throw new Exception("Download manager download failed, the file does not downloaded to destination.");
898904
else
899-
this.callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, customDest);
905+
this.invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, customDest);
900906

901907
} catch (Exception ex) {
902908
ex.printStackTrace();
903-
this.callback.invoke(ex.getLocalizedMessage(), null);
909+
this.invoke_callback(ex.getLocalizedMessage(), null);
904910
}
905911
} else {
906912
if (filePath == null)
907-
this.callback.invoke("Download manager could not resolve downloaded file path.", ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, null);
913+
this.invoke_callback("Download manager could not resolve downloaded file path.", ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, null);
908914
else
909-
this.callback.invoke(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, filePath);
915+
this.invoke_callback(null, ReactNativeBlobUtilConst.RNFB_RESPONSE_PATH, filePath);
910916
}
911917

912918
}

0 commit comments

Comments
 (0)