Skip to content

Commit f420906

Browse files
author
Ron Radtke
committed
some more refactoring and cleanup
1 parent 69c0367 commit f420906

File tree

5 files changed

+390
-349
lines changed

5 files changed

+390
-349
lines changed

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
import android.app.Activity;
44
import android.app.DownloadManager;
5+
import android.content.ActivityNotFoundException;
56
import android.content.Intent;
67
import android.content.pm.PackageManager;
78
import android.net.Uri;
89
import android.os.Build;
10+
import android.util.SparseArray;
911

1012
import androidx.annotation.NonNull;
1113
import androidx.core.content.FileProvider;
1214

13-
import android.util.SparseArray;
14-
import android.content.ActivityNotFoundException;
15-
1615
import com.facebook.react.bridge.ActivityEventListener;
1716
import com.facebook.react.bridge.Callback;
1817
import com.facebook.react.bridge.LifecycleEventListener;
@@ -22,24 +21,22 @@
2221
import com.facebook.react.bridge.ReactMethod;
2322
import com.facebook.react.bridge.ReadableArray;
2423
import com.facebook.react.bridge.ReadableMap;
25-
26-
// Cookies
2724
import com.facebook.react.bridge.WritableMap;
28-
import com.facebook.react.modules.network.ForwardingCookieHandler;
2925
import com.facebook.react.modules.network.CookieJarContainer;
26+
import com.facebook.react.modules.network.ForwardingCookieHandler;
3027
import com.facebook.react.modules.network.OkHttpClientProvider;
3128

32-
import okhttp3.JavaNetCookieJar;
33-
import okhttp3.OkHttpClient;
34-
35-
import javax.annotation.Nullable;
36-
3729
import java.io.File;
3830
import java.util.Map;
3931
import java.util.concurrent.LinkedBlockingQueue;
4032
import java.util.concurrent.ThreadPoolExecutor;
4133
import java.util.concurrent.TimeUnit;
4234

35+
import javax.annotation.Nullable;
36+
37+
import okhttp3.JavaNetCookieJar;
38+
import okhttp3.OkHttpClient;
39+
4340
import static android.app.Activity.RESULT_OK;
4441
import static com.ReactNativeBlobUtil.ReactNativeBlobUtilConst.GET_CONTENT_INTENT;
4542

