@@ -95,8 +95,8 @@ enum ResponseFormat {
95
95
96
96
private boolean shouldTransformFile () {
97
97
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 );
100
100
}
101
101
102
102
public static HashMap <String , Call > taskTable = new HashMap <>();
@@ -114,6 +114,7 @@ private boolean shouldTransformFile() {
114
114
ReadableArray rawRequestBodyArray ;
115
115
ReadableMap headers ;
116
116
Callback callback ;
117
+ Boolean callbackfired = false ;
117
118
long contentLength ;
118
119
long downloadManagerId ;
119
120
ReactNativeBlobUtilBody requestBody ;
@@ -138,7 +139,7 @@ public ReactNativeBlobUtilReq(ReadableMap options, String taskId, String method,
138
139
139
140
// If transformFile is specified, we first want to get the response back in memory so we can
140
141
// 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 ())
142
143
responseType = ResponseType .FileStorage ;
143
144
else
144
145
responseType = ResponseType .KeepInMemory ;
@@ -167,6 +168,11 @@ public static void cancelTask(String taskId) {
167
168
}
168
169
}
169
170
171
+ private void invoke_callback (Object ... args ) {
172
+ if (this .callbackfired ) return ;
173
+ this .callback .invoke (args );
174
+ }
175
+
170
176
private final int QUERY = 1314 ;
171
177
private ScheduledExecutorService scheduledExecutorService = Executors .newScheduledThreadPool (1 );
172
178
private Future <?> future ;
@@ -301,7 +307,7 @@ public void run() {
301
307
File file = new File (ReactNativeBlobUtilFS .getTmpPath (cacheKey ) + ext );
302
308
303
309
if (file .exists ()) {
304
- callback . invoke (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , file .getAbsolutePath ());
310
+ invoke_callback (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , file .getAbsolutePath ());
305
311
return ;
306
312
}
307
313
}
@@ -355,7 +361,7 @@ else if (this.options.fileCache)
355
361
}
356
362
357
363
if (!found ) {
358
- callback . invoke ("No available WiFi connections." , null , null );
364
+ invoke_callback ("No available WiFi connections." , null , null );
359
365
releaseTaskResource ();
360
366
return ;
361
367
}
@@ -551,9 +557,9 @@ public void onFailure(@NonNull Call call, @NonNull IOException e) {
551
557
// check if this error caused by socket timeout
552
558
if (e .getClass ().equals (SocketTimeoutException .class )) {
553
559
respInfo .putBoolean ("timeout" , true );
554
- callback . invoke ("The request timed out." , null , null );
560
+ invoke_callback ("The request timed out." , null , null );
555
561
} else
556
- callback . invoke (e .getLocalizedMessage (), null , null );
562
+ invoke_callback (e .getLocalizedMessage (), null , null );
557
563
releaseTaskResource ();
558
564
}
559
565
@@ -586,7 +592,7 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO
586
592
} catch (Exception error ) {
587
593
error .printStackTrace ();
588
594
releaseTaskResource ();
589
- callback . invoke ("ReactNativeBlobUtil request error: " + error .getMessage () + error .getCause ());
595
+ invoke_callback ("ReactNativeBlobUtil request error: " + error .getMessage () + error .getCause ());
590
596
}
591
597
}
592
598
@@ -631,7 +637,7 @@ private void done(Response resp) {
631
637
ins .close ();
632
638
os .flush ();
633
639
os .close ();
634
- callback . invoke (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , dest );
640
+ invoke_callback (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , dest );
635
641
}
636
642
// response data directly pass to JS context as string.
637
643
else {
@@ -649,15 +655,15 @@ private void done(Response resp) {
649
655
}
650
656
try (FileOutputStream fos = new FileOutputStream (file )) {
651
657
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 );
654
660
return ;
655
661
}
656
- callback . invoke (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , this .destPath );
662
+ invoke_callback (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , this .destPath );
657
663
return ;
658
664
}
659
665
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 ));
661
667
return ;
662
668
}
663
669
try {
@@ -667,21 +673,21 @@ private void done(Response resp) {
667
673
decoder .decode (ByteBuffer .wrap (b ));
668
674
// If the data contains invalid characters the following lines will be skipped.
669
675
String utf8 = new String (b , charSet );
670
- callback . invoke (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_UTF8 , utf8 );
676
+ invoke_callback (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_UTF8 , utf8 );
671
677
}
672
678
// This usually means the data contains invalid unicode characters but still valid data,
673
679
// it's binary data, so send it as a normal string
674
680
catch (CharacterCodingException ignored ) {
675
681
if (responseFormat == ResponseFormat .UTF8 ) {
676
682
String utf8 = new String (b );
677
- callback . invoke (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_UTF8 , utf8 );
683
+ invoke_callback (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_UTF8 , utf8 );
678
684
} 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 ));
680
686
}
681
687
}
682
688
}
683
689
} 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 );
685
691
}
686
692
break ;
687
693
case FileStorage :
@@ -713,26 +719,26 @@ private void done(Response resp) {
713
719
} catch (IOException exception ) {
714
720
exception .printStackTrace ();
715
721
}
716
- callback . invoke ("Unexpected FileStorage response file: " + responseBodyString , null );
722
+ invoke_callback ("Unexpected FileStorage response file: " + responseBodyString , null );
717
723
} else {
718
- callback . invoke ("Unexpected FileStorage response with no file." , null );
724
+ invoke_callback ("Unexpected FileStorage response with no file." , null );
719
725
}
720
726
return ;
721
727
}
722
728
723
729
if (ReactNativeBlobUtilFileResp != null && !ReactNativeBlobUtilFileResp .isDownloadComplete ()) {
724
- callback . invoke ("Download interrupted." , null );
730
+ invoke_callback ("Download interrupted." , null );
725
731
} else {
726
732
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 );
728
734
}
729
735
730
736
break ;
731
737
default :
732
738
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" ));
734
740
} 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 );
736
742
}
737
743
break ;
738
744
}
@@ -856,7 +862,7 @@ public void onReceive(Context context, Intent intent) {
856
862
Cursor c = dm .query (query );
857
863
// #236 unhandled null check for DownloadManager.query() return value
858
864
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 );
860
866
return ;
861
867
}
862
868
@@ -867,7 +873,7 @@ public void onReceive(Context context, Intent intent) {
867
873
// #297 handle failed request
868
874
int statusCode = c .getInt (c .getColumnIndex (DownloadManager .COLUMN_STATUS ));
869
875
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 );
871
877
return ;
872
878
}
873
879
String contentUri = c .getString (c .getColumnIndex (DownloadManager .COLUMN_LOCAL_URI ));
@@ -896,17 +902,17 @@ public void onReceive(Context context, Intent intent) {
896
902
if (!exists )
897
903
throw new Exception ("Download manager download failed, the file does not downloaded to destination." );
898
904
else
899
- this .callback . invoke (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , customDest );
905
+ this .invoke_callback (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , customDest );
900
906
901
907
} catch (Exception ex ) {
902
908
ex .printStackTrace ();
903
- this .callback . invoke (ex .getLocalizedMessage (), null );
909
+ this .invoke_callback (ex .getLocalizedMessage (), null );
904
910
}
905
911
} else {
906
912
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 );
908
914
else
909
- this .callback . invoke (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , filePath );
915
+ this .invoke_callback (null , ReactNativeBlobUtilConst .RNFB_RESPONSE_PATH , filePath );
910
916
}
911
917
912
918
}
0 commit comments