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

Commit 7aa9287

Browse files
committed
Merge branch 'issue_74' into 0.9.0
2 parents 849d240 + 3a3a1fa commit 7aa9287

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ public void run() {
127127
Uri uri = Uri.parse(url);
128128
DownloadManager.Request req = new DownloadManager.Request(uri);
129129
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
130-
131-
if (options.addAndroidDownloads.hasKey("title")) {
130+
if(options.addAndroidDownloads.hasKey("title")) {
132131
req.setTitle(options.addAndroidDownloads.getString("title"));
133132
}
134-
if (options.addAndroidDownloads.hasKey("description")) {
133+
if(options.addAndroidDownloads.hasKey("description")) {
135134
req.setDescription(options.addAndroidDownloads.getString("description"));
136135
}
136+
if(options.addAndroidDownloads.hasKey("path")) {
137+
req.setDestinationUri(Uri.parse("file://" + options.addAndroidDownloads.getString("path")));
138+
}
137139
// set headers
138140
ReadableMapKeySetIterator it = headers.keySetIterator();
139141
while (it.hasNextKey()) {
@@ -558,14 +560,28 @@ public void onReceive(Context context, Intent intent) {
558560
String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
559561
Uri uri = Uri.parse(contentUri);
560562
Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
563+
// use default destination of DownloadManager
561564
if (cursor != null) {
562565
cursor.moveToFirst();
563566
String filePath = cursor.getString(0);
564567
cursor.close();
565568
this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
566569
}
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+
}
569585
}
570586
}
571587
}

test/test-android.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,26 @@ describe('APK downloaded from Download Manager should correct', (report, done) =
173173
})
174174

175175
})
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+
})

0 commit comments

Comments
 (0)