Skip to content

Commit

Permalink
Update:
Browse files Browse the repository at this point in the history
* Media store support

fixes joltup#95
fixes joltup#62
fixes joltup#100
fixes joltup#91
Merge remote-tracking branch 'origin/develop'
  • Loading branch information
Ron Radtke committed Feb 5, 2022
2 parents 6801d80 + 71d1c44 commit dea7c81
Show file tree
Hide file tree
Showing 23 changed files with 1,460 additions and 753 deletions.
877 changes: 471 additions & 406 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ android {

dependencies {
implementation "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"
implementation 'org.apache.commons:commons-lang3:3.0'
}

afterEvaluate { project ->
Expand Down
107 changes: 86 additions & 21 deletions android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import android.app.Activity;
import android.app.DownloadManager;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.util.SparseArray;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.FileProvider;

import android.util.SparseArray;
import android.content.ActivityNotFoundException;

import com.ReactNativeBlobUtil.Utils.FileDescription;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
Expand All @@ -21,24 +22,22 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;

// Cookies
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.network.ForwardingCookieHandler;
import com.facebook.react.modules.network.CookieJarContainer;
import com.facebook.react.modules.network.ForwardingCookieHandler;
import com.facebook.react.modules.network.OkHttpClientProvider;

import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient;

import javax.annotation.Nullable;

import java.io.File;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;

import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient;

import static android.app.Activity.RESULT_OK;
import static com.ReactNativeBlobUtil.ReactNativeBlobUtilConst.GET_CONTENT_INTENT;

Expand Down Expand Up @@ -81,6 +80,7 @@ public void onNewIntent(Intent intent) {
});
}

