Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 5f3c018

Browse files
committed
Merge branch '0.10.7' of github.com:wkh237/react-native-fetch-blob into 0.10.7
2 parents 6bde516 + d83d800 commit 5f3c018

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import android.content.IntentFilter;
88
import android.database.Cursor;
99
import android.net.Uri;
10+
import android.os.Build;
1011
import android.util.Base64;
1112

1213
import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
1314
import com.RNFetchBlob.Response.RNFetchBlobFileResp;
15+
import com.facebook.common.logging.FLog;
1416
import com.facebook.react.bridge.Arguments;
1517
import com.facebook.react.bridge.Callback;
1618
import com.facebook.react.bridge.ReactApplicationContext;
@@ -21,6 +23,7 @@
2123
import com.facebook.react.bridge.WritableMap;
2224
import com.facebook.react.modules.core.DeviceEventManagerModule;
2325
import com.facebook.react.modules.network.OkHttpClientProvider;
26+
import com.facebook.react.modules.network.TLSSocketFactory;
2427

2528
import java.io.File;
2629
import java.io.FileOutputStream;
@@ -35,11 +38,14 @@
3538
import java.nio.charset.Charset;
3639
import java.nio.charset.CharsetEncoder;
3740
import java.util.ArrayList;
41+
import java.util.List;
3842
import java.util.HashMap;
43+
3944
import java.util.concurrent.TimeUnit;
4045

4146
import okhttp3.Call;
4247
import okhttp3.ConnectionPool;
48+
import okhttp3.ConnectionSpec;
4349
import okhttp3.Headers;
4450
import okhttp3.Interceptor;
4551
import okhttp3.MediaType;
@@ -48,6 +54,8 @@
4854
import okhttp3.RequestBody;
4955
import okhttp3.Response;
5056
import okhttp3.ResponseBody;
57+
import okhttp3.TlsVersion;
58+
5159

5260
public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
5361

@@ -366,9 +374,10 @@ public Response intercept(Chain chain) throws IOException {
366374
clientBuilder.retryOnConnectionFailure(false);
367375
clientBuilder.followRedirects(options.followRedirect);
368376
clientBuilder.followSslRedirects(options.followRedirect);
377+
clientBuilder.retryOnConnectionFailure(true);
369378

379+
OkHttpClient client = enableTls12OnPreLollipop(clientBuilder).build();
370380

371-
OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();
372381
Call call = client.newCall(req);
373382
taskTable.put(taskId, call);
374383
call.enqueue(new okhttp3.Callback() {
@@ -683,5 +692,28 @@ public void onReceive(Context context, Intent intent) {
683692
}
684693
}
685694

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+
686718

687719
}

ios/RNFetchBlobFS.m

+5-5
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,11 @@ - (NSString *)openWithPath:(NSString *)destPath encode:(nullable NSString *)enco
568568

569569
// Write file chunk into an opened stream
570570
- (void)writeEncodeChunk:(NSString *) chunk {
571-
NSMutableData * decodedData = [NSData alloc];
571+
NSData * decodedData = nil;
572572
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"]) {
576576
decodedData = [chunk dataUsingEncoding:NSUTF8StringEncoding];
577577
}
578578
else if([[self.encoding lowercaseString] isEqualToString:@"ascii"]) {
@@ -793,4 +793,4 @@ + (void) writeAssetToPath:(ALAssetRepresentation * )rep dest:(NSString *)dest
793793
return;
794794
}
795795

796-
@end
796+
@end

polyfill/Blob.js

+16
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ export default class Blob extends EventTarget {
130130
// Blob data from file path
131131
else if(typeof data === 'string' && data.startsWith('RNFetchBlob-file://')) {
132132
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
133135
this._ref = String(data).replace('RNFetchBlob-file://', '')
134136
let orgPath = this._ref
135137
if(defer)
@@ -282,6 +284,20 @@ export default class Blob extends EventTarget {
282284
})
283285
}
284286

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+
285301
_invokeOnCreateEvent() {
286302
log.verbose('invoke create event', this._onCreated)
287303
this._blobCreated = true

0 commit comments

Comments
 (0)