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

Commit 389eda8

Browse files
committed
Add fetch.uploadProgress and fetch.progress overload method #140
1 parent 25f77e8 commit 389eda8

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ public void onResponse(Call call, Response response) throws IOException {
394394
DownloadManager dm = (DownloadManager)RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
395395
dm.addCompletedDownload(title, desc, scannable, mime, destPath, contentLength, notification);
396396
}
397+
397398
done(response);
398399
}
399400
});
@@ -488,6 +489,7 @@ private void done(Response resp) {
488489
} catch (Exception ignored) {
489490
ignored.printStackTrace();
490491
}
492+
this.destPath = this.destPath.replace("?append=true", "");
491493
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
492494
break;
493495
default:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public RNFetchBlobFileResp(ReactApplicationContext ctx, String taskId, ResponseB
4343
this.mPath = path;
4444
if (path != null) {
4545
boolean appendToExistingFile = path.contains("?append=true");
46+
path = path.replace("?append=true", "");
47+
mPath = path;
4648
File f = new File(path);
4749
if(f.exists() == false)
4850
f.createNewFile();

src/index.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,37 @@ function fetch(...args:any):Promise {
194194
// extend Promise object, add `progress`, `uploadProgress`, and `cancel`
195195
// method for register progress event handler and cancel request.
196196
// Add second parameter for performance purpose #140
197-
promise.progress = (fn, interval:number=250, count:number=-1) => {
197+
// When there's only one argument pass to this method, use default `interval`
198+
// and `count`, otherwise use the given on.
199+
// TODO : code refactor, move `uploadProgress` and `progress` to StatefulPromise
200+
promise.progress = (...args) => {
201+
let interval = 250
202+
let count = -1
203+
let fn = () => {}
204+
if(args.length === 2) {
205+
interval = args[0].interval
206+
interval = args[0].count
207+
fn = args[1]
208+
}
209+
else {
210+
fn = args[0]
211+
}
198212
promise.onProgress = fn
199213
RNFetchBlob.enableProgressReport(taskId, interval, count)
200214
return promise
201215
}
202-
promise.uploadProgress = (fn, interval:number=250, count:number=-1) => {
216+
promise.uploadProgress = (...args) => {
217+
let interval = 250
218+
let count = -1
219+
let fn = () => {}
220+
if(args.length === 2) {
221+
interval = args[0].interval
222+
interval = args[0].count
223+
fn = args[1]
224+
}
225+
else {
226+
fn = args[0]
227+
}
203228
promise.onUploadProgress = fn
204229
RNFetchBlob.enableUploadProgressReport(taskId, interval, count)
205230
return promise

0 commit comments

Comments
 (0)