mirror of
https://github.com/status-im/react-native-fs.git
synced 2026-09-01 12:31:13 +00:00
Merge branch 'feat/promisify' of https://github.com/grabbou/react-native-fs into grabbou-feat/promisify
This commit is contained in:
@@ -1,35 +1,25 @@
|
||||
package com.rnfs;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.os.Environment;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.StatFs;
|
||||
import android.util.Base64;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
import com.facebook.react.bridge.NativeModule;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.Callback;
|
||||
import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
@@ -60,7 +50,7 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void writeFile(String filepath, String base64Content, ReadableMap options, Callback callback) {
|
||||
public void writeFile(String filepath, String base64Content, ReadableMap options, Promise promise) {
|
||||
try {
|
||||
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
|
||||
|
||||
@@ -68,10 +58,10 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
outputStream.write(bytes);
|
||||
outputStream.close();
|
||||
|
||||
callback.invoke(null, true, filepath);
|
||||
promise.resolve(null);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,26 +74,26 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
outputStream.write(bytes);
|
||||
outputStream.close();
|
||||
|
||||
callback.invoke(null, true, filepath);
|
||||
promise.resolve(true);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void exists(String filepath, Callback callback) {
|
||||
public void exists(String filepath, Promise promise) {
|
||||
try {
|
||||
File file = new File(filepath);
|
||||
callback.invoke(null, file.exists());
|
||||
promise.resolve(file.exists());
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void readFile(String filepath, Callback callback) {
|
||||
public void readFile(String filepath, Promise promise) {
|
||||
try {
|
||||
File file = new File(filepath);
|
||||
|
||||
@@ -115,29 +105,29 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
|
||||
String base64Content = Base64.encodeToString(buffer, Base64.NO_WRAP);
|
||||
|
||||
callback.invoke(null, base64Content);
|
||||
promise.resolve(base64Content);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void moveFile(String filepath, String destPath, Callback callback) {
|
||||
public void moveFile(String filepath, String destPath, Promise promise) {
|
||||
try {
|
||||
File from = new File(filepath);
|
||||
File to = new File(destPath);
|
||||
from.renameTo(to);
|
||||
|
||||
callback.invoke(null, true, destPath);
|
||||
promise.resolve(true);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void readDir(String directory, Callback callback) {
|
||||
public void readDir(String directory, Promise promise) {
|
||||
try {
|
||||
File file = new File(directory);
|
||||
|
||||
@@ -158,16 +148,15 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
fileMaps.pushMap(fileMap);
|
||||
}
|
||||
|
||||
callback.invoke(null, fileMaps);
|
||||
|
||||
promise.resolve(fileMaps);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void stat(String filepath, Callback callback) {
|
||||
public void stat(String filepath, Promise promise) {
|
||||
try {
|
||||
File file = new File(filepath);
|
||||
|
||||
@@ -180,15 +169,15 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
statMap.putInt("size", (int)file.length());
|
||||
statMap.putInt("type", file.isDirectory() ? 1 : 0);
|
||||
|
||||
callback.invoke(null, statMap);
|
||||
promise.resolve(statMap);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void unlink(String filepath, Callback callback) {
|
||||
public void unlink(String filepath, Promise promise) {
|
||||
try {
|
||||
File file = new File(filepath);
|
||||
|
||||
@@ -196,10 +185,10 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
|
||||
boolean success = DeleteRecursive(file);
|
||||
|
||||
callback.invoke(null, success, filepath);
|
||||
promise.resolve(success);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,7 +203,7 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void mkdir(String filepath, Boolean excludeFromBackup, Callback callback) {
|
||||
public void mkdir(String filepath, Boolean excludeFromBackup, Promise promise) {
|
||||
try {
|
||||
File file = new File(filepath);
|
||||
|
||||
@@ -222,10 +211,10 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
|
||||
boolean success = file.exists();
|
||||
|
||||
callback.invoke(null, success, filepath);
|
||||
promise.resolve(success);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,7 +225,7 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void downloadFile(ReadableMap options, final Callback callback) {
|
||||
public void downloadFile(ReadableMap options, final Promise promise) {
|
||||
try {
|
||||
File file = new File(options.getString("toFile"));
|
||||
URL url = new URL(options.getString("fromUrl"));
|
||||
@@ -260,9 +249,9 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
infoMap.putInt("statusCode", res.statusCode);
|
||||
infoMap.putInt("bytesWritten", res.bytesWritten);
|
||||
|
||||
callback.invoke(null, infoMap);
|
||||
promise.resolve(infoMap);
|
||||
} else {
|
||||
callback.invoke(makeErrorPayload(res.exception));
|
||||
reject(promise, res.exception);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -305,7 +294,7 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
this.downloaders.put(jobId, downloader);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
callback.invoke(makeErrorPayload(ex));
|
||||
reject(promise, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,12 +308,12 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void pathForBundle(String bundleNamed, Callback callback) {
|
||||
public void pathForBundle(String bundleNamed, Promise promise) {
|
||||
// TODO: Not sure what equilivent would be?
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void getFSInfo(Callback callback) {
|
||||
public void getFSInfo(Promise promise) {
|
||||
File path = Environment.getDataDirectory();
|
||||
StatFs stat = new StatFs(path.getPath());
|
||||
long totalSpace;
|
||||
@@ -340,13 +329,20 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
WritableMap info = Arguments.createMap();
|
||||
info.putDouble("totalSpace", (double)totalSpace); // Int32 too small, must use Double
|
||||
info.putDouble("freeSpace", (double)freeSpace);
|
||||
callback.invoke(null, info);
|
||||
promise.resolve(info);
|
||||
}
|
||||
|
||||
private WritableMap makeErrorPayload(Exception ex) {
|
||||
WritableMap error = Arguments.createMap();
|
||||
error.putString("message", ex.getMessage());
|
||||
return error;
|
||||
private void reject(Promise promise, String filepath, Exception ex) {
|
||||
if (ex instanceof FileNotFoundException) {
|
||||
rejectFileNotFound(promise, filepath);
|
||||
return;
|
||||
}
|
||||
|
||||
promise.reject(null, ex.getMessage());
|
||||
}
|
||||
|
||||
private void rejectFileNotFound(Promise promise, String filepath) {
|
||||
promise.reject("ENOENT", "ENOENT: no such file or directory, open '" + filepath + "'");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -357,9 +353,9 @@ public class RNFSManager extends ReactContextBaseJavaModule {
|
||||
constants.put(NSTemporaryDirectoryPath, null);
|
||||
File externalDirectory = this.getReactApplicationContext().getExternalFilesDir(null);
|
||||
if (externalDirectory != null) {
|
||||
constants.put(NSExternalDirectoryPath, externalDirectory.getAbsolutePath());
|
||||
constants.put(NSExternalDirectoryPath, externalDirectory.getAbsolutePath());
|
||||
} else {
|
||||
constants.put(NSExternalDirectoryPath, null);
|
||||
constants.put(NSExternalDirectoryPath, null);
|
||||
}
|
||||
constants.put(NSPicturesDirectoryPath, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
|
||||
constants.put(NSCachesDirectoryPath, this.getReactApplicationContext().getCacheDir().getAbsolutePath());
|
||||
|
||||
Reference in New Issue
Block a user