Skip to content

Commit 5bf78be

Browse files
committed
升级版本
1 parent 82b55a9 commit 5bf78be

25 files changed

+304
-207
lines changed

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]>

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ RNFetchBlob
242242

243243
**Use Specific File Path**
244244

245-
If you prefer a particular file path rather than randomly generated one, you can use `path` option. We've added [several constants](#user-content-dirs) in v0.5.0 which represents commonly used directories.
245+
If you prefer a particular file path rather than randomly generated one, you can use `path` option. We've added [several constants](https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#dirs) in v0.5.0 which represents commonly used directories.
246246

247247
```js
248248
let dirs = RNFetchBlob.fs.dirs
@@ -771,7 +771,7 @@ If you're going to concatenate files, you don't have to read the data to JS cont
771771
## Caveats
772772

773773
* This library does not urlencode unicode characters in URL automatically, see [#146](https://github.com/wkh237/react-native-fetch-blob/issues/146).
774-
* When a `Blob` , from existing file, the file **WILL BE REMOVE** if you `close` the blob.
774+
* When you create a `Blob` , from an existing file, the file **WILL BE REMOVED** if you `close` the blob.
775775
* If you replaced `window.XMLHttpRequest` for some reason (e.g. make Firebase SDK work), it will also affect how official `fetch` works (basically it should work just fine).
776776
* When file stream and upload/download progress event slow down your app, consider an upgrade to `0.9.6+`, use [additional arguments](https://github.com/wkh237/react-native-fetch-blob/wiki/Fetch-API#fetchprogressconfig-eventlistenerpromisernfetchblobresponse) to limit its frequency.
777777
* When passing a file path to the library, remove `file://` prefix.

android.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +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.')
25+
}
26+
27+
function getContentIntent(mime:string) {
28+
if(Platform.OS === 'android')
29+
return RNFetchBlob.getContentIntent(mime)
30+
else
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.')
2539
}
2640

2741

2842
export default {
29-
actionViewIntent
43+
actionViewIntent,
44+
getContentIntent,
45+
addCompleteDownload
3046
}

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: 82 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.RNFetchBlob;
22

3+
import android.app.Activity;
4+
import android.app.DownloadManager;
35
import android.content.Intent;
46
import android.net.Uri;
57

6-
import com.RNFetchBlob.Utils.RNFBCookieJar;
8+
import com.facebook.react.bridge.ActivityEventListener;
79
import com.facebook.react.bridge.Callback;
810
import com.facebook.react.bridge.LifecycleEventListener;
911
import com.facebook.react.bridge.Promise;
@@ -12,28 +14,64 @@
1214
import com.facebook.react.bridge.ReactMethod;
1315
import com.facebook.react.bridge.ReadableArray;
1416
import com.facebook.react.bridge.ReadableMap;
15-
import com.facebook.react.bridge.WritableArray;
17+
18+
// Cookies
1619
import com.facebook.react.bridge.WritableMap;
20+
import com.facebook.react.modules.network.ForwardingCookieHandler;
21+
import com.facebook.react.modules.network.CookieJarContainer;
22+
import com.facebook.react.modules.network.OkHttpClientProvider;
23+
import okhttp3.OkHttpClient;
24+
import okhttp3.JavaNetCookieJar;
1725

26+
import java.util.HashMap;
1827
import java.util.Map;
1928
import java.util.concurrent.LinkedBlockingQueue;
2029
import java.util.concurrent.ThreadPoolExecutor;
2130
import java.util.concurrent.TimeUnit;
2231

