7
7
import android .content .IntentFilter ;
8
8
import android .database .Cursor ;
9
9
import android .net .Uri ;
10
+ import android .os .Build ;
10
11
import android .util .Base64 ;
11
12
12
13
import com .RNFetchBlob .Response .RNFetchBlobDefaultResp ;
13
14
import com .RNFetchBlob .Response .RNFetchBlobFileResp ;
15
+ import com .facebook .common .logging .FLog ;
14
16
import com .facebook .react .bridge .Arguments ;
15
17
import com .facebook .react .bridge .Callback ;
16
18
import com .facebook .react .bridge .ReactApplicationContext ;
21
23
import com .facebook .react .bridge .WritableMap ;
22
24
import com .facebook .react .modules .core .DeviceEventManagerModule ;
23
25
import com .facebook .react .modules .network .OkHttpClientProvider ;
26
+ import com .facebook .react .modules .network .TLSSocketFactory ;
24
27
25
28
import java .io .File ;
26
29
import java .io .FileOutputStream ;
35
38
import java .nio .charset .Charset ;
36
39
import java .nio .charset .CharsetEncoder ;
37
40
import java .util .ArrayList ;
41
+ import java .util .List ;
38
42
import java .util .HashMap ;
43
+
39
44
import java .util .concurrent .TimeUnit ;
40
45
41
46
import okhttp3 .Call ;
42
47
import okhttp3 .ConnectionPool ;
48
+ import okhttp3 .ConnectionSpec ;
43
49
import okhttp3 .Headers ;
44
50
import okhttp3 .Interceptor ;
45
51
import okhttp3 .MediaType ;
48
54
import okhttp3 .RequestBody ;
49
55
import okhttp3 .Response ;
50
56
import okhttp3 .ResponseBody ;
57
+ import okhttp3 .TlsVersion ;
58
+
51
59
52
60
public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
53
61
@@ -148,8 +156,15 @@ public void run() {
148
156
if (options .addAndroidDownloads .hasKey ("path" )) {
149
157
req .setDestinationUri (Uri .parse ("file://" + options .addAndroidDownloads .getString ("path" )));
150
158
}
159
+ // #391 Add MIME type to the request
160
+ if (options .addAndroidDownloads .hasKey ("mime" )) {
161
+ req .setMimeType (options .addAndroidDownloads .getString ("mime" ));
162
+ }
151
163
// set headers
152
164
ReadableMapKeySetIterator it = headers .keySetIterator ();
165
+ if (options .addAndroidDownloads .hasKey ("mediaScannable" ) && options .addAndroidDownloads .hasKey ("mediaScannable" ) == true ) {
166
+ req .allowScanningByMediaScanner ();
167
+ }
153
168
while (it .hasNextKey ()) {
154
169
String key = it .nextKey ();
155
170
req .addRequestHeader (key , headers .getString (key ));
@@ -359,9 +374,10 @@ public Response intercept(Chain chain) throws IOException {
359
374
clientBuilder .retryOnConnectionFailure (false );
360
375
clientBuilder .followRedirects (options .followRedirect );
361
376
clientBuilder .followSslRedirects (options .followRedirect );
377
+ clientBuilder .retryOnConnectionFailure (true );
362
378
379
+ OkHttpClient client = enableTls12OnPreLollipop (clientBuilder ).build ();
363
380
364
- OkHttpClient client = clientBuilder .retryOnConnectionFailure (true ).build ();
365
381
Call call = client .newCall (req );
366
382
taskTable .put (taskId , call );
367
383
call .enqueue (new okhttp3 .Callback () {
@@ -636,16 +652,20 @@ public void onReceive(Context context, Intent intent) {
636
652
return ;
637
653
}
638
654
String contentUri = c .getString (c .getColumnIndex (DownloadManager .COLUMN_LOCAL_URI ));
639
- if (contentUri != null ) {
655
+ if ( contentUri != null &&
656
+ options .addAndroidDownloads .hasKey ("mime" ) &&
657
+ options .addAndroidDownloads .getString ("mime" ).contains ("image" )) {
640
658
Uri uri = Uri .parse (contentUri );
641
659
Cursor cursor = appCtx .getContentResolver ().query (uri , new String []{android .provider .MediaStore .Images .ImageColumns .DATA }, null , null , null );
642
- // use default destination of DownloadManager
660
+
661
+ // use default destination of DownloadManager
643
662
if (cursor != null ) {
644
663
cursor .moveToFirst ();
645
664
filePath = cursor .getString (0 );
646
665
}
647
666
}
648
667
}
668
+
649
669
// When the file is not found in media content database, check if custom path exists
650
670
if (options .addAndroidDownloads .hasKey ("path" )) {
651
671
try {
@@ -672,5 +692,28 @@ public void onReceive(Context context, Intent intent) {
672
692
}
673
693
}
674
694
695
+ public static OkHttpClient .Builder enableTls12OnPreLollipop (OkHttpClient .Builder client ) {
696
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN && Build .VERSION .SDK_INT <= Build .VERSION_CODES .KITKAT ) {
697
+ try {
698
+ client .sslSocketFactory (new TLSSocketFactory ());
699
+
700
+ ConnectionSpec cs = new ConnectionSpec .Builder (ConnectionSpec .MODERN_TLS )
701
+ .tlsVersions (TlsVersion .TLS_1_2 )
702
+ .build ();
703
+
704
+ List < ConnectionSpec > specs = new ArrayList < > ();
705
+ specs .add (cs );
706
+ specs .add (ConnectionSpec .COMPATIBLE_TLS );
707
+ specs .add (ConnectionSpec .CLEARTEXT );
708
+
709
+ client .connectionSpecs (specs );
710
+ } catch (Exception exc ) {
711
+ FLog .e ("OkHttpClientProvider" , "Error while enabling TLS 1.2" , exc );
712
+ }
713
+ }
714
+
715
+ return client ;
716
+ }
717
+
675
718
676
719
}
0 commit comments