Skip to content

Commit

Permalink
Fixing joltup#236 which did not expect null from DownloadManager.query()
Browse files Browse the repository at this point in the history
  • Loading branch information
mtolmacs committed Mar 2, 2019
1 parent d8c79f6 commit ede920c
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,29 +667,39 @@ public void onReceive(Context context, Intent intent) {
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
dm.query(query);
Cursor c = dm.query(query);

// #236 unhandled null check for DownloadManager.query() return value
if (c == null) {
this.callback.invoke("Download manager failed to download from " + this.url + ". Query was unsuccessful ", null, null);
return;
}

String filePath = null;
// the file exists in media content database
if (c.moveToFirst()) {
// #297 handle failed request
int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if(statusCode == DownloadManager.STATUS_FAILED) {
this.callback.invoke("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
return;
}
String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
if ( contentUri != null &&
options.addAndroidDownloads.hasKey("mime") &&
options.addAndroidDownloads.getString("mime").contains("image")) {
Uri uri = Uri.parse(contentUri);
Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
// use default destination of DownloadManager
if (cursor != null) {
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
try {
// the file exists in media content database
if (c.moveToFirst()) {
// #297 handle failed request
int statusCode = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
if(statusCode == DownloadManager.STATUS_FAILED) {
this.callback.invoke("Download manager failed to download from " + this.url + ". Status Code = " + statusCode, null, null);
return;
}
String contentUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
if ( contentUri != null &&
options.addAndroidDownloads.hasKey("mime") &&
options.addAndroidDownloads.getString("mime").contains("image")) {
Uri uri = Uri.parse(contentUri);
Cursor cursor = appCtx.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
// use default destination of DownloadManager
if (cursor != null) {
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
}
}
}
} finally {
if (c != null) {
c.close();
}
}

Expand Down

0 comments on commit ede920c

Please sign in to comment.