Skip to content

Commit 889ffb7

Browse files
author
Ron Radtke
committed
Update:
* adds deprecated paths as legacy paths * adjusts mediastoreage API to fallback to the correct legacy paths for Andorid < 10
2 parents a1ba9eb + 77df27f commit 889ffb7

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.facebook.react.modules.network.OkHttpClientProvider;
2929

3030
import java.io.File;
31+
import java.util.HashMap;
3132
import java.util.Map;
3233
import java.util.concurrent.LinkedBlockingQueue;
3334
import java.util.concurrent.ThreadPoolExecutor;
@@ -88,7 +89,11 @@ public String getName() {
8889

8990
@Override
9091
public Map<String, Object> getConstants() {
91-
return ReactNativeBlobUtilFS.getSystemfolders(this.getReactApplicationContext());
92+
Map<String, Object> res = new HashMap<>();
93+
res.putAll(ReactNativeBlobUtilFS.getSystemfolders(this.getReactApplicationContext()));
94+
res.putAll(ReactNativeBlobUtilFS.getLegacySystemfolders(this.getReactApplicationContext()));
95+
96+
return res;
9297
}
9398

9499
@ReactMethod
@@ -440,7 +445,7 @@ public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
440445
@ReactMethod
441446
public void writeToMediaFile(String fileUri, String path, boolean transformFile, Promise promise) {
442447
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise);
443-
if(res) promise.resolve("Success");
448+
if (res) promise.resolve("Success");
444449
}
445450

446451
@RequiresApi(api = Build.VERSION_CODES.Q)
@@ -479,7 +484,7 @@ public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promi
479484
}
480485

481486
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, false, promise);
482-
if(res) promise.resolve(fileuri.toString());
487+
if (res) promise.resolve(fileuri.toString());
483488
}
484489

485490
}

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import android.os.StatFs;
1010
import android.util.Base64;
1111

12+
import androidx.annotation.NonNull;
13+
1214
import com.facebook.react.bridge.Arguments;
1315
import com.facebook.react.bridge.Callback;
1416
import com.facebook.react.bridge.Promise;
@@ -359,6 +361,34 @@ static Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
359361
return res;
360362
}
361363

364+
/**
365+
* Static method that returns legacy system folders to JS context (usage of deprecated functions since these retunr different folders)
366+
*
367+
* @param ctx React Native application context
368+
*/
369+
370+
@NonNull
371+
@SuppressWarnings("deprecation")
372+
static Map<String, Object> getLegacySystemfolders(ReactApplicationContext ctx) {
373+
Map<String, Object> res = new HashMap<>();
374+
375+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) return ReactNativeBlobUtilFS.getSystemfolders(ctx);
376+
377+
res.put("LegacyDCIMDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath());
378+
res.put("LegacyPictureDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
379+
res.put("LegacyMusicDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath());
380+
res.put("LegacyDownloadDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
381+
res.put("LegacyMovieDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath());
382+
res.put("LegacyRingtoneDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getAbsolutePath());
383+
384+
String state = Environment.getExternalStorageState();
385+
if (state.equals(Environment.MEDIA_MOUNTED)) {
386+
res.put("LegacySDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
387+
}
388+
389+
return res;
390+
}
391+
362392
static String getExternalFilesDirPath(ReactApplicationContext ctx, String type) {
363393
File dir = ctx.getExternalFilesDir(type);
364394
if (dir != null) return dir.getAbsolutePath();
@@ -809,7 +839,7 @@ static void hash(String path, String algorithm, Promise promise) {
809839
promise.reject("EINVAL", "Invalid algorithm '" + algorithm + "', must be one of md5, sha1, sha224, sha256, sha384, sha512");
810840
return;
811841
}
812-
842+
813843
path = ReactNativeBlobUtilUtils.normalizePath(path);
814844

815845
File file = new File(path);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ private static String getRelativePath(MediaType mt, ReactApplicationContext ctx)
6969
if (mt == MediaType.Download) return Environment.DIRECTORY_DOWNLOADS;
7070
return Environment.DIRECTORY_DOWNLOADS;
7171
} else {
72-
if (mt == MediaType.Audio) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_MUSIC);
73-
if (mt == MediaType.Video) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_MOVIES);
74-
if (mt == MediaType.Image) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_PICTURES);
75-
if (mt == MediaType.Download) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS);
76-
return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS);
72+
if (mt == MediaType.Audio) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMusicDir").toString();
73+
if (mt == MediaType.Video) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMovieDir").toString();
74+
if (mt == MediaType.Image) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyPictureDir").toString();
75+
if (mt == MediaType.Download) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString();
76+
return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString();
7777
}
7878
}
7979

fs.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ const dirs = {
2424
SDCardApplicationDir: ReactNativeBlobUtil.SDCardApplicationDir, // Deprecated
2525
MainBundleDir: ReactNativeBlobUtil.MainBundleDir,
2626
LibraryDir: ReactNativeBlobUtil.LibraryDir,
27-
ApplicationSupportDir: ReactNativeBlobUtil.ApplicationSupportDir
27+
ApplicationSupportDir: ReactNativeBlobUtil.ApplicationSupportDir,
28+
29+
LegacyPictureDir: ReactNativeBlobUtil.LegacyPictureDir,
30+
LegacyMusicDir: ReactNativeBlobUtil.LegacyMusicDir,
31+
LegacyMovieDir: ReactNativeBlobUtil.LegacyMovieDir,
32+
LegacyDownloadDir: ReactNativeBlobUtil.LegacyDownloadDir,
33+
LegacyDCIMDir: ReactNativeBlobUtil.LegacyDCIMDir,
34+
LegacySDCardDir: ReactNativeBlobUtil.LegacySDCardDir, // Depracated
35+
2836
};
2937

3038
function addCode(code: string, error: Error): Error {

0 commit comments

Comments
 (0)