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

Commit c4d5594

Browse files
committed
Merge branch 'master' into 0.11.0
2 parents 07e9d71 + 9c875a8 commit c4d5594

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+365
-296
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.3
4+
2. Bug fix request to "Bug Fix Branch" 0.10.6
55
3. Correct README.md can directly to master

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 xeiyan@gmail.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
[![release](https://img.shields.io/github/release/wkh237/react-native-fetch-blob.svg?style=flat-square)](https://github.com/wkh237/react-native-fetch-blob/releases) [![npm](https://img.shields.io/npm/v/react-native-fetch-blob.svg?style=flat-square)](https://www.npmjs.com/package/react-native-fetch-blob) ![](https://img.shields.io/badge/PR-Welcome-brightgreen.svg?style=flat-square) [![](https://img.shields.io/badge/Wiki-Public-brightgreen.svg?style=flat-square)](https://github.com/wkh237/react-native-fetch-blob/wiki) [![npm](https://img.shields.io/npm/l/react-native-fetch-blob.svg?maxAge=2592000&style=flat-square)]()
33

44

5-
A project committed to making file access and data transfer easier, efficient for React Native developers.
6-
7-
> If you're going to use GitHub repo as npm dependency, please visit the [archive repository](https://github.com/wkh237/react-native-fetch-blob-package/releases/tag/v0.9.6).
8-
9-
> For Firebase Storage solution, please upgrade to latest version for best compatibility.
5+
A project committed to making file access and data transfer easier and more efficient for React Native developers.
6+
> For Firebase Storage solution, please upgrade to the latest version for the best compatibility.
107
118
## Features
129
- Transfer data directly from/to storage without BASE64 bridging
@@ -16,7 +13,7 @@ A project committed to making file access and data transfer easier, efficient fo
1613
- Blob, File, XMLHttpRequest polyfills that make browser-based library available in RN (experimental)
1714
- JSON stream supported base on [Oboe.js](https://github.com/jimhigson/oboe.js/) @jimhigson
1815

19-
## TOC
16+
## TOC (visit [Wiki](https://github.com/wkh237/react-native-fetch-blob/wiki) to get the complete documentation)
2017
* [About](#user-content-about)
2118
* [Installation](#user-content-installation)
2219
* [HTTP Data Transfer](#user-content-http-data-transfer)
@@ -35,15 +32,16 @@ A project committed to making file access and data transfer easier, efficient fo
3532
* [File stream](#user-content-file-stream)
3633
* [Manage cached files](#user-content-cache-file-management)
3734
* [Web API Polyfills](#user-content-web-api-polyfills)
38-
* [Performance Tips](#user-content-performance-tips)
35+
* [Performance Tips](#user-content-performance-tipsd)
3936
* [API References](https://github.com/wkh237/react-native-fetch-blob/wiki/Fetch-API)
4037
* [Caveats](#user-content-caveats)
4138
* [Development](#user-content-development)
4239

4340
## About
4441

45-
This project was initially for solving the issue [facebook/react-native#854](https://github.com/facebook/react-native/issues/854) because React Native lack of `Blob` implementation and it's problematic when transferring binary data. Now the project is committed to making file access and transfer easier, efficient for React Native developers.
46-
We've implemented highly customizable filesystem and network module which plays well together. For example, upload and download data directly from/to storage which is much more efficient in some cases(especially for large ones). The file system supports file stream, so you don't have to worry about OOM problem when accessing large files.
42+
This project was started in the cause of solving issue [facebook/react-native#854](https://github.com/facebook/react-native/issues/854), React Native's lacks of `Blob` implementation which results into problems when transferring binary data.
43+
44+
It is committed to making file access and transfer easier and more efficient for React Native developers. We've implemented highly customizable filesystem and network module which plays well together. For example, developers can upload and download data directly from/to storage, which is more efficient, especially for large files. The file system supports file stream, so you don't have to worry about OOM problem when accessing large files.
4745

4846
In `0.8.0` we introduced experimental Web API polyfills that make it possible to use browser-based libraries in React Native, such as, [FireBase JS SDK](https://github.com/wkh237/rn-firebase-storage-upload-sample)
4947

@@ -56,16 +54,22 @@ Install package from npm
5654
npm install --save react-native-fetch-blob
5755
```
5856

59-
Or if using CocoaPods, add the pod to your `Podfile`, for example:
57+
Or if using CocoaPods, add the pod to your `Podfile`
6058

6159
```
6260
pod 'react-native-fetch-blob',
6361
:path => '../node_modules/react-native-fetch-blob'
6462
```
6563

64+
After `0.10.3` you can install this package directly from Github
65+
66+
```sh
67+
# replace <branch_name> with any one of the branches
68+
npm install --save github:wkh237/react-native-fetch-blob-package#<branch_name>
69+
```
6670
**Automatically Link Native Modules**
6771

68-
For 0.29.2+ projects, simply link native packages via the following command because rnpm has been merged into react-native, you no longer need it.
72+
For 0.29.2+ projects, simply link native packages via the following command (note: rnpm has been merged into react-native)
6973

7074
```
7175
react-native link
@@ -112,7 +116,7 @@ If you're going to access external storage (say, SD card storage) for `Android 5
112116

113117
```
114118

115-
Also, if you're going to use `Android Download Manager` you have to add this to `AndroidManifetst.xml`
119+
Also, if you're going to use `Android Download Manager` you have to add this to `AndroidManifest.xml`
116120

117121
```diff
118122
<intent-filter>
@@ -149,9 +153,9 @@ var RNFetchBlob = require('react-native-fetch-blob').default
149153

150154
### Regular Request
151155

152-
After `0.8.0` react-native-fetch-blob automatically decide how to send the body by checking its type and `Content-Type` in the header. The rule is described in the following diagram
156+
After `0.8.0` react-native-fetch-blob automatically decides how to send the body by checking its type and `Content-Type` in the header. The rule is described in the following diagram
153157

154-
<img src="img/RNFB-flow (1).png" style="width : 90%" />
158+
<img src="img/RNFB-Flow-hd.png" style="width : 90%" />
155159

156160
To sum up:
157161

@@ -238,7 +242,7 @@ RNFetchBlob
238242

239243
**Use Specific File Path**
240244

241-
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.
242246

243247
```js
244248
let dirs = RNFetchBlob.fs.dirs
@@ -529,7 +533,9 @@ When downloading large files on Android it is recommended to use `Download Manag
529533

530534
<img src="img/download-manager.png" width="256">
531535

532-
When using DownloadManager, `fileCache` and `path` properties in `config` will not take effect, because Android DownloadManager can only store files to external storage. When download complete, DownloadManager will generate a file path so that you can deal with it.
536+
When using DownloadManager, `fileCache` and `path` properties in `config` will not take effect, because Android DownloadManager can only store files to external storage, also notice that Download Manager can only support `GET` method, which means the request body will be ignored.
537+
538+
When download complete, DownloadManager will generate a file path so that you can deal with it.
533539

534540
```js
535541
RNFetchBlob
@@ -551,6 +557,7 @@ RNFetchBlob
551557
})
552558
```
553559

560+
Your app might not have right to remove/change the file created by Download Manager, therefore you might need to [set custom location to the download task](https://github.com/wkh237/react-native-fetch-blob/issues/236).
554561

555562
**Download Notification and Visibility in Download App (Android Only)**
556563

@@ -806,7 +813,7 @@ If you're going to concatenate files, you don't have to read the data to JS cont
806813
## Caveats
807814

808815
* This library does not urlencode unicode characters in URL automatically, see [#146](https://github.com/wkh237/react-native-fetch-blob/issues/146).
809-
* When a `Blob` , from existing file, the file **WILL BE REMOVE** if you `close` the blob.
816+
* When you create a `Blob` , from an existing file, the file **WILL BE REMOVED** if you `close` the blob.
810817
* 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).
811818
* 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.
812819
* When passing a file path to the library, remove `file://` prefix.

android.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ function actionViewIntent(path:string, mime:string = 'text/plain') {
2424
return Promise.reject('RNFetchBlob.actionViewIntent only supports Android.')
2525
}
2626

27+
function getContentIntent(mime:string) {
28+
if(Platform.OS === 'android')
29+
return RNFetchBlob.getContentIntent(mime)
30+
else
31+
return Promise.reject('RNFetchBlob.getContentIntent only supports Android.')
32+
}
33+
2734

2835
export default {
29-
actionViewIntent
36+
actionViewIntent,
37+
getContentIntent
3038
}

android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
package="com.RNFetchBlob">
33

44
<application
5-
android:label="@string/app_name"
6-
android:supportsRtl="true">
5+
android:label="@string/app_name">
76

87
<service
98
android:name=".RNFetchBlobService"

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.RNFetchBlob;
22

3+
import android.app.Activity;
34
import android.content.Intent;
45
import android.net.Uri;
56
import android.support.annotation.Nullable;
7+
import android.util.Log;
68

79
import com.RNFetchBlob.Utils.RNFBCookieJar;
10+
import com.facebook.react.bridge.ActivityEventListener;
811
import com.facebook.react.bridge.Callback;
912
import com.facebook.react.bridge.LifecycleEventListener;
1013
import com.facebook.react.bridge.Promise;
@@ -16,11 +19,16 @@
1619
import com.facebook.react.bridge.WritableArray;
1720
import com.facebook.react.bridge.WritableMap;
1821

22+
import java.util.HashMap;
1923
import java.util.Map;
24+
import java.util.UUID;
2025
import java.util.concurrent.LinkedBlockingQueue;
2126
import java.util.concurrent.ThreadPoolExecutor;
2227
import java.util.concurrent.TimeUnit;
2328

29+
import static android.app.Activity.RESULT_OK;
30+
import static com.RNFetchBlob.RNFetchBlobConst.GET_CONTENT_INTENT;
31+
2432
public class RNFetchBlob extends ReactContextBaseJavaModule {
2533

2634
static ReactApplicationContext RCTContext;
@@ -29,12 +37,28 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
2937
static LinkedBlockingQueue<Runnable> fsTaskQueue = new LinkedBlockingQueue<>();
3038
static ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor(2, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
3139
static public boolean ActionViewVisible = false;
40+
static HashMap<Integer, Promise> promiseTable = new HashMap<>();
3241

3342
public RNFetchBlob(ReactApplicationContext reactContext) {
3443

3544
super(reactContext);
3645

3746
RCTContext = reactContext;
47+
reactContext.addActivityEventListener(new ActivityEventListener() {
48+
@Override
49+
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
50+
if(requestCode == GET_CONTENT_INTENT && resultCode == RESULT_OK) {
51+
Uri d = data.getData();
52+
promiseTable.get(GET_CONTENT_INTENT).resolve(d.toString());
53+
promiseTable.remove(GET_CONTENT_INTENT);
54+
}
55+
}
56+
57+
@Override
58+
public void onNewIntent(Intent intent) {
59+
60+
}
61+
});
3862
}
3963

4064
@Override
@@ -345,4 +369,15 @@ public void run() {
345369
});
346370
}
347371

372+
public void getContentIntent(String mime, Promise promise) {
373+
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
374+
if(mime != null)
375+
i.setType(mime);
376+
else
377+
i.setType("*/*");
378+
promiseTable.put(GET_CONTENT_INTENT, promise);
379+
this.getReactApplicationContext().startActivityForResult(i, GET_CONTENT_INTENT, null);
380+
381+
}
382+
348383
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public class RNFetchBlobConst {
1515
public static final String RNFB_RESPONSE_BASE64 = "base64";
1616
public static final String RNFB_RESPONSE_UTF8 = "utf8";
1717
public static final String RNFB_RESPONSE_PATH = "path";
18+
public static final Integer GET_CONTENT_INTENT = 99900;
1819

1920
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ public void readStream(String path, String encoding, int bufferSize, int tick, f
236236

237237
InputStream fs;
238238
if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
239-
fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
239+
fs = RNFetchBlob.RCTContext.getAssets()
240+
.open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
240241
}
241242
else {
242243
fs = new FileInputStream(new File(path));
@@ -251,8 +252,9 @@ public void readStream(String path, String encoding, int bufferSize, int tick, f
251252
while ((cursor = fs.read(buffer)) != -1) {
252253
encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
253254
String chunk = new String(buffer);
254-
if(cursor != bufferSize)
255+
if(cursor != bufferSize) {
255256
chunk = chunk.substring(0, cursor);
257+
}
256258
emitStreamEvent(streamId, "data", chunk);
257259
if(tick > 0)
258260
SystemClock.sleep(tick);
@@ -294,7 +296,8 @@ public void readStream(String path, String encoding, int bufferSize, int tick, f
294296
buffer = null;
295297

296298
} catch (Exception err) {
297-
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.");
299+
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.");
300+
err.printStackTrace();
298301
}
299302
}
300303

0 commit comments

Comments
 (0)