Browse Source

Merge branch '0.10.7' of github.com:wkh237/react-native-fetch-blob into 0.10.7

Ben Hsieh 7 years ago
parent
commit
5f3c018b0a

+ 33
- 1
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

7
 import android.content.IntentFilter;
7
 import android.content.IntentFilter;
8
 import android.database.Cursor;
8
 import android.database.Cursor;
9
 import android.net.Uri;
9
 import android.net.Uri;
10
+import android.os.Build;
10
 import android.util.Base64;
11
 import android.util.Base64;
11
 
12
 
12
 import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
13
 import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
13
 import com.RNFetchBlob.Response.RNFetchBlobFileResp;
14
 import com.RNFetchBlob.Response.RNFetchBlobFileResp;
15
+import com.facebook.common.logging.FLog;
14
 import com.facebook.react.bridge.Arguments;
16
 import com.facebook.react.bridge.Arguments;
15
 import com.facebook.react.bridge.Callback;
17
 import com.facebook.react.bridge.Callback;
16
 import com.facebook.react.bridge.ReactApplicationContext;
18
 import com.facebook.react.bridge.ReactApplicationContext;
21
 import com.facebook.react.bridge.WritableMap;
23
 import com.facebook.react.bridge.WritableMap;
22
 import com.facebook.react.modules.core.DeviceEventManagerModule;
24
 import com.facebook.react.modules.core.DeviceEventManagerModule;
23
 import com.facebook.react.modules.network.OkHttpClientProvider;
25
 import com.facebook.react.modules.network.OkHttpClientProvider;
26
+import com.facebook.react.modules.network.TLSSocketFactory;
24
 
27
 
25
 import java.io.File;
28
 import java.io.File;
26
 import java.io.FileOutputStream;
29
 import java.io.FileOutputStream;
35
 import java.nio.charset.Charset;
38
 import java.nio.charset.Charset;
36
 import java.nio.charset.CharsetEncoder;
39
 import java.nio.charset.CharsetEncoder;
37
 import java.util.ArrayList;
40
 import java.util.ArrayList;
41
+import java.util.List;
38
 import java.util.HashMap;
42
 import java.util.HashMap;
43
+
39
 import java.util.concurrent.TimeUnit;
44
 import java.util.concurrent.TimeUnit;
40
 
45
 
41
 import okhttp3.Call;
46
 import okhttp3.Call;
42
 import okhttp3.ConnectionPool;
47
 import okhttp3.ConnectionPool;
48
+import okhttp3.ConnectionSpec;
43
 import okhttp3.Headers;
49
 import okhttp3.Headers;
44
 import okhttp3.Interceptor;
50
 import okhttp3.Interceptor;
45
 import okhttp3.MediaType;
51
 import okhttp3.MediaType;
48
 import okhttp3.RequestBody;
54
 import okhttp3.RequestBody;
49
 import okhttp3.Response;
55
 import okhttp3.Response;
50
 import okhttp3.ResponseBody;
56
 import okhttp3.ResponseBody;
57
+import okhttp3.TlsVersion;
58
+
51
 
59
 
52
 public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
60
 public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
53
 
61
 
366
             clientBuilder.retryOnConnectionFailure(false);
374
             clientBuilder.retryOnConnectionFailure(false);
367
             clientBuilder.followRedirects(options.followRedirect);
375
             clientBuilder.followRedirects(options.followRedirect);
368
             clientBuilder.followSslRedirects(options.followRedirect);
376
             clientBuilder.followSslRedirects(options.followRedirect);
377
+            clientBuilder.retryOnConnectionFailure(true);
369
 
378
 
379
+            OkHttpClient client = enableTls12OnPreLollipop(clientBuilder).build();
370
 
380
 
371
-            OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();
372
             Call call =  client.newCall(req);
381
             Call call =  client.newCall(req);
373
             taskTable.put(taskId, call);
382
             taskTable.put(taskId, call);
374
             call.enqueue(new okhttp3.Callback() {
383
             call.enqueue(new okhttp3.Callback() {
683
         }
692
         }
684
     }
693
     }
685
 
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
+
686
 
718
 
687
 }
719
 }

+ 5
- 5
ios/RNFetchBlobFS.m View File

568
 
568
 
569
 // Write file chunk into an opened stream
569
 // Write file chunk into an opened stream
570
 - (void)writeEncodeChunk:(NSString *) chunk {
570
 - (void)writeEncodeChunk:(NSString *) chunk {
571
-    NSMutableData * decodedData = [NSData alloc];
571
+    NSData * decodedData = nil;
572
     if([[self.encoding lowercaseString] isEqualToString:@"base64"]) {
572
     if([[self.encoding lowercaseString] isEqualToString:@"base64"]) {
573
-        decodedData = [[NSData alloc] initWithBase64EncodedData:chunk options:0];
574
-    }
575
-    if([[self.encoding lowercaseString] isEqualToString:@"utf8"]) {
573
+        decodedData = [[NSData alloc] initWithBase64EncodedString:chunk options: NSDataBase64DecodingIgnoreUnknownCharacters];
574
+    } 
575
+    else if([[self.encoding lowercaseString] isEqualToString:@"utf8"]) {
576
         decodedData = [chunk dataUsingEncoding:NSUTF8StringEncoding];
576
         decodedData = [chunk dataUsingEncoding:NSUTF8StringEncoding];
577
     }
577
     }
578
     else if([[self.encoding lowercaseString] isEqualToString:@"ascii"]) {
578
     else if([[self.encoding lowercaseString] isEqualToString:@"ascii"]) {
793
     return;
793
     return;
794
 }
794
 }
795
 
795
 
796
-@end
796
+@end

+ 16
- 0
polyfill/Blob.js View File

130
     // Blob data from file path
130
     // Blob data from file path
131
     else if(typeof data === 'string' && data.startsWith('RNFetchBlob-file://')) {
131
     else if(typeof data === 'string' && data.startsWith('RNFetchBlob-file://')) {
132
       log.verbose('create Blob cache file from file path', data)
132
       log.verbose('create Blob cache file from file path', data)
133
+      // set this flag so that we know this blob is a wrapper of an existing file
134
+      this._isReference = true
133
       this._ref = String(data).replace('RNFetchBlob-file://', '')
135
       this._ref = String(data).replace('RNFetchBlob-file://', '')
134
       let orgPath = this._ref
136
       let orgPath = this._ref
135
       if(defer)
137
       if(defer)
282
     })
284
     })
283
   }
285
   }
284
 
286
 
287
+  safeClose() {
288
+    if(this._closed)
289
+      return Promise.reject('Blob has been released.)
290
+    this._closed = true
291
+    if(!this._isReference) {
292
+      return fs.unlink(this._ref).catch((err) => {
293
+        console.warn(err)
294
+      })   
295
+    }
296
+    else {
297
+      return Promise.resolve()
298
+    }
299
+  }
300
+
285
   _invokeOnCreateEvent() {
301
   _invokeOnCreateEvent() {
286
     log.verbose('invoke create event', this._onCreated)
302
     log.verbose('invoke create event', this._onCreated)
287
     this._blobCreated = true
303
     this._blobCreated = true