Skip to content

Commit 71a8fa3

Browse files
author
Ron Radtke
committed
introduces a new option for addAndroidDownloads to store downloaded file in app specific directories
#299
1 parent 7e031f2 commit 71a8fa3

File tree

5 files changed

+192
-149
lines changed

5 files changed

+192
-149
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilReq.java

+39-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.ReactNativeBlobUtil.Response.ReactNativeBlobUtilDefaultResp;
2525
import com.ReactNativeBlobUtil.Response.ReactNativeBlobUtilFileResp;
2626
import com.ReactNativeBlobUtil.Utils.Tls12SocketFactory;
27+
import com.ReactNativeBlobUtil.ReactNativeBlobUtilFS;
2728
import com.facebook.common.logging.FLog;
2829
import com.facebook.react.bridge.Arguments;
2930
import com.facebook.react.bridge.Callback;
@@ -32,6 +33,7 @@
3233
import com.facebook.react.bridge.ReadableMapKeySetIterator;
3334
import com.facebook.react.bridge.WritableArray;
3435
import com.facebook.react.bridge.WritableMap;
36+
import com.facebook.react.bridge.ReactApplicationContext;
3537
import com.facebook.react.modules.core.DeviceEventManagerModule;
3638

3739
import java.io.File;
@@ -234,6 +236,7 @@ public boolean handleMessage(Message msg) {
234236

235237
@Override
236238
public void run() {
239+
Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
237240

238241
// use download manager instead of default HTTP implementation
239242
if (options.addAndroidDownloads != null && options.addAndroidDownloads.hasKey("useDownloadManager")) {
@@ -253,14 +256,48 @@ public void run() {
253256
req.setDescription(options.addAndroidDownloads.getString("description"));
254257
}
255258
if (options.addAndroidDownloads.hasKey("path")) {
256-
req.setDestinationUri(Uri.parse("file://" + options.addAndroidDownloads.getString("path")));
259+
String path = options.addAndroidDownloads.getString("path");
260+
File f = new File(path);
261+
File dir = f.getParentFile();
262+
263+
if (!f.exists()) {
264+
if (dir != null && !dir.exists()) {
265+
if (!dir.mkdirs() && !dir.exists()) {
266+
invoke_callback( "Failed to create parent directory of '" + path + "'", null, null);
267+
return;
268+
}
269+
}
257270
}
271+
req.setDestinationUri(Uri.parse("file://" + path));
272+
}
273+
274+
275+
if (options.addAndroidDownloads.hasKey("storeLocal") && options.addAndroidDownloads.getBoolean("storeLocal")) {
276+
String path = (String) ReactNativeBlobUtilFS.getSystemfolders((ReactApplicationContext) appCtx).get("DownloadDir");
277+
path = path + UUID.randomUUID().toString();
278+
279+
File f = new File(path);
280+
File dir = f.getParentFile();
281+
282+
if (!f.exists()) {
283+
if (dir != null && !dir.exists()) {
284+
if (!dir.mkdirs() && !dir.exists()) {
285+
invoke_callback( "Failed to create parent directory of '" + path + "'", null, null);
286+
return;
287+
}
288+
}
289+
}
290+
req.setDestinationUri(Uri.parse("file://" + path));
291+
}
292+
258293
if (options.addAndroidDownloads.hasKey("mime")) {
259294
req.setMimeType(options.addAndroidDownloads.getString("mime"));
260295
}
296+
261297
if (options.addAndroidDownloads.hasKey("mediaScannable") && options.addAndroidDownloads.getBoolean("mediaScannable")) {
262298
req.allowScanningByMediaScanner();
263299
}
300+
264301
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && options.addAndroidDownloads.hasKey("storeInDownloads") && options.addAndroidDownloads.getBoolean("storeInDownloads")) {
265302
String t = options.addAndroidDownloads.getString("title");
266303
if(t == null || t.isEmpty())
@@ -288,7 +325,7 @@ public void run() {
288325
} catch (MalformedURLException e) {
289326
e.printStackTrace();
290327
}
291-
Context appCtx = ReactNativeBlobUtilImpl.RCTContext.getApplicationContext();
328+
292329
DownloadManager dm = (DownloadManager) appCtx.getSystemService(Context.DOWNLOAD_SERVICE);
293330
downloadManagerId = dm.enqueue(req);
294331
androidDownloadManagerTaskTable.put(taskId, Long.valueOf(downloadManagerId));

index.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ export interface AddAndroidDownloads {
771771
*/
772772
description?: string;
773773
/**
774-
* The destination which the file will be downloaded, it SHOULD be a location on external storage (DCIMDir).
774+
* The destination which the file will be downloaded, it SHOULD be a location on external storage (DCIMDir). CacheDir and DocumentDir don't work
775775
*/
776776
path?: string;
777777
/**
@@ -791,6 +791,11 @@ export interface AddAndroidDownloads {
791791
* A boolean value decide whether show a notification when download complete.
792792
*/
793793
notification?: boolean;
794+
795+
/**
796+
* If true android download manager will try to save the file to the apps Download direcotry
797+
*/
798+
storeLocal?: boolean
794799
}
795800

796801
export interface ReactNativeBlobUtilResponseInfo {

0 commit comments

Comments
 (0)