@@ -180,7 +177,7 @@ public void onHostDestroy() {
180177

181178
@ReactMethod
182179
public void writeArrayChunk(final String streamId, final ReadableArray dataArray, final Callback callback) {
183-
ReactNativeBlobUtilFS.writeArrayChunk(streamId, dataArray, callback);
180+
ReactNativeBlobUtilStream.writeArrayChunk(streamId, dataArray, callback);
184181
}
185182

186183
@ReactMethod
@@ -220,17 +217,17 @@ public void ls(String path, Promise promise) {
220217

221218
@ReactMethod
222219
public void writeStream(String path, String encode, boolean append, Callback callback) {
223-
new ReactNativeBlobUtilFS(this.getReactApplicationContext()).writeStream(path, encode, append, callback);
220+
new ReactNativeBlobUtilStream(this.getReactApplicationContext()).writeStream(path, encode, append, callback);
224221
}
225222

226223
@ReactMethod
227224
public void writeChunk(String streamId, String data, Callback callback) {
228-
ReactNativeBlobUtilFS.writeChunk(streamId, data, callback);
225+
ReactNativeBlobUtilStream.writeChunk(streamId, data, callback);
229226
}
230227

231228
@ReactMethod
232229
public void closeStream(String streamId, Callback callback) {
233-
ReactNativeBlobUtilFS.closeStream(streamId, callback);
230+
ReactNativeBlobUtilStream.closeStream(streamId, callback);
234231
}
235232

236233
@ReactMethod
@@ -323,7 +320,7 @@ public void readStream(final String path, final String encoding, final int buffe
323320
fsThreadPool.execute(new Runnable() {
324321
@Override
325322
public void run() {
326-
ReactNativeBlobUtilFS fs = new ReactNativeBlobUtilFS(ctx);
323+
ReactNativeBlobUtilStream fs = new ReactNativeBlobUtilStream(ctx);
327324
fs.readStream(path, encoding, bufferSize, tick, streamId);
328325
}
329326
});
@@ -396,7 +393,7 @@ public void addCompleteDownload(ReadableMap config, Promise promise) {
396393
promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload config or path missing.");
397394
return;
398395
}
399-
String path = ReactNativeBlobUtilFS.normalizePath(config.getString("path"));
396+
String path = ReactNativeBlobUtilUtils.normalizePath(config.getString("path"));
400397
if (path == null) {
401398
promise.reject("EINVAL", "ReactNativeBlobUtil.addCompleteDownload can not resolve URI:" + config.getString("path"));
402399
return;
@@ -428,4 +425,5 @@ public void getSDCardDir(Promise promise) {
428425
public void getSDCardApplicationDir(Promise promise) {
429426
ReactNativeBlobUtilFS.getSDCardApplicationDir(this.getReactApplicationContext(), promise);
430427
}
428+
431429
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,17 @@ private InputStream getRequestStream() throws Exception {
144144
// upload from storage
145145
if (rawBody.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX)) {
146146
String orgPath = rawBody.substring(ReactNativeBlobUtilConst.FILE_PREFIX.length());
147-
orgPath = ReactNativeBlobUtilFS.normalizePath(orgPath);
147+
orgPath = ReactNativeBlobUtilUtils.normalizePath(orgPath);
148148
// upload file from assets
149-
if (ReactNativeBlobUtilFS.isAsset(orgPath)) {
149+
if (ReactNativeBlobUtilUtils.isAsset(orgPath)) {
150150
try {
151151
String assetName = orgPath.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "");
152152
return ReactNativeBlobUtil.RCTContext.getAssets().open(assetName);
153153
} catch (Exception e) {
154154
throw new Exception("error when getting request stream from asset : " + e.getLocalizedMessage());
155155
}
156156
} else {
157-
File f = new File(ReactNativeBlobUtilFS.normalizePath(orgPath));
157+
File f = new File(ReactNativeBlobUtilUtils.normalizePath(orgPath));
158158
try {
159159
if (!f.exists())
160160
f.createNewFile();
@@ -214,9 +214,9 @@ private File createMultipartBodyCache() throws IOException {
214214
// upload from storage
215215
if (data.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX)) {
216216
String orgPath = data.substring(ReactNativeBlobUtilConst.FILE_PREFIX.length());
217-
orgPath = ReactNativeBlobUtilFS.normalizePath(orgPath);
217+
orgPath = ReactNativeBlobUtilUtils.normalizePath(orgPath);
218218
// path starts with content://
219-
if (ReactNativeBlobUtilFS.isAsset(orgPath)) {
219+
if (ReactNativeBlobUtilUtils.isAsset(orgPath)) {
220220
try {
221221
String assetName = orgPath.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "");
222222
InputStream in = ctx.getAssets().open(assetName);
@@ -227,7 +227,7 @@ private File createMultipartBodyCache() throws IOException {
227227
}
228228
// data from normal files
229229
else {
230-
File file = new File(ReactNativeBlobUtilFS.normalizePath(orgPath));
230+
File file = new File(ReactNativeBlobUtilUtils.normalizePath(orgPath));
231231
if (file.exists()) {
232232
FileInputStream fs = new FileInputStream(file);
233233
pipeStreamToFileStream(fs, os);
@@ -331,9 +331,9 @@ private ArrayList<FormField> countFormDataLength() throws IOException {
331331
// upload from storage
332332
if (data.startsWith(ReactNativeBlobUtilConst.FILE_PREFIX)) {
333333
String orgPath = data.substring(ReactNativeBlobUtilConst.FILE_PREFIX.length());
334-
orgPath = ReactNativeBlobUtilFS.normalizePath(orgPath);
334+
orgPath = ReactNativeBlobUtilUtils.normalizePath(orgPath);
335335
// path starts with asset://
336-
if (ReactNativeBlobUtilFS.isAsset(orgPath)) {
336+
if (ReactNativeBlobUtilUtils.isAsset(orgPath)) {
337337
try {
338338
String assetName = orgPath.replace(ReactNativeBlobUtilConst.FILE_PREFIX_BUNDLE_ASSET, "");
339339
long length = ctx.getAssets().open(assetName).available();
@@ -344,7 +344,7 @@ private ArrayList<FormField> countFormDataLength() throws IOException {
344344
}
345345
// general files
346346
else {
347-
File file = new File(ReactNativeBlobUtilFS.normalizePath(orgPath));
347+
File file = new File(ReactNativeBlobUtilUtils.normalizePath(orgPath));
348348
total += file.length();
349349
}
350350
} else if (data.startsWith(ReactNativeBlobUtilConst.CONTENT_PREFIX)) {

0 commit comments

Comments
 (0)