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.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Nov 7, 2016
2 parents 386e98e + 422b308 commit 5da0113
Show file tree
Hide file tree
Showing 48 changed files with 52,288 additions and 190 deletions.
14 changes: 5 additions & 9 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@

Andreas Amsenius <andreas@amsenius.se>
Corentin Smith <corentin.smith@gmail.com>
Dmitry Petukhov <dmitryvpetukhov@gmail.com>

Erik Smartt <code@eriksmartt.com>

Evgeniy Baraniuk <ev.baraniuk@gmail.com>

Juan B. Rodriguez <jbrodriguez@gmail.com>

Kaishley <kklingachetti@msn.com>

Mike Monteith <mike@mikemonteith.com>
Nguyen Cao Nhat Linh <nhatlinh95@gmail.com>

Tim Suchanek <tim.suchanek@gmail.com>
follower <github@rancidbacon.com>

francisco-sanchez-molina <psm1984@gmail.com>

kejinliang <kejinliang@users.noreply.github.com>
smartt <github@eriksmartt.com>
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ A project committed to make file acess and data transfer easier, efficient for R
- Native-to-native file manipulation API, reduce JS bridging performance loss
- File stream support for dealing with large file
- Blob, File, XMLHttpRequest polyfills that make browser-based library available in RN (experimental)
- JSON stream supported base on [Oboe.js@jimhigson](https://github.com/jimhigson/oboe.js/)

## TOC
* [About](#user-content-about)
Expand Down Expand Up @@ -81,13 +82,13 @@ Optionally, use the following command to add Android permissions to `AndroidMani
RNFB_ANDROID_PERMISSIONS=true react-native link
```

pre 0.29 projects
pre 0.29 projects

```sh
RNFB_ANDROID_PERMISSIONS=true rnpm link
```

The link script might not take effect if you have non-default project structure, please visit [the wiki](https://github.com/wkh237/react-native-fetch-blob/wiki/Manually-Link-Package) to manually link the package.
The link script might not take effect if you have non-default project structure, please visit [the wiki](https://github.com/wkh237/react-native-fetch-blob/wiki/Manually-Link-Package) to manually link the pacakge.

**Grant Permission to External storage for Android 5.0 or lower**

Expand Down Expand Up @@ -156,10 +157,10 @@ To sum up :
- To send a form data, the `Content-Type` header does not matters. When the body is an `Array` we will set proper content type for you.
- To send binary data, you have two choices, use BASE64 encoded string or path points to a file contains the body.
- If the `Content-Type` containing substring`;BASE64` or `application/octet` the given body will be considered as a BASE64 encoded data which will be decoded to binary data as the request body.
- Otherwise, if a string starts with `RNFetchBlob-file://` (which can simply done by `RNFetchBlob.wrap(PATH_TO_THE_FILE)`), it will try to find the data from the URI string after `RNFetchBlob-file://` and use it as request body.
- Otherwise, if a string starts with `RNFetchBlob-file://` (which can simply done by `RNFetchBlob.wrap(PATH_TO_THE_FILE)`), it will try to find the data from the URI string after `RNFetchBlob-file://` and use it as request body.
- To send the body as-is, simply use a `Content-Type` header not containing `;BASE64` or `application/octet`.

> It is Worth to mentioning that the HTTP request uses cache by default, if you're going to disable it simply add a Cache Control header `'Cache-Control' : 'no-store'`
> It is Worth to mentioning that the HTTP request uses cache by default, if you're going to disable it simply add a Cache Control header `'Cache-Control' : 'no-store'`
> After 0.9.4, we disabled `Chunked` transfer encoding by default, if you're going to use it, you should explicitly set header `Transfer-Encoding` to `Chunked`.
Expand Down Expand Up @@ -382,7 +383,7 @@ What if you want to append a file to form data ? Just like [upload a file from s

### Upload/Download progress

In `version >= 0.4.2` it is possible to know the upload/download progress. After `0.7.0` IOS and Android upload progress are also supported.
In `version >= 0.4.2` it is possible to know the upload/download progress. After `0.7.0` IOS and Android upload progress are also supported.

```js
RNFetchBlob.fetch('POST', 'http://www.example.com/upload', {
Expand Down Expand Up @@ -739,7 +740,7 @@ Here's a [sample app](https://github.com/wkh237/rn-firebase-storage-upload-sampl

**Read Stream and Progress Event Overhead**

When reading data via `fs.readStream` the process seems blocking JS thread when file is large, it's because the default buffer size is quite small (4kb) which result in large amount of events triggered in JS thread, try to increase the buffer size (for example 100kb = 102400) and set a larger interval (which is introduced in 0.9.4 default value is 10ms) to limit the frequency.
When reading data via `fs.readStream` the process seems blocking JS thread when file is large, it's because the default buffer size is quite small (4kb) which result in large amount of events triggered in JS thread, try to increase the buffer size (for example 100kb = 102400) and set a larger interval (which is introduced in 0.9.4 default value is 10ms) to limit the frequency.

**Reduce RCT Bridge and BASE64 Overhead**

Expand All @@ -761,7 +762,7 @@ If you're going to concatenate files, you don't have to read the data to JS cont

## Caveats

* This library does not urlencode unicode characters in URL automatically, see [#146](https://github.com/wkh237/react-native-fetch-blob/issues/146).
* This library does not urlencode unicode characters in URL automatically, see [#146](https://github.com/wkh237/react-native-fetch-blob/issues/146).
* When a `Blob` is created from existing file, the file **WILL BE REMOVE** if you `close` the blob.
* If you replaced `window.XMLHttpRequest` for some reason (e.g. make Firebase SDK work), it will also effect how official `fetch` works (basically it should work just fine).
* When file stream and upload/download progress event slow down your app, consider 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.
Expand Down
26 changes: 22 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "react-native-fetch-blob-dev-env",
"description" : "RNFB development environment, not dist package",
"version": "0.9.6",
"description": "RNFB development environment, not dist package",
"version": "0.10.0-dev",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"update-info": "sh scripts/contributors.sh",
"test": "sh test.sh"
},
"devDependencies": {
Expand All @@ -13,5 +14,22 @@
"chokidar": "^1.5.1",
"express": "^4.13.4",
"multer": "^1.1.0"
}
}
},
"contributors": [
"Andreas Amsenius <andreas@amsenius.se>",
"Corentin Smith <corentin.smith@gmail.com>",
"Dmitry Petukhov <dmitryvpetukhov@gmail.com>",
"Erik Smartt <code@eriksmartt.com>",
"Evgeniy Baraniuk <ev.baraniuk@gmail.com>",
"Juan B. Rodriguez <jbrodriguez@gmail.com>",
"Kaishley <kklingachetti@msn.com>",
"Mike Monteith <mike@mikemonteith.com>",
"Nguyen Cao Nhat Linh <nhatlinh95@gmail.com>",
"Tim Suchanek <tim.suchanek@gmail.com>",
"follower <github@rancidbacon.com>",
"francisco-sanchez-molina <psm1984@gmail.com>",
"kejinliang <kejinliang@users.noreply.github.com>",
"smartt <github@eriksmartt.com>",
""
]
}
1 change: 1 addition & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Always execute these scripts from root folder, not here.
21 changes: 21 additions & 0 deletions scripts/contributors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

git log --pretty="%an <%ae>" | sort |uniq > CONTRIBUTORS.md
sed -i.bak '/xeiyan@gmail.com/d' ./CONTRIBUTORS.md
rm CONTRIBUTORS.md.bak

echo "list contributors .."

cat CONTRIBUTORS.md

echo "update package.json .."

node -e "var fs = require('fs');\
var json = JSON.parse(fs.readFileSync('./package.json'));\
var contributors = String(fs.readFileSync('./CONTRIBUTORS.md')).split(/[\r\n]/);\
json.contributors = contributors;\
var distJSON = JSON.parse(fs.readFileSync('./src/package.json'));\
distJSON.contributors = contributors;\
fs.writeFileSync('./src/package.json', JSON.stringify(distJSON, null, 2));\
fs.writeFileSync('./package.json', JSON.stringify(json, null, 2));"

echo "done"
1 change: 1 addition & 0 deletions test.sh → scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ node -e "var fs=require('fs'); var pkg = JSON.parse(fs.readFileSync('./RNFetchBl
cd "${TEST_APP_PATH}"
# npm install --save "${CWD}/src"
npm install --save react-native-fetch-blob
# libs that requires web API polyfills
npm install --save firebase
react-native link

Expand Down
29 changes: 25 additions & 4 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# react-native-fetch-blob
[![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)]()


A project committed to make file acess and data transfer easier, efficient for React Native developers.

# [Please visit our Github Page for latest document](https://github.com/wkh237/react-native-fetch-blob)

## Features
- Transfer data directly from/to storage without BASE64 bridging
- File API supports normal files, Asset files, and CameraRoll files
Expand Down Expand Up @@ -85,7 +82,7 @@ pre 0.29 projects
RNFB_ANDROID_PERMISSIONS=true rnpm link
```

The link script might not take effect if you have non-default project structure, please visit [the wiki](https://github.com/wkh237/react-native-fetch-blob/wiki/Manually-Link-Package/_edit) to manually link the pacakge.
The link script might not take effect if you have non-default project structure, please visit [the wiki](https://github.com/wkh237/react-native-fetch-blob/wiki/Manually-Link-Package) to manually link the pacakge.

**Grant Permission to External storage for Android 5.0 or lower**

Expand Down Expand Up @@ -403,6 +400,30 @@ In `version >= 0.4.2` it is possible to know the upload/download progress. After
})
```

In `0.9.6`, you can specify an optional first argument which contains `count` and `interval` to limit progress event frequency (this will be done in native context in order to reduce RCT bridge overhead). Notice that `count` argument will not work if the server does not provide response content length.


```js
RNFetchBlob.fetch('POST', 'http://www.example.com/upload', {
... some headers,
'Content-Type' : 'octet-stream'
}, base64DataString)
// listen to upload progress event, emit every 250ms
.uploadProgress({ interval : 250 },(written, total) => {
console.log('uploaded', written / total)
})
// listen to download progress event, every 10%
.progress({ count : 10 }, (received, total) => {
console.log('progress', received / total)
})
.then((resp) => {
// ...
})
.catch((err) => {
// ...
})
```

### Cancel Request

After `0.7.0` it is possible to cancel a HTTP request. When the request cancel, it will definately throws an promise rejection, be sure to catch it.
Expand Down
55 changes: 52 additions & 3 deletions src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import android.content.Intent;
import android.net.Uri;

import com.RNFetchBlob.Utils.RNFBCookieJar;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;

import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -23,10 +26,12 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
static LinkedBlockingQueue<Runnable> fsTaskQueue = new LinkedBlockingQueue<>();
static ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor(2, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
static public boolean ActionViewVisible = false;

public RNFetchBlob(ReactApplicationContext reactContext) {

super(reactContext);

RCTContext = reactContext;
}

Expand All @@ -52,14 +57,33 @@ public void run() {
}

@ReactMethod
public void actionViewIntent(String path, String mime, Promise promise) {
public void actionViewIntent(String path, String mime, final Promise promise) {
try {
Intent intent= new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("file://" + path), mime);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

this.getReactApplicationContext().startActivity(intent);
promise.resolve(null);
ActionViewVisible = true;

final LifecycleEventListener listener = new LifecycleEventListener() {
@Override
public void onHostResume() {
if(ActionViewVisible)
promise.resolve(null);
RCTContext.removeLifecycleEventListener(this);
}

@Override
public void onHostPause() {

}

@Override
public void onHostDestroy() {

}
};
RCTContext.addLifecycleEventListener(listener);
} catch(Exception ex) {
promise.reject(ex.getLocalizedMessage());
}
Expand Down Expand Up @@ -203,6 +227,20 @@ public void run() {

}

@ReactMethod
/**
* Get cookies belongs specific host.
* @param host String host name.
*/
public void getCookies(String host, Promise promise) {
try {
WritableArray cookies = RNFBCookieJar.getCookies(host);
promise.resolve(cookies);
} catch(Exception err) {
promise.reject("RNFetchBlob.getCookies", err.getMessage());
}
}

@ReactMethod
/**
* @param path Stream file path
Expand Down Expand Up @@ -241,6 +279,17 @@ public void enableProgressReport(String taskId, int interval, int count) {
RNFetchBlobReq.progressReport.put(taskId, config);
}

@ReactMethod
public void df(final Callback callback) {
fsThreadPool.execute(new Runnable() {
@Override
public void run() {
RNFetchBlobFS.df(callback);
}
});
}


@ReactMethod
public void enableUploadProgressReport(String taskId, int interval, int count) {
RNFetchBlobProgressConfig config = new RNFetchBlobProgressConfig(true, interval, count, RNFetchBlobProgressConfig.ReportType.Upload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public class RNFetchBlobConfig {
public String key;
public String mime;
public Boolean auto;
public Boolean overwrite = true;
public long timeout = 60000;
public Boolean increment = false;
public ReadableArray binaryContentTypes = null;

RNFetchBlobConfig(ReadableMap options) {
Expand All @@ -31,8 +33,15 @@ public class RNFetchBlobConfig {
}
if(options.hasKey("binaryContentTypes"))
this.binaryContentTypes = options.getArray("binaryContentTypes");
if(this.path != null && path.toLowerCase().contains("?append=true")) {
this.overwrite = false;
}

if(options.hasKey("overwrite"))
this.overwrite = options.getBoolean("overwrite");
this.key = options.hasKey("key") ? options.getString("key") : null;
this.mime = options.hasKey("contentType") ? options.getString("contentType") : null;
this.increment = options.hasKey("increment") ? options.getBoolean("increment") : false;
this.auto = options.hasKey("auto") ? options.getBoolean("auto") : false;
if(options.hasKey("timeout")) {
this.timeout = options.getInt("timeout");
Expand Down

0 comments on commit 5da0113

Please sign in to comment.