Browse Source

Add fetch.uploadProgress and fetch.progress overload method #140

Ben Hsieh 7 years ago
parent
commit
389eda8d07

+ 2
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

@@ -394,6 +394,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
394 394
                         DownloadManager dm = (DownloadManager)RNFetchBlob.RCTContext.getSystemService(RNFetchBlob.RCTContext.DOWNLOAD_SERVICE);
395 395
                         dm.addCompletedDownload(title, desc, scannable, mime, destPath, contentLength, notification);
396 396
                     }
397
+
397 398
                     done(response);
398 399
                 }
399 400
             });
@@ -488,6 +489,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
488 489
                 } catch (Exception ignored) {
489 490
                     ignored.printStackTrace();
490 491
                 }
492
+                this.destPath = this.destPath.replace("?append=true", "");
491 493
                 callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
492 494
                 break;
493 495
             default:

+ 2
- 0
src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java View File

@@ -43,6 +43,8 @@ public class RNFetchBlobFileResp extends ResponseBody {
43 43
         this.mPath = path;
44 44
         if (path != null) {
45 45
             boolean appendToExistingFile = path.contains("?append=true");
46
+            path = path.replace("?append=true", "");
47
+            mPath = path;
46 48
             File f = new File(path);
47 49
             if(f.exists() == false)
48 50
                 f.createNewFile();

+ 27
- 2
src/index.js View File

@@ -194,12 +194,37 @@ function fetch(...args:any):Promise {
194 194
   // extend Promise object, add `progress`, `uploadProgress`, and `cancel`
195 195
   // method for register progress event handler and cancel request.
196 196
   // 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
+    }
198 212
     promise.onProgress = fn
199 213
     RNFetchBlob.enableProgressReport(taskId, interval, count)
200 214
     return promise
201 215
   }
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
+    }
203 228
     promise.onUploadProgress = fn
204 229
     RNFetchBlob.enableUploadProgressReport(taskId, interval, count)
205 230
     return promise