2424import com .ReactNativeBlobUtil .Response .ReactNativeBlobUtilDefaultResp ;
2525import com .ReactNativeBlobUtil .Response .ReactNativeBlobUtilFileResp ;
2626import com .ReactNativeBlobUtil .Utils .Tls12SocketFactory ;
27+ import com .ReactNativeBlobUtil .ReactNativeBlobUtilFS ;
2728import com .facebook .common .logging .FLog ;
2829import com .facebook .react .bridge .Arguments ;
2930import com .facebook .react .bridge .Callback ;
3233import com .facebook .react .bridge .ReadableMapKeySetIterator ;
3334import com .facebook .react .bridge .WritableArray ;
3435import com .facebook .react .bridge .WritableMap ;
36+ import com .facebook .react .bridge .ReactApplicationContext ;
3537import com .facebook .react .modules .core .DeviceEventManagerModule ;
3638
3739import 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 ));
0 commit comments