77import android .content .IntentFilter ;
88import android .database .Cursor ;
99import android .net .Uri ;
10+ import android .os .Build ;
1011import android .util .Base64 ;
1112
1213import com .RNFetchBlob .Response .RNFetchBlobDefaultResp ;
1314import com .RNFetchBlob .Response .RNFetchBlobFileResp ;
15+ import com .facebook .common .logging .FLog ;
1416import com .facebook .react .bridge .Arguments ;
1517import com .facebook .react .bridge .Callback ;
1618import com .facebook .react .bridge .ReactApplicationContext ;
2123import com .facebook .react .bridge .WritableMap ;
2224import com .facebook .react .modules .core .DeviceEventManagerModule ;
2325import com .facebook .react .modules .network .OkHttpClientProvider ;
26+ import com .facebook .react .modules .network .TLSSocketFactory ;
2427
2528import java .io .File ;
2629import java .io .FileOutputStream ;
3538import java .nio .charset .Charset ;
3639import java .nio .charset .CharsetEncoder ;
3740import java .util .ArrayList ;
41+ import java .util .List ;
3842import java .util .HashMap ;
43+
3944import java .util .concurrent .TimeUnit ;
4045
4146import okhttp3 .Call ;
4247import okhttp3 .ConnectionPool ;
48+ import okhttp3 .ConnectionSpec ;
4349import okhttp3 .Headers ;
4450import okhttp3 .Interceptor ;
4551import okhttp3 .MediaType ;
4854import okhttp3 .RequestBody ;
4955import okhttp3 .Response ;
5056import okhttp3 .ResponseBody ;
57+ import okhttp3 .TlsVersion ;
58+
5159
5260public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
5361
@@ -148,8 +156,15 @@ public void run() {
148156 if (options .addAndroidDownloads .hasKey ("path" )) {
149157 req .setDestinationUri (Uri .parse ("file://" + options .addAndroidDownloads .getString ("path" )));
150158 }
159+ // #391 Add MIME type to the request
160+ if (options .addAndroidDownloads .hasKey ("mime" )) {
161+ req .setMimeType (options .addAndroidDownloads .getString ("mime" ));
162+ }
151163 // set headers
152164 ReadableMapKeySetIterator it = headers .keySetIterator ();
165+ if (options .addAndroidDownloads .hasKey ("mediaScannable" ) && options .addAndroidDownloads .hasKey ("mediaScannable" ) == true ) {
166+ req .allowScanningByMediaScanner ();
167+ }
153168 while (it .hasNextKey ()) {
154169 String key = it .nextKey ();
155170 req .addRequestHeader (key , headers .getString (key ));
@@ -359,9 +374,10 @@ public Response intercept(Chain chain) throws IOException {
359374 clientBuilder .retryOnConnectionFailure (false );
360375 clientBuilder .followRedirects (options .followRedirect );
361376 clientBuilder .followSslRedirects (options .followRedirect );
377+ clientBuilder .retryOnConnectionFailure (true );
362378
379+ OkHttpClient client = enableTls12OnPreLollipop (clientBuilder ).build ();
363380
364- OkHttpClient client = clientBuilder .retryOnConnectionFailure (true ).build ();
365381 Call call = client .newCall (req );
366382 taskTable .put (taskId , call );
367383 call .enqueue (new okhttp3 .Callback () {
@@ -636,16 +652,20 @@ public void onReceive(Context context, Intent intent) {
636652 return ;
637653 }
638654 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" )) {
640658 Uri uri = Uri .parse (contentUri );
641659 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
643662 if (cursor != null ) {
644663 cursor .moveToFirst ();
645664 filePath = cursor .getString (0 );
646665 }
647666 }
648667 }
668+
649669 // When the file is not found in media content database, check if custom path exists
650670 if (options .addAndroidDownloads .hasKey ("path" )) {
651671 try {
@@ -672,5 +692,28 @@ public void onReceive(Context context, Intent intent) {
672692 }
673693 }
674694
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+
675718
676719}
0 commit comments