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

Commit 637a53b

Browse files
committed
Merge branch 'master' into 0.11.0
2 parents 6782949 + 40efd14 commit 637a53b

23 files changed

+166
-322
lines changed

.github/PULL_REQUEST_TEMPLATE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Thank you for making a pull request ! Just a gentle reminder :)
22

33
1. If the PR is offering a feature please make the request to our "Feature Branch" 0.11.0
4-
2. Bug fix request to "Bug Fix Branch" 0.10.6
4+
2. Bug fix request to "Bug Fix Branch" 0.10.7
55
3. Correct README.md can directly to master

CONTRIBUTING.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
For developers who interested in making contribution to this project, please see [https://github.com/wkh237/react-native-fetch-blob-dev](https://github.com/wkh237/react-native-fetch-blob-dev) for more information.
2+
3+
Please read the following rules before opening a PR :
4+
5+
1. If the PR is offering a feature please make the PR to our "Feature Branch" 0.11.0
6+
2. Bug fix request to "Bug Fix Branch" 0.10.6
7+
3. Correct README.md can directly to master

CONTRIBUTORS.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
2+
Amerrnath <[email protected]>
23
Andreas Amsenius <[email protected]>
4+
Andrew Jack <[email protected]>
35
Arthur Ouaki <[email protected]>
6+
7+
Ben Hsieh <[email protected]>
48
Binur Konarbai <[email protected]>
9+
510
Chris Sloey <[email protected]>
611
Corentin Smith <[email protected]>
712
Dmitry Petukhov <[email protected]>
813
Dombi Soma Kristóf <[email protected]>
914
Erik Smartt <[email protected]>
1015
Evgeniy Baraniuk <[email protected]>
1116
Frank van der Hoek <[email protected]>
17+
Guy Blank <[email protected]>
18+
Jacob Lauritzen <[email protected]>
19+
Jeremi Stadler <[email protected]>
20+
Jon San Miguel <[email protected]>
1221
Juan B. Rodriguez <[email protected]>
1322
1423
Martin Giachetti <[email protected]>
24+
Max Gurela <[email protected]>
1525
Mike Monteith <[email protected]>
1626
Naoki AINOYA <[email protected]>
1727
Nguyen Cao Nhat Linh <[email protected]>
28+
Nick Pomfret <[email protected]>
29+
1830
Petter Hesselberg <[email protected]>
31+
Reza Ghorbani <[email protected]>
32+
Simón Gómez <[email protected]>
33+
Steve Liles <[email protected]>
1934
Tim Suchanek <[email protected]>
35+
Yonsh Lin <[email protected]>
36+
2037
2138
francisco-sanchez-molina <[email protected]>
39+
gferreyra91 <[email protected]>
2240
2341
kejinliang <[email protected]>
2442
pedramsaleh <[email protected]>

android.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@ function actionViewIntent(path:string, mime:string = 'text/plain') {
2121
if(Platform.OS === 'android')
2222
return RNFetchBlob.actionViewIntent(path, mime)
2323
else
24-
return Promise.reject('RNFetchBlob.actionViewIntent only supports Android.')
24+
return Promise.reject('RNFetchBlob.android.actionViewIntent only supports Android.')
2525
}
2626

2727
function getContentIntent(mime:string) {
2828
if(Platform.OS === 'android')
2929
return RNFetchBlob.getContentIntent(mime)
3030
else
31-
return Promise.reject('RNFetchBlob.getContentIntent only supports Android.')
31+
return Promise.reject('RNFetchBlob.android.getContentIntent only supports Android.')
32+
}
33+
34+
function addCompleteDownload(config) {
35+
if(Platform.OS === 'android')
36+
return RNFetchBlob.addCompleteDownload(config)
37+
else
38+
return Promise.reject('RNFetchBlob.android.addCompleteDownload only supports Android.')
3239
}
3340

3441

3542
export default {
3643
actionViewIntent,
37-
getContentIntent
44+
getContentIntent,
45+
addCompleteDownload
3846
}

android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ repositories {
66

77
buildscript {
88
repositories {
9-
mavenCentral()
9+
jcenter()
1010
}
1111
dependencies {
12-
classpath 'com.android.tools.build:gradle:2.0.0'
12+
classpath 'com.android.tools.build:gradle:2.2.3'
1313
}
1414
}
1515

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

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.RNFetchBlob;
22

33
import android.app.Activity;
4+
import android.app.DownloadManager;
45
import android.content.Intent;
56
import android.net.Uri;
67
import android.support.annotation.Nullable;
@@ -17,6 +18,11 @@
1718
import com.facebook.react.bridge.ReadableArray;
1819
import com.facebook.react.bridge.ReadableMap;
1920
import com.facebook.react.bridge.WritableMap;
21+
import com.facebook.react.modules.network.ForwardingCookieHandler;
22+
import com.facebook.react.modules.network.CookieJarContainer;
23+
import com.facebook.react.modules.network.OkHttpClientProvider;
24+
import okhttp3.OkHttpClient;
25+
import okhttp3.JavaNetCookieJar;
2026

2127
import java.util.HashMap;
2228
import java.util.Map;
@@ -29,6 +35,11 @@
2935

3036
public class RNFetchBlob extends ReactContextBaseJavaModule {
3137

38+
// Cookies
39+
private final ForwardingCookieHandler mCookieHandler;
40+
private final CookieJarContainer mCookieJarContainer;
41+
private final OkHttpClient mClient;
42+
3243
static ReactApplicationContext RCTContext;
3344
static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
3445
static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
@@ -41,6 +52,11 @@ public RNFetchBlob(ReactApplicationContext reactContext) {
4152

4253
super(reactContext);
4354

55+
mClient = OkHttpClientProvider.getOkHttpClient();
56+
mCookieHandler = new ForwardingCookieHandler(reactContext);
57+
mCookieJarContainer = (CookieJarContainer) mClient.cookieJar();
58+
mCookieJarContainer.setCookieJar(new JavaNetCookieJar(mCookieHandler));
59+
4460
RCTContext = reactContext;
4561
reactContext.addActivityEventListener(new ActivityEventListener() {
4662
@Override
@@ -251,35 +267,6 @@ public void run() {
251267

252268
}
253269

254-
@ReactMethod
255-
/**
256-
* Get cookies belongs specific host.
257-
* @param host String domain name.
258-
*/
259-
public void getCookies(String domain, Promise promise) {
260-
try {
261-
WritableMap cookies = RNFBCookieJar.getCookies(domain);
262-
promise.resolve(cookies);
263-
} catch(Exception err) {
264-
promise.reject("RNFetchBlob.getCookies", err.getMessage());
265-
}
266-
}
267-
268-
@ReactMethod
269-
/**
270-
* Remove cookies for specific domain
271-
* @param domain String of the domain
272-
* @param promise JSC promise injected by RN
273-
*/
274-
public void removeCookies(String domain, Promise promise) {
275-
try {
276-
RNFBCookieJar.removeCookies(domain);
277-
promise.resolve(null);
278-
} catch(Exception err) {
279-
promise.reject("RNFetchBlob.removeCookies", err.getMessage());
280-
}
281-
}
282-
283270
@ReactMethod
284271
/**
285272
* @param path Stream file path
@@ -370,12 +357,12 @@ public void enableUploadProgressReport(String taskId, int interval, int count) {
370357

371358
@ReactMethod
372359
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
373-
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();
374-
}
360+
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
361+
}
375362

376363
@ReactMethod
377364
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
378-
new RNFetchBlobReq(options, taskId, method, url, headers, null, body, callback).run();
365+
new RNFetchBlobReq(options, taskId, method, url, headers, null, body, mClient, callback).run();
379366
}
380367

381368
@ReactMethod
@@ -411,4 +398,31 @@ public void getContentIntent(String mime, Promise promise) {
411398

412399
}
413400

401+
@ReactMethod
402+
public void addCompleteDownload (ReadableMap config, Promise promise) {
403+
DownloadManager dm = (DownloadManager) RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
404+
String path = RNFetchBlobFS.normalizePath(config.getString("path"));
405+
if(path == null) {
406+
promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"), "RNFetchblob.addCompleteDownload can not resolve URI:" + path);
407+
return;
408+
}
409+
try {
410+
WritableMap stat = RNFetchBlobFS.statFile(path);
411+
dm.addCompletedDownload(
412+
config.hasKey("title") ? config.getString("title") : "",
413+
config.hasKey("description") ? config.getString("description") : "",
414+
true,
415+
config.hasKey("mime") ? config.getString("mime") : null,
416+
path,
417+
Long.valueOf(stat.getString("size")),
418+
config.hasKey("showNotification") && config.getBoolean("showNotification")
419+
);
420+
promise.resolve(null);
421+
}
422+
catch(Exception ex) {
423+
promise.reject("RNFetchblob.addCompleteDownload failed", ex.getStackTrace().toString());
424+
}
425+
426+
}
427+
414428
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,7 @@ public void readStream(String path, String encoding, int bufferSize, int tick, f
258258
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
259259
while ((cursor = fs.read(buffer)) != -1) {
260260
encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
261-
String chunk = new String(buffer);
262-
if(cursor != bufferSize) {
263-
chunk = chunk.substring(0, cursor);
264-
}
261+
String chunk = new String(buffer, 0, cursor);
265262
emitStreamEvent(streamId, "data", chunk);
266263
if(tick > 0)
267264
SystemClock.sleep(tick);
@@ -896,13 +893,21 @@ static boolean isAsset(String path) {
896893
return false;
897894
}
898895

896+
/**
897+
* Normalize the path, remove URI scheme (xxx://) so that we can handle it.
898+
* @param path URI string.
899+
* @return Normalized string
900+
*/
899901
static String normalizePath(String path) {
900902
if(path == null)
901903
return null;
902-
Uri uri = Uri.parse(path);
903-
if(uri.getScheme() == null) {
904+
if(!path.matches("\\w+\\:.*"))
904905
return path;
906+
if(path.startsWith("file://")) {
907+
return path.replace("file://", "");
905908
}
909+
910+
Uri uri = Uri.parse(path);
906911
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
907912
return path;
908913
}

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
1414
import com.RNFetchBlob.Response.RNFetchBlobFileResp;
15-
import com.RNFetchBlob.Utils.RNFBCookieJar;
1615
import com.facebook.react.bridge.Arguments;
1716
import com.facebook.react.bridge.Callback;
1817
import com.facebook.react.bridge.ReactApplicationContext;
@@ -23,14 +22,12 @@
2322
import com.facebook.react.bridge.WritableArray;
2423
import com.facebook.react.bridge.WritableMap;
2524
import com.facebook.react.modules.core.DeviceEventManagerModule;
25+
import com.facebook.react.modules.network.OkHttpClientProvider;
2626

2727
import java.io.File;
2828
import java.io.FileOutputStream;
2929
import java.io.IOException;
3030
import java.io.InputStream;
31-
import java.net.CookieHandler;
32-
import java.net.CookieManager;
33-
import java.net.CookiePolicy;
3431
import java.net.MalformedURLException;
3532
import java.net.SocketException;
3633
import java.net.SocketTimeoutException;
@@ -45,7 +42,6 @@
4542

4643
import okhttp3.Call;
4744
import okhttp3.ConnectionPool;
48-
import okhttp3.CookieJar;
4945
import okhttp3.Headers;
5046
import okhttp3.Interceptor;
5147
import okhttp3.MediaType;
@@ -100,8 +96,9 @@ enum ResponseFormat {
10096
WritableMap respInfo;
10197
boolean timeout = false;
10298
ArrayList<String> redirects = new ArrayList<>();
99+
OkHttpClient client;
103100

104-
public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
101+
public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, OkHttpClient client, final Callback callback) {
105102
this.method = method.toUpperCase();
106103
this.options = new RNFetchBlobConfig(options);
107104
this.taskId = taskId;
@@ -110,6 +107,7 @@ public RNFetchBlobReq(ReadableMap options, String taskId, String method, String
110107
this.callback = callback;
111108
this.rawRequestBody = body;
112109
this.rawRequestBodyArray = arrayBody;
110+
this.client = client;
113111

114112
if(this.options.fileCache || this.options.path != null)
115113
responseType = ResponseType.FileStorage;
@@ -234,9 +232,9 @@ else if(this.options.fileCache)
234232
try {
235233
// use trusty SSL socket
236234
if (this.options.trusty) {
237-
clientBuilder = RNFetchBlobUtils.getUnsafeOkHttpClient();
235+
clientBuilder = RNFetchBlobUtils.getUnsafeOkHttpClient(client);
238236
} else {
239-
clientBuilder = new OkHttpClient.Builder();
237+
clientBuilder = client.newBuilder();
240238
}
241239

242240
final Request.Builder builder = new Request.Builder();
@@ -337,10 +335,7 @@ else if(cType.isEmpty()) {
337335
}
338336

339337
// #156 fix cookie issue
340-
341-
342338
final Request req = builder.build();
343-
clientBuilder.cookieJar(new RNFBCookieJar());
344339
clientBuilder.addNetworkInterceptor(new Interceptor() {
345340
@Override
346341
public Response intercept(Chain chain) throws IOException {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static void emitWarningEvent(String data) {
5252
.emit(RNFetchBlobConst.EVENT_MESSAGE, args);
5353
}
5454

55-
public static OkHttpClient.Builder getUnsafeOkHttpClient() {
55+
public static OkHttpClient.Builder getUnsafeOkHttpClient(OkHttpClient client) {
5656
try {
5757
// Create a trust manager that does not validate certificate chains
5858
final TrustManager[] trustAllCerts = new TrustManager[]{
@@ -78,7 +78,7 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
7878
// Create an ssl socket factory with our all-trusting manager
7979
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
8080

81-
OkHttpClient.Builder builder = new OkHttpClient.Builder();
81+
OkHttpClient.Builder builder = client.newBuilder();
8282
builder.sslSocketFactory(sslSocketFactory);
8383
builder.hostnameVerifier(new HostnameVerifier() {
8484
@Override

android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public long read(Buffer sink, long byteCount) throws IOException {
8181
byte[] bytes = new byte[(int) byteCount];
8282
long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
8383
bytesDownloaded += read > 0 ? read : 0;
84-
Log.i("bytes downloaded", String.valueOf(byteCount) + "/" + String.valueOf(read) + "=" + String.valueOf(bytesDownloaded));
8584
if (read > 0) {
8685
ofStream.write(bytes, 0, (int) read);
8786
}

0 commit comments

Comments
 (0)