@NonNull
@Override
public String getName() {
return "ReactNativeBlobUtil";
Expand Down Expand Up @@ -114,9 +114,13 @@ public void run() {
@ReactMethod
public void actionViewIntent(String path, String mime, @Nullable String chooserTitle, final Promise promise) {
try {
Uri uriForFile = FileProvider.getUriForFile(this.getReactApplicationContext(),
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));

Uri uriForFile = null;
if (!ReactNativeBlobUtilUtils.isContentUri(path)) {
uriForFile = FileProvider.getUriForFile(this.getReactApplicationContext(),
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));
} else {
uriForFile = Uri.parse(path);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// Create the intent with data and type
Expand Down Expand Up @@ -170,7 +174,7 @@ public void onHostDestroy() {

@ReactMethod
public void writeArrayChunk(final String streamId, final ReadableArray dataArray, final Callback callback) {
ReactNativeBlobUtilFS.writeArrayChunk(streamId, dataArray, callback);
ReactNativeBlobUtilStream.writeArrayChunk(streamId, dataArray, callback);
}

@ReactMethod
Expand Down Expand Up @@ -210,17 +214,17 @@ public void ls(String path, Promise promise) {

@ReactMethod
public void writeStream(String path, String encode, boolean append, Callback callback) {
new ReactNativeBlobUtilFS(this.getReactApplicationContext()).writeStream(path, encode, append, callback);
new ReactNativeBlobUtilStream(this.getReactApplicationContext()).writeStream(path, encode, append, callback);
}

@ReactMethod
public void writeChunk(String streamId, String data, Callback callback) {
ReactNativeBlobUtilFS.writeChunk(streamId, data, callback);
ReactNativeBlobUtilStream.writeChunk(streamId, data, callback);
}

@ReactMethod
public void closeStream(String streamId, Callback callback) {
ReactNativeBlobUtilFS.closeStream(streamId, callback);
ReactNativeBlobUtilStream.closeStream(streamId, callback);
}

@ReactMethod
Expand Down Expand Up @@ -313,7 +317,7 @@ public void readStream(final String path, final String encoding, final int buffe
fsThreadPool.execute(new Runnable() {
@Override
public void run() {
ReactNativeBlobUtilFS fs = new ReactNativeBlobUtilFS(ctx);
ReactNativeBlobUtilStream fs = new ReactNativeBlobUtilStream(ctx);
fs.readStream(path, encoding, bufferSize, tick, streamId);
}
});
Expand Down Expand Up @@ -386,7 +390,7 @@ public void addCompleteDownload(ReadableMap config, Promise promise) {
promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload config or path missing.");
return;
}
String path = ReactNativeBlobUtilFS.normalizePath(config.getString("path"));
String path = ReactNativeBlobUtilUtils.normalizePath(config.getString("path"));
if (path == null) {
promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload can not resolve URI:" + config.getString("path"));
return;
Expand Down Expand Up @@ -418,4 +422,65 @@ public void getSDCardDir(Promise promise) {
public void getSDCardApplicationDir(Promise promise) {
ReactNativeBlobUtilFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise);
}

@ReactMethod
public void createMediaFile(ReadableMap filedata, String mt, Promise promise) {
if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString());
return;
}
if (mt == null) promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");

FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
Uri res = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt));
if (res != null) promise.resolve(res.toString());
else promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
}

@RequiresApi(api = Build.VERSION_CODES.Q)
@ReactMethod
public void writeToMediaFile(String fileUri, String path, Promise promise) {
boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(Uri.parse(fileUri), path, promise);
if(res) promise.resolve("Success");
}

@ReactMethod
public void copyToInternal(String contentUri, String destpath, Promise promise) {
ReactNativeBlobUtilMediaCollection.copyToInternal(Uri.parse(contentUri), destpath, promise);
}

@ReactMethod
public void getBlob(String contentUri, String encoding, Promise promise) {
ReactNativeBlobUtilMediaCollection.getBlob(Uri.parse(contentUri), encoding, promise);
}

@RequiresApi(api = Build.VERSION_CODES.Q)
@ReactMethod
public void copyToMediaStore(ReadableMap filedata, String mt, String path, Promise promise) {
if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid filedata: " + filedata.toString());
return;
}
if (mt == null) {
promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid mediatype");
return;
}
if (path == null) {
promise.reject("ReactNativeBlobUtil.createMediaFile", "invalid path");
return;
}

FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
Uri fileuri = ReactNativeBlobUtilMediaCollection.createNewMediaFile(file, ReactNativeBlobUtilMediaCollection.MediaType.valueOf(mt));

if (fileuri == null) {
promise.reject("ReactNativeBlobUtil.createMediaFile", "File could not be created");
return;
}

boolean res = ReactNativeBlobUtilMediaCollection.writeToMediaFile(fileuri, path, promise);

if(res) promise.resolve(fileuri.toString());
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.ReactNativeBlobUtil;

import androidx.annotation.NonNull;

import android.net.Uri;
import android.util.Base64;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
Expand All @@ -19,8 +20,6 @@
import java.io.InputStream;
import java.util.ArrayList;

import android.net.Uri;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
Expand Down Expand Up @@ -145,17 +144,17 @@ private InputStream getRequestStream() throws Exception {
// upload from storage
if (rawBody.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX)) {
String orgPath = rawBody.substring(ReactNativeBlobUtilConst.FILE_PREFIX.length());
orgPath = ReactNativeBlobUtilFS.normalizePath(orgPath);
orgPath = ReactNativeBlobUtilUtils.normalizePath(orgPath);
// upload file from assets
if (ReactNativeBlobUtilFS.isAsset(orgPath)) {
if (ReactNativeBlobUtilUtils.isAsset(orgPath)) {
try {
String assetName = orgPath.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "");
return ReactNativeBlobUtil.RCTContext.getAssets().open(assetName);
} catch (Exception e) {
throw new Exception("error when getting request stream from asset : " + e.getLocalizedMessage());
}
} else {
File f = new File(ReactNativeBlobUtilFS.normalizePath(orgPath));
File f = new File(ReactNativeBlobUtilUtils.normalizePath(orgPath));
try {
if (!f.exists())
f.createNewFile();
Expand Down Expand Up @@ -187,7 +186,7 @@ private InputStream getRequestStream() throws Exception {
* Create a temp file that contains content of multipart form data content
*
* @return The cache file object
* @throws IOException
* @throws IOException .
*/
private File createMultipartBodyCache() throws IOException {
String boundary = "ReactNativeBlobUtil-" + mTaskId;
Expand Down Expand Up @@ -215,9 +214,9 @@ private File createMultipartBodyCache() throws IOException {
// upload from storage
if (data.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX)) {
String orgPath = data.substring(ReactNativeBlobUtilConst.FILE_PREFIX.length());
orgPath = ReactNativeBlobUtilFS.normalizePath(orgPath);
orgPath = ReactNativeBlobUtilUtils.normalizePath(orgPath);
// path starts with content://
if (ReactNativeBlobUtilFS.isAsset(orgPath)) {
if (ReactNativeBlobUtilUtils.isAsset(orgPath)) {
try {
String assetName = orgPath.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "");
InputStream in = ctx.getAssets().open(assetName);
Expand All @@ -228,7 +227,7 @@ private File createMultipartBodyCache() throws IOException {
}
// data from normal files
else {
File file = new File(ReactNativeBlobUtilFS.normalizePath(orgPath));
File file = new File(ReactNativeBlobUtilUtils.normalizePath(orgPath));
if (file.exists()) {
FileInputStream fs = new FileInputStream(file);
pipeStreamToFileStream(fs, os);
Expand Down Expand Up @@ -282,7 +281,7 @@ private File createMultipartBodyCache() throws IOException {
*
* @param stream The input stream
* @param sink The request body buffer sink
* @throws IOException
* @throws IOException .
*/
private void pipeStreamToSink(InputStream stream, BufferedSink sink) throws IOException {
byte[] chunk = new byte[10240];
Expand Down Expand Up @@ -332,9 +331,9 @@ private ArrayList<FormField> countFormDataLength() throws IOException {
// upload from storage
if (data.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX)) {
String orgPath = data.substring(ReactNativeBlobUtilConst.FILE_PREFIX.length());
orgPath = ReactNativeBlobUtilFS.normalizePath(orgPath);
orgPath = ReactNativeBlobUtilUtils.normalizePath(orgPath);
// path starts with asset://
if (ReactNativeBlobUtilFS.isAsset(orgPath)) {
if (ReactNativeBlobUtilUtils.isAsset(orgPath)) {
try {
String assetName = orgPath.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "");
long length = ctx.getAssets().open(assetName).available();
Expand All @@ -345,7 +344,7 @@ private ArrayList<FormField> countFormDataLength() throws IOException {
}
// general files
else {
File file = new File(ReactNativeBlobUtilFS.normalizePath(orgPath));
File file = new File(ReactNativeBlobUtilUtils.normalizePath(orgPath));
total += file.length();
}
} else if (data.startsWith(ReactNativeBlobUtilConst.CONTENT_PREFIX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class ReactNativeBlobUtilConfig {
ReactNativeBlobUtilConfig(ReadableMap options) {
if (options == null)
return;
this.fileCache = options.hasKey("fileCache") ? options.getBoolean("fileCache") : false;
this.fileCache = options.hasKey("fileCache") && options.getBoolean("fileCache");
this.path = options.hasKey("path") ? options.getString("path") : null;
this.appendExt = options.hasKey("appendExt") ? options.getString("appendExt") : "";
this.trusty = options.hasKey("trusty") ? options.getBoolean("trusty") : false;
this.wifiOnly = options.hasKey("wifiOnly") ? options.getBoolean("wifiOnly") : false;
this.trusty = options.hasKey("trusty") && options.getBoolean("trusty");
this.wifiOnly = options.hasKey("wifiOnly") && options.getBoolean("wifiOnly");
if (options.hasKey("addAndroidDownloads")) {
this.addAndroidDownloads = options.getMap("addAndroidDownloads");
}
Expand All @@ -45,8 +45,8 @@ class ReactNativeBlobUtilConfig {
}
this.key = options.hasKey("key") ? options.getString("key") : null;
this.mime = options.hasKey("contentType") ? options.getString("contentType") : null;
this.increment = options.hasKey("increment") ? options.getBoolean("increment") : false;
this.auto = options.hasKey("auto") ? options.getBoolean("auto") : false;
this.increment = options.hasKey("increment") && options.getBoolean("increment");
this.auto = options.hasKey("auto") && options.getBoolean("auto");
if (options.hasKey("timeout")) {
this.timeout = options.getInt("timeout");
}
Expand Down

0 comments on commit dea7c81

Please sign in to comment.