Skip to content

Commit 5b43136

Browse files
committed
Fix Fetch Replacement progress and cancel task issue wkh237#370
1 parent d807f0f commit 5b43136

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.RNFetchBlob;
22

33
import android.app.Activity;
4+
import android.app.DownloadManager;
45
import android.content.Intent;
56
import android.net.Uri;
67

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

342343
}
343344

345+
@ReactMethod
346+
public void addCompleteDownload (ReadableMap config, Promise promise) {
347+
DownloadManager dm = (DownloadManager) RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
348+
String path = RNFetchBlobFS.normalizePath(config.getString("path"));
349+
if(path == null) {
350+
promise.reject("RNFetchblob.addCompleteDownload can not resolve URI:" + config.getString("path"), "RNFetchblob.addCompleteDownload can not resolve URI:" + path);
351+
return;
352+
}
353+
try {
354+
WritableMap stat = RNFetchBlobFS.statFile(path);
355+
dm.addCompletedDownload(
356+
config.hasKey("title") ? config.getString("title") : "",
357+
config.hasKey("description") ? config.getString("description") : "",
358+
true,
359+
config.hasKey("mime") ? config.getString("mime") : null,
360+
path,
361+
Long.valueOf(stat.getString("size")),
362+
config.hasKey("showNotification") && config.getBoolean("showNotification")
363+
);
364+
promise.resolve(null);
365+
}
366+
catch(Exception ex) {
367+
promise.reject("RNFetchblob.addCompleteDownload failed", ex.getStackTrace().toString());
368+
}
369+
370+
}
371+
344372
}

polyfill/Fetch.js

+28-14
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,38 @@ class RNFetchBlobFetchPolyfill {
5757
// task is a progress reportable and cancellable Promise, however,
5858
// task.then is not, so we have to extend task.then with progress and
5959
// cancel function
60-
let task = promise
60+
let progressHandler, uploadHandler, cancelHandler
61+
let statefulPromise = promise
6162
.then((body) => {
62-
return RNFetchBlob.config(config)
63-
.fetch(options.method, url, options.headers, body)
63+
let task = RNFetchBlob.config(config)
64+
.fetch(options.method, url, options.headers, body)
65+
if(progressHandler)
66+
task.progress(progressHandler)
67+
if(uploadHandler)
68+
task.uploadProgress(uploadHandler)
69+
if(cancelHandler)
70+
task.cancel()
71+
return task.then((resp) => {
72+
log.verbose('response', resp)
73+
// release blob cache created when sending request
74+
if(blobCache !== null && blobCache instanceof Blob)
75+
blobCache.close()
76+
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
77+
})
6478
})
6579

66-
let statefulPromise = task.then((resp) => {
67-
log.verbose('response', resp)
68-
// release blob cache created when sending request
69-
if(blobCache !== null && blobCache instanceof Blob)
70-
blobCache.close()
71-
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
72-
})
73-
7480
// extend task.then progress with report and cancelling functions
75-
statefulPromise.cancel = task.cancel
76-
statefulPromise.progress = task.progress
77-
statefulPromise.uploadProgress = task.uploadProgress
81+
statefulPromise.progress = (fn) => {
82+
progressHandler = fn
83+
}
84+
statefulPromise.uploadProgress = (fn) => {
85+
uploadHandler = fn
86+
}
87+
statefulPromise.cancel = () => {
88+
cancelHandler = true
89+
if(task.cancel)
90+
task.cancel()
91+
}
7892

7993
return statefulPromise
8094

0 commit comments

Comments
 (0)