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

Commit 130d9f3

Browse files
committed
Change Download Manager complete handler #75
1 parent 161c86c commit 130d9f3

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -558,33 +558,38 @@ public void onReceive(Context context, Intent intent) {
558558
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
559559
dm.query(query);
560560
Cursor c = dm.query(query);
561+
String filePath = null;
562+
// the file exists in media content database
561563
if (c.moveToFirst()) {
562564
String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
563565
Uri uri = Uri.parse(contentUri);
564566
Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
565567
// use default destination of DownloadManager
566568
if (cursor != null) {
567569
cursor.moveToFirst();
568-
String filePath = cursor.getString(0);
570+
filePath = cursor.getString(0);
569571
cursor.close();
570-
this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
571-
}
572-
// custom destination
573-
else {
574-
if(options.addAndroidDownloads.hasKey("path")) {
575-
try {
576-
String customDest = options.addAndroidDownloads.getString("path");
577-
boolean exists = new File(customDest).exists();
578-
if(!exists)
579-
throw new Exception("Download manager download failed, the file does not downloaded to destination.");
580-
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, customDest);
581-
582-
} catch(Exception ex) {
583-
this.callback.invoke(ex.getLocalizedMessage(), null, null);
584-
}
572+
if(filePath != null) {
573+
this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
574+
return;
585575
}
586576
}
587577
}
578+
// When the file is not found in media content database, check if custom path exists
579+
if (options.addAndroidDownloads.hasKey("path")) {
580+
try {
581+
String customDest = options.addAndroidDownloads.getString("path");
582+
boolean exists = new File(customDest).exists();
583+
if(!exists)
584+
throw new Exception("Download manager download failed, the file does not downloaded to destination.");
585+
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, customDest);
586+
587+
} catch(Exception ex) {
588+
this.callback.invoke(ex.getLocalizedMessage(), null, null);
589+
}
590+
}
591+
else
592+
this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
588593
}
589594
}
590595
}

src/ios/RNFetchBlobFS.h

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565

6666
// file stream
6767
- (void) openWithDestination;
68-
- (void) openWithId;
6968
- (NSString *)openWithPath:(NSString *)destPath encode:(nullable NSString *)encode appendData:(BOOL)append;
7069
- (void) startAssetReadStream:(NSData *)assetUrl;
7170

0 commit comments

Comments
 (0)