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

Commit

Permalink
Fix Fetch Replacement progress and cancel task issue #370
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Jun 11, 2017
1 parent d807f0f commit 5b43136
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
28 changes: 28 additions & 0 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.RNFetchBlob;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.Intent;
import android.net.Uri;

Expand Down Expand Up @@ -341,4 +342,31 @@ public void getContentIntent(String mime, Promise promise) {

}

@ReactMethod
public void addCompleteDownload (ReadableMap config, Promise promise) {
DownloadManager dm = (DownloadManager) RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
String path = RNFetchBlobFS.normalizePath(config.getString("path"));
if(path == null) {
promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"), "RNFetchblob.addCompleteDownload can not resolve URI:" + path);
return;
}
try {
WritableMap stat = RNFetchBlobFS.statFile(path);
dm.addCompletedDownload(
config.hasKey("title") ? config.getString("title") : "",
config.hasKey("description") ? config.getString("description") : "",
true,
config.hasKey("mime") ? config.getString("mime") : null,
path,
Long.valueOf(stat.getString("size")),
config.hasKey("showNotification") && config.getBoolean("showNotification")
);
promise.resolve(null);
}
catch(Exception ex) {
promise.reject("RNFetchblob.addCompleteDownload failed", ex.getStackTrace().toString());
}

}

}
42 changes: 28 additions & 14 deletions polyfill/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,38 @@ class RNFetchBlobFetchPolyfill {
// task is a progress reportable and cancellable Promise, however,
// task.then is not, so we have to extend task.then with progress and
// cancel function
let task = promise
let progressHandler, uploadHandler, cancelHandler
let statefulPromise = promise
.then((body) => {
return RNFetchBlob.config(config)
.fetch(options.method, url, options.headers, body)
let task = RNFetchBlob.config(config)
.fetch(options.method, url, options.headers, body)
if(progressHandler)
task.progress(progressHandler)
if(uploadHandler)
task.uploadProgress(uploadHandler)
if(cancelHandler)
task.cancel()
return task.then((resp) => {
log.verbose('response', resp)
// release blob cache created when sending request
if(blobCache !== null && blobCache instanceof Blob)
blobCache.close()
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
})
})

let statefulPromise = task.then((resp) => {
log.verbose('response', resp)
// release blob cache created when sending request
if(blobCache !== null && blobCache instanceof Blob)
blobCache.close()
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
})

// extend task.then progress with report and cancelling functions
statefulPromise.cancel = task.cancel
statefulPromise.progress = task.progress
statefulPromise.uploadProgress = task.uploadProgress
statefulPromise.progress = (fn) => {
progressHandler = fn
}
statefulPromise.uploadProgress = (fn) => {
uploadHandler = fn
}
statefulPromise.cancel = () => {
cancelHandler = true
if(task.cancel)
task.cancel()
}

return statefulPromise

Expand Down

2 comments on commit 5b43136

@egormerkushev
Copy link

@egormerkushev egormerkushev commented on 5b43136 Jun 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing import
import com.facebook.react.bridge.WritableMap;

@timothylui
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also needs the fix 5e554ac to build

Please sign in to comment.