24
24
import com .ReactNativeBlobUtil .Response .ReactNativeBlobUtilDefaultResp ;
25
25
import com .ReactNativeBlobUtil .Response .ReactNativeBlobUtilFileResp ;
26
26
import com .ReactNativeBlobUtil .Utils .Tls12SocketFactory ;
27
+ import com .ReactNativeBlobUtil .ReactNativeBlobUtilFS ;
27
28
import com .facebook .common .logging .FLog ;
28
29
import com .facebook .react .bridge .Arguments ;
29
30
import com .facebook .react .bridge .Callback ;
32
33
import com .facebook .react .bridge .ReadableMapKeySetIterator ;
33
34
import com .facebook .react .bridge .WritableArray ;
34
35
import com .facebook .react .bridge .WritableMap ;
36
+ import com .facebook .react .bridge .ReactApplicationContext ;
35
37
import com .facebook .react .modules .core .DeviceEventManagerModule ;
36
38
37
39
import java .io .File ;
@@ -234,6 +236,7 @@ public boolean handleMessage(Message msg) {
234
236
235
237
@ Override
236
238
public void run () {
239
+ Context appCtx = ReactNativeBlobUtilImpl .RCTContext .getApplicationContext ();
237
240
238
241
// use download manager instead of default HTTP implementation
239
242
if (options .addAndroidDownloads != null && options .addAndroidDownloads .hasKey ("useDownloadManager" )) {
@@ -253,14 +256,48 @@ public void run() {
253
256
req .setDescription (options .addAndroidDownloads .getString ("description" ));
254
257
}
255
258
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
+ }
257
270
}
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
+
258
293
if (options .addAndroidDownloads .hasKey ("mime" )) {
259
294
req .setMimeType (options .addAndroidDownloads .getString ("mime" ));
260
295
}
296
+
261
297
if (options .addAndroidDownloads .hasKey ("mediaScannable" ) && options .addAndroidDownloads .getBoolean ("mediaScannable" )) {
262
298
req .allowScanningByMediaScanner ();
263
299
}
300
+
264
301
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q && options .addAndroidDownloads .hasKey ("storeInDownloads" ) && options .addAndroidDownloads .getBoolean ("storeInDownloads" )) {
265
302
String t = options .addAndroidDownloads .getString ("title" );
266
303
if (t == null || t .isEmpty ())
@@ -288,7 +325,7 @@ public void run() {
288
325
} catch (MalformedURLException e ) {
289
326
e .printStackTrace ();
290
327
}
291
- Context appCtx = ReactNativeBlobUtilImpl . RCTContext . getApplicationContext ();
328
+
292
329
DownloadManager dm = (DownloadManager ) appCtx .getSystemService (Context .DOWNLOAD_SERVICE );
293
330
downloadManagerId = dm .enqueue (req );
294
331
androidDownloadManagerTaskTable .put (taskId , Long .valueOf (downloadManagerId ));
0 commit comments