32+
import static android.app.Activity.RESULT_OK;
33+
import static com.RNFetchBlob.RNFetchBlobConst.GET_CONTENT_INTENT;
34+
2335
public class RNFetchBlob extends ReactContextBaseJavaModule {
2436

37+
// Cookies
38+
private final ForwardingCookieHandler mCookieHandler;
39+
private final CookieJarContainer mCookieJarContainer;
40+
private final OkHttpClient mClient;
41+
2542
static ReactApplicationContext RCTContext;
2643
static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
2744
static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
2845
static LinkedBlockingQueue<Runnable> fsTaskQueue = new LinkedBlockingQueue<>();
2946
static ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor(2, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
3047
static public boolean ActionViewVisible = false;
48+
static HashMap<Integer, Promise> promiseTable = new HashMap<>();
3149

3250
public RNFetchBlob(ReactApplicationContext reactContext) {
3351

3452
super(reactContext);
3553

54+
mClient = OkHttpClientProvider.getOkHttpClient();
55+
mCookieHandler = new ForwardingCookieHandler(reactContext);
56+
mCookieJarContainer = (CookieJarContainer) mClient.cookieJar();
57+
mCookieJarContainer.setCookieJar(new JavaNetCookieJar(mCookieHandler));
58+
3659
RCTContext = reactContext;
60+
reactContext.addActivityEventListener(new ActivityEventListener() {
61+
@Override
62+
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
63+
if(requestCode == GET_CONTENT_INTENT && resultCode == RESULT_OK) {
64+
Uri d = data.getData();
65+
promiseTable.get(GET_CONTENT_INTENT).resolve(d.toString());
66+
promiseTable.remove(GET_CONTENT_INTENT);
67+
}
68+
}
69+
70+
@Override
71+
public void onNewIntent(Intent intent) {
72+
73+
}
74+
});
3775
}
3876

3977
@Override
@@ -228,35 +266,6 @@ public void run() {
228266

229267
}
230268

231-
@ReactMethod
232-
/**
233-
* Get cookies belongs specific host.
234-
* @param host String domain name.
235-
*/
236-
public void getCookies(String domain, Promise promise) {
237-
try {
238-
WritableMap cookies = RNFBCookieJar.getCookies(domain);
239-
promise.resolve(cookies);
240-
} catch(Exception err) {
241-
promise.reject("RNFetchBlob.getCookies", err.getMessage());
242-
}
243-
}
244-
245-
@ReactMethod
246-
/**
247-
* Remove cookies for specific domain
248-
* @param domain String of the domain
249-
* @param promise JSC promise injected by RN
250-
*/
251-
public void removeCookies(String domain, Promise promise) {
252-
try {
253-
RNFBCookieJar.removeCookies(domain);
254-
promise.resolve(null);
255-
} catch(Exception err) {
256-
promise.reject("RNFetchBlob.removeCookies", err.getMessage());
257-
}
258-
}
259-
260269
@ReactMethod
261270
/**
262271
* @param path Stream file path
@@ -314,12 +323,51 @@ public void enableUploadProgressReport(String taskId, int interval, int count) {
314323

315324
@ReactMethod
316325
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
317-
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();
318-
}
326+
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, mClient, callback).run();
327+
}
319328

320329
@ReactMethod
321330
public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
322-
new RNFetchBlobReq(options, taskId, method, url, headers, null, body, callback).run();
331+
new RNFetchBlobReq(options, taskId, method, url, headers, null, body, mClient, callback).run();
332+
}
333+
334+
@ReactMethod
335+
public void getContentIntent(String mime, Promise promise) {
336+
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
337+
if(mime != null)
338+
i.setType(mime);
339+
else
340+
i.setType("*/*");
341+
promiseTable.put(GET_CONTENT_INTENT, promise);
342+
this.getReactApplicationContext().startActivityForResult(i, GET_CONTENT_INTENT, null);
343+
344+
}
345+
346+
@ReactMethod
347+
public void addCompleteDownload (ReadableMap config, Promise promise) {
348+
DownloadManager dm = (DownloadManager) RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
349+
String path = RNFetchBlobFS.normalizePath(config.getString("path"));
350+
if(path == null) {
351+
promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"), "RNFetchblob.addCompleteDownload can not resolve URI:" + path);
352+
return;
353+
}
354+
try {
355+
WritableMap stat = RNFetchBlobFS.statFile(path);
356+
dm.addCompletedDownload(
357+
config.hasKey("title") ? config.getString("title") : "",
358+
config.hasKey("description") ? config.getString("description") : "",
359+
true,
360+
config.hasKey("mime") ? config.getString("mime") : null,
361+
path,
362+
Long.valueOf(stat.getString("size")),
363+
config.hasKey("showNotification") && config.getBoolean("showNotification")
364+
);
365+
promise.resolve(null);
366+
}
367+
catch(Exception ex) {
368+
promise.reject("RNFetchblob.addCompleteDownload failed", ex.getStackTrace().toString());
369+
}
370+
323371
}
324372

325373
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public class RNFetchBlobConst {
1313
public static final String RNFB_RESPONSE_BASE64 = "base64";
1414
public static final String RNFB_RESPONSE_UTF8 = "utf8";
1515
public static final String RNFB_RESPONSE_PATH = "path";
16+
public static final Integer GET_CONTENT_INTENT = 99900;
1617

1718
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ static public Map<String, Object> getSystemfolders(ReactApplicationContext ctx)
203203
state = Environment.getExternalStorageState();
204204
if (state.equals(Environment.MEDIA_MOUNTED)) {
205205
res.put("SDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
206+
res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
206207
}
207208
res.put("MainBundleDir", ctx.getApplicationInfo().dataDir);
208209
return res;
@@ -234,7 +235,8 @@ public void readStream(String path, String encoding, int bufferSize, int tick, f
234235

235236
InputStream fs;
236237
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
237-
fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
238+
fs = RNFetchBlob.RCTContext.getAssets()
239+
.open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
238240
}
239241
else {
240242
fs = new FileInputStream(new File(path));
@@ -248,9 +250,7 @@ public void readStream(String path, String encoding, int bufferSize, int tick, f
248250
CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
249251
while ((cursor = fs.read(buffer)) != -1) {
250252
encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
251-
String chunk = new String(buffer);
252-
if(cursor != bufferSize)
253-
chunk = chunk.substring(0, cursor);
253+
String chunk = new String(buffer, 0, cursor);
254254
emitStreamEvent(streamId, "data", chunk);
255255
if(tick > 0)
256256
SystemClock.sleep(tick);
@@ -292,7 +292,8 @@ public void readStream(String path, String encoding, int bufferSize, int tick, f
292292
buffer = null;
293293

294294
} catch (Exception err) {
295-
emitStreamEvent(streamId, "error", "Failed to convert data to "+encoding+" encoded string, this might due to the source data is not able to convert using this encoding.");
295+
emitStreamEvent(streamId, "warn", "Failed to convert data to "+encoding+" encoded string, this might due to the source data is not able to convert using this encoding.");
296+
err.printStackTrace();
296297
}
297298
}
298299

@@ -878,13 +879,21 @@ static boolean isAsset(String path) {
878879
return false;
879880
}
880881

882+
/**
883+
* Normalize the path, remove URI scheme (xxx://) so that we can handle it.
884+
* @param path URI string.
885+
* @return Normalized string
886+
*/
881887
static String normalizePath(String path) {
882888
if(path == null)
883889
return null;
884-
Uri uri = Uri.parse(path);
885-
if(uri.getScheme() == null) {
890+
if(!path.matches("\\w+\\:.*"))
886891
return path;
892+
if(path.startsWith("file://")) {
893+
return path.replace("file://", "");
887894
}
895+
896+
Uri uri = Uri.parse(path);
888897
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
889898
return path;
890899
}

0 commit comments

Comments
 (0)