Pārlūkot izejas kodu

Merge branch 'issue_74' into 0.9.0

Ben Hsieh 8 gadus atpakaļ
vecāks
revīzija
7aa9287a80

+ 21
- 5
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Parādīt failu

@@ -127,13 +127,15 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
127 127
                 Uri uri = Uri.parse(url);
128 128
                 DownloadManager.Request req = new DownloadManager.Request(uri);
129 129
                 req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
130
-
131
-                if (options.addAndroidDownloads.hasKey("title")) {
130
+                if(options.addAndroidDownloads.hasKey("title")) {
132 131
                     req.setTitle(options.addAndroidDownloads.getString("title"));
133 132
                 }
134
-                if (options.addAndroidDownloads.hasKey("description")) {
133
+                if(options.addAndroidDownloads.hasKey("description")) {
135 134
                     req.setDescription(options.addAndroidDownloads.getString("description"));
136 135
                 }
136
+                if(options.addAndroidDownloads.hasKey("path")) {
137
+                    req.setDestinationUri(Uri.parse("file://" + options.addAndroidDownloads.getString("path")));
138
+                }
137 139
                 // set headers
138 140
                 ReadableMapKeySetIterator it = headers.keySetIterator();
139 141
                 while (it.hasNextKey()) {
@@ -558,14 +560,28 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
558 560
                     String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
559 561
                     Uri uri = Uri.parse(contentUri);
560 562
                     Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
563
+                    // use default destination of DownloadManager
561 564
                     if (cursor != null) {
562 565
                         cursor.moveToFirst();
563 566
                         String filePath = cursor.getString(0);
564 567
                         cursor.close();
565 568
                         this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
566 569
                     }
567
-                    else
568
-                        this.callback.invoke(null, null, null);
570
+                    // custom destination
571
+                    else {
572
+                        if(options.addAndroidDownloads.hasKey("path")) {
573
+                            try {
574
+                                String customDest = options.addAndroidDownloads.getString("path");
575
+                                boolean exists = new File(customDest).exists();
576
+                                if(!exists)
577
+                                    throw new Exception("Download manager download failed, the file does not downloaded to destination.");
578
+                                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, customDest);
579
+
580
+                            } catch(Exception ex) {
581
+                                this.callback.invoke(ex.getLocalizedMessage(), null, null);
582
+                            }
583
+                        }
584
+                    }
569 585
                 }
570 586
             }
571 587
         }

+ 23
- 0
test/test-android.js Parādīt failu

@@ -173,3 +173,26 @@ describe('APK downloaded from Download Manager should correct', (report, done) =
173 173
   })
174 174
 
175 175
 })
176
+
177
+// issue #74
178
+describe('download file to specific location using DownloadManager', (report, done) => {
179
+  let dest = dirs.DCIMDir + '/android-download-test-' +Date.now() + '.png'
180
+  RNFetchBlob.config({
181
+    addAndroidDownloads : {
182
+      useDownloadManager : true,
183
+      path : dest,
184
+      mime : 'image/png',
185
+      title : 'android-download-path-test.png',
186
+      description : 'download to specific path #74'
187
+    }
188
+  })
189
+  .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
190
+  .then((res) => fs.stat(res.path()))
191
+  .then((stat) => {
192
+    report(
193
+      <Assert key="file exists at the path"
194
+        expect={true} actual={true}/>,
195
+      <Assert key="file size correct"
196
+        expect="23975" actual={stat.size}/>)
197
+    done()
198
+  })