Skip to content

Commit

Permalink
Update:
Browse files Browse the repository at this point in the history
* adds deprecated paths as legacy paths
* adjusts mediastoreage API to fallback to the correct legacy paths for Andorid < 10
  • Loading branch information
Ron Radtke committed Jul 2, 2022
2 parents a1ba9eb + 77df27f commit 889ffb7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.facebook.react.modules.network.OkHttpClientProvider;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -88,7 +89,11 @@ public String getName() {

@Override
public Map<String, Object> getConstants() {
return ReactNativeBlobUtilFS.getSystemfolders(this.getReactApplicationContext());
Map<String, Object> res = new HashMap<>();
res.putAll(ReactNativeBlobUtilFS.getSystemfolders(this.getReactApplicationContext()));
res.putAll(ReactNativeBlobUtilFS.getLegacySystemfolders(this.getReactApplicationContext()));

return res;
}

@ReactMethod
Expand Down Expand Up @@ -440,7 +445,7 @@ public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
@ReactMethod
public void writeToMediaFile(String fileUri, String path, boolean transformFile, Promise promise) {
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise);
if(res) promise.resolve("Success");
if (res) promise.resolve("Success");
}

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

boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, false, promise);
if(res) promise.resolve(fileuri.toString());
if (res) promise.resolve(fileuri.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.os.StatFs;
import android.util.Base64;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
Expand Down Expand Up @@ -359,6 +361,34 @@ static Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
return res;
}

/**
* Static method that returns legacy system folders to JS context (usage of deprecated functions since these retunr different folders)
*
* @param ctx React Native application context
*/

@NonNull
@SuppressWarnings("deprecation")
static Map<String, Object> getLegacySystemfolders(ReactApplicationContext ctx) {
Map<String, Object> res = new HashMap<>();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) return ReactNativeBlobUtilFS.getSystemfolders(ctx);

res.put("LegacyDCIMDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath());
res.put("LegacyPictureDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
res.put("LegacyMusicDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath());
res.put("LegacyDownloadDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
res.put("LegacyMovieDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath());
res.put("LegacyRingtoneDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getAbsolutePath());

String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
res.put("LegacySDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
}

return res;
}

static String getExternalFilesDirPath(ReactApplicationContext ctx, String type) {
File dir = ctx.getExternalFilesDir(type);
if (dir != null) return dir.getAbsolutePath();
Expand Down Expand Up @@ -809,7 +839,7 @@ static void hash(String path, String algorithm, Promise promise) {
promise.reject("EINVAL", "Invalid algorithm '" + algorithm + "', must be one of md5, sha1, sha224, sha256, sha384, sha512");
return;
}

path = ReactNativeBlobUtilUtils.normalizePath(path);

File file = new File(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ private static String getRelativePath(MediaType mt, ReactApplicationContext ctx)
if (mt == MediaType.Download) return Environment.DIRECTORY_DOWNLOADS;
return Environment.DIRECTORY_DOWNLOADS;
} else {
if (mt == MediaType.Audio) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_MUSIC);
if (mt == MediaType.Video) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_MOVIES);
if (mt == MediaType.Image) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_PICTURES);
if (mt == MediaType.Download) return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS);
return ReactNativeBlobUtilFS.getExternalFilesDirPath(ctx, Environment.DIRECTORY_DOWNLOADS);
if (mt == MediaType.Audio) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMusicDir").toString();
if (mt == MediaType.Video) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyMovieDir").toString();
if (mt == MediaType.Image) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyPictureDir").toString();
if (mt == MediaType.Download) return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString();
return ReactNativeBlobUtilFS.getLegacySystemfolders(ctx).get("LegacyDownloadDir").toString();
}
}

Expand Down
10 changes: 9 additions & 1 deletion fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ const dirs = {
SDCardApplicationDir: ReactNativeBlobUtil.SDCardApplicationDir, // Deprecated
MainBundleDir: ReactNativeBlobUtil.MainBundleDir,
LibraryDir: ReactNativeBlobUtil.LibraryDir,
ApplicationSupportDir: ReactNativeBlobUtil.ApplicationSupportDir
ApplicationSupportDir: ReactNativeBlobUtil.ApplicationSupportDir,

LegacyPictureDir: ReactNativeBlobUtil.LegacyPictureDir,
LegacyMusicDir: ReactNativeBlobUtil.LegacyMusicDir,
LegacyMovieDir: ReactNativeBlobUtil.LegacyMovieDir,
LegacyDownloadDir: ReactNativeBlobUtil.LegacyDownloadDir,
LegacyDCIMDir: ReactNativeBlobUtil.LegacyDCIMDir,
LegacySDCardDir: ReactNativeBlobUtil.LegacySDCardDir, // Depracated

};

function addCode(code: string, error: Error): Error {
Expand Down

0 comments on commit 889ffb7

Please sign in to comment.