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

Commit

Permalink
Merge branch '0.10.7' of github.com:wkh237/react-native-fetch-blob in…
Browse files Browse the repository at this point in the history
…to 0.10.7
  • Loading branch information
wkh237 committed Aug 2, 2017
2 parents 6bde516 + d83d800 commit 5f3c018
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
34 changes: 33 additions & 1 deletion android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.util.Base64;

import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
import com.RNFetchBlob.Response.RNFetchBlobFileResp;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -21,6 +23,7 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.TLSSocketFactory;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -35,11 +38,14 @@
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;

import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.ConnectionPool;
import okhttp3.ConnectionSpec;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
Expand All @@ -48,6 +54,8 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.TlsVersion;


public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {

Expand Down Expand Up @@ -366,9 +374,10 @@ public Response intercept(Chain chain) throws IOException {
clientBuilder.retryOnConnectionFailure(false);
clientBuilder.followRedirects(options.followRedirect);
clientBuilder.followSslRedirects(options.followRedirect);
clientBuilder.retryOnConnectionFailure(true);

OkHttpClient client = enableTls12OnPreLollipop(clientBuilder).build();

OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();
Call call = client.newCall(req);
taskTable.put(taskId, call);
call.enqueue(new okhttp3.Callback() {
Expand Down Expand Up @@ -683,5 +692,28 @@ public void onReceive(Context context, Intent intent) {
}
}

public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
try {
client.sslSocketFactory(new TLSSocketFactory());

ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.build();

List< ConnectionSpec > specs = new ArrayList < > ();
specs.add(cs);
specs.add(ConnectionSpec.COMPATIBLE_TLS);
specs.add(ConnectionSpec.CLEARTEXT);

client.connectionSpecs(specs);
} catch (Exception exc) {
FLog.e("OkHttpClientProvider", "Error while enabling TLS 1.2", exc);
}
}

return client;
}


}
10 changes: 5 additions & 5 deletions ios/RNFetchBlobFS.m
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,11 @@ - (NSString *)openWithPath:(NSString *)destPath encode:(nullable NSString *)enco

// Write file chunk into an opened stream
- (void)writeEncodeChunk:(NSString *) chunk {
NSMutableData * decodedData = [NSData alloc];
NSData * decodedData = nil;
if([[self.encoding lowercaseString] isEqualToString:@"base64"]) {
decodedData = [[NSData alloc] initWithBase64EncodedData:chunk options:0];
}
if([[self.encoding lowercaseString] isEqualToString:@"utf8"]) {
decodedData = [[NSData alloc] initWithBase64EncodedString:chunk options: NSDataBase64DecodingIgnoreUnknownCharacters];
}
else if([[self.encoding lowercaseString] isEqualToString:@"utf8"]) {
decodedData = [chunk dataUsingEncoding:NSUTF8StringEncoding];
}
else if([[self.encoding lowercaseString] isEqualToString:@"ascii"]) {
Expand Down Expand Up @@ -793,4 +793,4 @@ + (void) writeAssetToPath:(ALAssetRepresentation * )rep dest:(NSString *)dest
return;
}

@end
@end
16 changes: 16 additions & 0 deletions polyfill/Blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export default class Blob extends EventTarget {
// Blob data from file path
else if(typeof data === 'string' && data.startsWith('RNFetchBlob-file://')) {
log.verbose('create Blob cache file from file path', data)
// set this flag so that we know this blob is a wrapper of an existing file
this._isReference = true
this._ref = String(data).replace('RNFetchBlob-file://', '')
let orgPath = this._ref
if(defer)
Expand Down Expand Up @@ -282,6 +284,20 @@ export default class Blob extends EventTarget {
})
}

safeClose() {
if(this._closed)
return Promise.reject('Blob has been released.)
this._closed = true
if(!this._isReference) {
return fs.unlink(this._ref).catch((err) => {
console.warn(err)
})
}
else {
return Promise.resolve()
}
}
_invokeOnCreateEvent() {
log.verbose('invoke create event', this._onCreated)
this._blobCreated = true
Expand Down

0 comments on commit 5f3c018

Please sign in to comment.