正在查看: Pulsar v1.0.0 应用的 RNFSManager.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: Pulsar v1.0.0 应用的 RNFSManager.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.rnfs;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.util.Base64;
import android.util.SparseArray;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
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.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.RCTNativeAppEventEmitter;
import com.rnfs.b;
import com.rnfs.i;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@ReactModule(name = RNFSManager.MODULE_NAME)
public class RNFSManager extends ReactContextBaseJavaModule {
static final String MODULE_NAME = "RNFSManager";
private static final String RNFSCachesDirectoryPath = "RNFSCachesDirectoryPath";
private static final String RNFSDocumentDirectory = "RNFSDocumentDirectory";
private static final String RNFSDocumentDirectoryPath = "RNFSDocumentDirectoryPath";
private static final String RNFSDownloadDirectoryPath = "RNFSDownloadDirectoryPath";
private static final String RNFSExternalCachesDirectoryPath = "RNFSExternalCachesDirectoryPath";
private static final String RNFSExternalDirectoryPath = "RNFSExternalDirectoryPath";
private static final String RNFSExternalStorageDirectoryPath = "RNFSExternalStorageDirectoryPath";
private static final String RNFSFileTypeDirectory = "RNFSFileTypeDirectory";
private static final String RNFSFileTypeRegular = "RNFSFileTypeRegular";
private static final String RNFSPicturesDirectoryPath = "RNFSPicturesDirectoryPath";
private static final String RNFSTemporaryDirectoryPath = "RNFSTemporaryDirectoryPath";
private SparseArray<com.rnfs.e> downloaders;
private ReactApplicationContext reactContext;
private SparseArray<k> uploaders;
class a extends j {
final File b;
final Promise c;
final String d;
a(File file, Promise promise, String str) {
super();
this.b = file;
this.c = promise;
this.d = str;
}
@Override
public void onPostExecute(Exception exc) {
if (exc == null) {
this.b.delete();
this.c.resolve(Boolean.TRUE);
} else {
exc.printStackTrace();
RNFSManager.this.reject(this.c, this.d, exc);
}
}
}
class b extends j {
final Promise b;
final String c;
b(Promise promise, String str) {
super();
this.b = promise;
this.c = str;
}
@Override
public void onPostExecute(Exception exc) {
if (exc == null) {
this.b.resolve((Object) null);
} else {
exc.printStackTrace();
RNFSManager.this.reject(this.b, this.c, exc);
}
}
}
class c implements b.c {
final int a;
final Promise b;
final ReadableMap c;
c(int i, Promise promise, ReadableMap readableMap) {
this.a = i;
this.b = promise;
this.c = readableMap;
}
@Override
public void onTaskCompleted(com.rnfs.c cVar) {
if (cVar.c != null) {
RNFSManager.this.reject(this.b, this.c.getString("toFile"), cVar.c);
return;
}
WritableMap createMap = Arguments.createMap();
createMap.putInt("jobId", this.a);
createMap.putInt("statusCode", cVar.a);
createMap.putDouble("bytesWritten", cVar.b);
this.b.resolve(createMap);
}
}
class d implements b.a {
final int a;
d(int i) {
this.a = i;
}
@Override
public void onDownloadBegin(int i, long j, Map<String, String> map) {
WritableMap createMap = Arguments.createMap();
for (Map.Entry<String, String> entry : map.entrySet()) {
createMap.putString(entry.getKey(), entry.getValue());
}
WritableMap createMap2 = Arguments.createMap();
createMap2.putInt("jobId", this.a);
createMap2.putInt("statusCode", i);
createMap2.putDouble("contentLength", j);
createMap2.putMap("headers", createMap);
RNFSManager rNFSManager = RNFSManager.this;
rNFSManager.sendEvent(rNFSManager.getReactApplicationContext(), "DownloadBegin", createMap2);
}
}
class e implements b.InterfaceC0091b {
final int a;
e(int i) {
this.a = i;
}
@Override
public void onDownloadProgress(long j, long j2) {
WritableMap createMap = Arguments.createMap();
createMap.putInt("jobId", this.a);
createMap.putDouble("contentLength", j);
createMap.putDouble("bytesWritten", j2);
RNFSManager rNFSManager = RNFSManager.this;
rNFSManager.sendEvent(rNFSManager.getReactApplicationContext(), "DownloadProgress", createMap);
}
}
class f implements i.b {
final int a;
final Promise b;
final ReadableMap c;
f(int i, Promise promise, ReadableMap readableMap) {
this.a = i;
this.b = promise;
this.c = readableMap;
}
@Override
public void onUploadComplete(com.rnfs.j jVar) {
if (jVar.c != null) {
RNFSManager.this.reject(this.b, this.c.getString("toUrl"), jVar.c);
return;
}
WritableMap createMap = Arguments.createMap();
createMap.putInt("jobId", this.a);
createMap.putInt("statusCode", jVar.a);
createMap.putMap("headers", jVar.b);
createMap.putString("body", jVar.d);
this.b.resolve(createMap);
}
}
class g implements i.a {
final int a;
g(int i) {
this.a = i;
}
@Override
public void onUploadBegin() {
WritableMap createMap = Arguments.createMap();
createMap.putInt("jobId", this.a);
RNFSManager rNFSManager = RNFSManager.this;
rNFSManager.sendEvent(rNFSManager.getReactApplicationContext(), "UploadBegin", createMap);
}
}
class h implements i.c {
final int a;
h(int i) {
this.a = i;
}
@Override
public void onUploadProgress(int i, int i2) {
WritableMap createMap = Arguments.createMap();
createMap.putInt("jobId", this.a);
createMap.putInt("totalBytesExpectedToSend", i);
createMap.putInt("totalBytesSent", i2);
RNFSManager rNFSManager = RNFSManager.this;
rNFSManager.sendEvent(rNFSManager.getReactApplicationContext(), "UploadProgress", createMap);
}
}
class i implements MediaScannerConnection.MediaScannerConnectionClient {
final Promise a;
i(Promise promise) {
this.a = promise;
}
@Override
public void onMediaScannerConnected() {
}
@Override
public void onScanCompleted(String str, Uri uri) {
this.a.resolve(str);
}
}
private class j extends AsyncTask<String, Void, Exception> {
private j() {
}
@Override
public Exception doInBackground(String... strArr) {
try {
String str = strArr[0];
String str2 = strArr[1];
InputStream inputStream = RNFSManager.this.getInputStream(str);
OutputStream outputStream = RNFSManager.this.getOutputStream(str2, false);
byte[] bArr = new byte[1024];
while (true) {
int read = inputStream.read(bArr);
if (read <= 0) {
inputStream.close();
outputStream.close();
return null;
}
outputStream.write(bArr, 0, read);
Thread.yield();
}
} catch (Exception e) {
return e;
}
}
}
public RNFSManager(ReactApplicationContext reactApplicationContext) {
super(reactApplicationContext);
this.downloaders = new SparseArray<>();
this.uploaders = new SparseArray<>();
this.reactContext = reactApplicationContext;
}
private void DeleteRecursive(File file) {
if (file.isDirectory()) {
for (File file2 : file.listFiles()) {
DeleteRecursive(file2);
}
}
file.delete();
}
private void copyInputStream(java.io.InputStream r8, java.lang.String r9, java.lang.String r10, com.facebook.react.bridge.Promise r11) {
throw new UnsupportedOperationException("Method not decompiled: com.rnfs.RNFSManager.copyInputStream(java.io.InputStream, java.lang.String, java.lang.String, com.facebook.react.bridge.Promise):void");
}
private Uri getFileUri(String str, boolean z) throws com.rnfs.f {
Uri parse = Uri.parse(str);
if (parse.getScheme() != null) {
return parse;
}
File file = new File(str);
if (z || !file.isDirectory()) {
return Uri.parse("file://" + str);
}
throw new com.rnfs.f("EISDIR", "EISDIR: illegal operation on a directory, read '" + str + "'");
}
public InputStream getInputStream(String str) throws com.rnfs.f {
try {
InputStream openInputStream = this.reactContext.getContentResolver().openInputStream(getFileUri(str, false));
if (openInputStream != null) {
return openInputStream;
}
throw new com.rnfs.f("ENOENT", "ENOENT: could not open an input stream for '" + str + "'");
} catch (FileNotFoundException e2) {
throw new com.rnfs.f("ENOENT", "ENOENT: " + e2.getMessage() + ", open '" + str + "'");
}
}
private static byte[] getInputStreamBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bArr = new byte[1024];
while (true) {
try {
int read = inputStream.read(bArr);
if (read == -1) {
break;
}
byteArrayOutputStream.write(bArr, 0, read);
} catch (Throwable th) {
try {
byteArrayOutputStream.close();
} catch (IOException unused) {
}
throw th;
}
}
byte[] byteArray = byteArrayOutputStream.toByteArray();
try {
byteArrayOutputStream.close();
} catch (IOException unused2) {
}
return byteArray;
}
private String getOriginalFilepath(String str, boolean z) throws com.rnfs.f {
Uri fileUri = getFileUri(str, z);
if (fileUri.getScheme().equals(v6.a.d)) {
try {
Cursor query = this.reactContext.getContentResolver().query(fileUri, null, null, null, null);
if (query.moveToFirst()) {
str = query.getString(query.getColumnIndexOrThrow("_data"));
}
query.close();
} catch (IllegalArgumentException unused) {
}
}
return str;
}
public OutputStream getOutputStream(String str, boolean z) throws com.rnfs.f {
try {
OutputStream openOutputStream = this.reactContext.getContentResolver().openOutputStream(getFileUri(str, false), z ? "wa" : getWriteAccessByAPILevel());
if (openOutputStream != null) {
return openOutputStream;
}
throw new com.rnfs.f("ENOENT", "ENOENT: could not open an output stream for '" + str + "'");
} catch (FileNotFoundException e2) {
throw new com.rnfs.f("ENOENT", "ENOENT: " + e2.getMessage() + ", open '" + str + "'");
}
}
private int getResIdentifier(String str) {
boolean z = true;
String substring = str.substring(str.lastIndexOf(".") + 1);
String substring2 = str.substring(0, str.lastIndexOf("."));
if (!substring.equals("png") && !substring.equals("jpg") && !substring.equals("jpeg") && !substring.equals("bmp") && !substring.equals("gif") && !substring.equals("webp") && !substring.equals("psd") && !substring.equals("svg") && !substring.equals("tiff")) {
z = false;
}
return getReactApplicationContext().getResources().getIdentifier(substring2, Boolean.valueOf(z).booleanValue() ? "drawable" : "raw", getReactApplicationContext().getPackageName());
}
private String getWriteAccessByAPILevel() {
return Build.VERSION.SDK_INT <= 28 ? "w" : "rwt";
}
public void reject(Promise promise, String str, Exception exc) {
String str2;
Exception exc2;
if (exc instanceof FileNotFoundException) {
rejectFileNotFound(promise, str);
return;
}
if (exc instanceof com.rnfs.f) {
com.rnfs.f fVar = (com.rnfs.f) exc;
str2 = fVar.getCode();
exc2 = fVar;
} else {
str2 = null;
exc2 = exc;
}
promise.reject(str2, exc2.getMessage());
}
private void rejectFileIsDirectory(Promise promise) {
promise.reject("EISDIR", "EISDIR: illegal operation on a directory, read");
}
private void rejectFileNotFound(Promise promise, String str) {
promise.reject("ENOENT", "ENOENT: no such file or directory, open '" + str + "'");
}
public void sendEvent(ReactContext reactContext, String str, WritableMap writableMap) {
reactContext.getJSModule(RCTNativeAppEventEmitter.class).emit(str, writableMap);
}
@ReactMethod
public void addListener(String str) {
}
@ReactMethod
public void appendFile(String str, String str2, Promise promise) {
try {
byte[] decode = Base64.decode(str2, 0);
OutputStream outputStream = getOutputStream(str, true);
outputStream.write(decode);
outputStream.close();
promise.resolve((Object) null);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void copyFile(String str, String str2, ReadableMap readableMap, Promise promise) {
new b(promise, str).execute(str, str2);
}
@ReactMethod
public void copyFileAssets(String str, String str2, Promise promise) {
try {
copyInputStream(getReactApplicationContext().getAssets().open(str), str, str2, promise);
} catch (IOException unused) {
reject(promise, str, new Exception(String.format("Asset '%s' could not be opened", str)));
}
}
@ReactMethod
public void copyFileRes(String str, String str2, Promise promise) {
try {
copyInputStream(getReactApplicationContext().getResources().openRawResource(getResIdentifier(str)), str, str2, promise);
} catch (Exception unused) {
reject(promise, str, new Exception(String.format("Res '%s' could not be opened", str)));
}
}
@ReactMethod
public void downloadFile(ReadableMap readableMap, Promise promise) {
try {
File file = new File(readableMap.getString("toFile"));
URL url = new URL(readableMap.getString("fromUrl"));
int i2 = readableMap.getInt("jobId");
ReadableMap map = readableMap.getMap("headers");
int i3 = readableMap.getInt("progressInterval");
int i4 = readableMap.getInt("progressDivider");
int i8 = readableMap.getInt("readTimeout");
int i9 = readableMap.getInt("connectionTimeout");
boolean z = readableMap.getBoolean("hasBeginCallback");
boolean z2 = readableMap.getBoolean("hasProgressCallback");
com.rnfs.b bVar = new com.rnfs.b();
bVar.a = url;
bVar.b = file;
bVar.c = map;
bVar.d = i3;
bVar.e = i4;
bVar.f = i8;
bVar.g = i9;
bVar.h = new c(i2, promise, readableMap);
if (z) {
bVar.i = new d(i2);
}
if (z2) {
bVar.j = new e(i2);
}
com.rnfs.e eVar = new com.rnfs.e();
eVar.execute(bVar);
this.downloaders.put(i2, eVar);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, readableMap.getString("toFile"), e2);
}
}
@ReactMethod
public void exists(String str, Promise promise) {
try {
promise.resolve(Boolean.valueOf(new File(str).exists()));
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void existsAssets(String str, Promise promise) {
try {
AssetManager assets = getReactApplicationContext().getAssets();
try {
String[] list = assets.list(str);
if (list != null && list.length > 0) {
promise.resolve(Boolean.TRUE);
return;
}
} catch (Exception unused) {
}
InputStream inputStream = null;
try {
try {
inputStream = assets.open(str);
promise.resolve(Boolean.TRUE);
if (inputStream == null) {
return;
}
} catch (Exception unused2) {
promise.resolve(Boolean.FALSE);
if (inputStream == null) {
return;
}
}
try {
inputStream.close();
} catch (Exception unused3) {
}
} catch (Throwable th) {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception unused4) {
}
}
throw th;
}
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void existsRes(String str, Promise promise) {
try {
promise.resolve(getResIdentifier(str) > 0 ? Boolean.TRUE : Boolean.FALSE);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void getAllExternalFilesDirs(Promise promise) {
File[] externalFilesDirs = getReactApplicationContext().getExternalFilesDirs(null);
WritableArray createArray = Arguments.createArray();
for (File file : externalFilesDirs) {
if (file != null) {
createArray.pushString(file.getAbsolutePath());
}
}
promise.resolve(createArray);
}
public Map<String, Object> getConstants() {
HashMap hashMap = new HashMap();
hashMap.put(RNFSDocumentDirectory, 0);
hashMap.put(RNFSDocumentDirectoryPath, getReactApplicationContext().getFilesDir().getAbsolutePath());
hashMap.put(RNFSTemporaryDirectoryPath, getReactApplicationContext().getCacheDir().getAbsolutePath());
hashMap.put(RNFSPicturesDirectoryPath, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
hashMap.put(RNFSCachesDirectoryPath, getReactApplicationContext().getCacheDir().getAbsolutePath());
hashMap.put(RNFSDownloadDirectoryPath, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
hashMap.put(RNFSFileTypeRegular, 0);
hashMap.put(RNFSFileTypeDirectory, 1);
File externalStorageDirectory = Environment.getExternalStorageDirectory();
if (externalStorageDirectory != null) {
hashMap.put(RNFSExternalStorageDirectoryPath, externalStorageDirectory.getAbsolutePath());
} else {
hashMap.put(RNFSExternalStorageDirectoryPath, null);
}
File externalFilesDir = getReactApplicationContext().getExternalFilesDir(null);
if (externalFilesDir != null) {
hashMap.put(RNFSExternalDirectoryPath, externalFilesDir.getAbsolutePath());
} else {
hashMap.put(RNFSExternalDirectoryPath, null);
}
File externalCacheDir = getReactApplicationContext().getExternalCacheDir();
if (externalCacheDir != null) {
hashMap.put(RNFSExternalCachesDirectoryPath, externalCacheDir.getAbsolutePath());
} else {
hashMap.put(RNFSExternalCachesDirectoryPath, null);
}
return hashMap;
}
@ReactMethod
public void getFSInfo(Promise promise) {
StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
StatFs statFs2 = new StatFs(Environment.getExternalStorageDirectory().getPath());
long totalBytes = statFs.getTotalBytes();
long freeBytes = statFs.getFreeBytes();
long totalBytes2 = statFs2.getTotalBytes();
long freeBytes2 = statFs2.getFreeBytes();
WritableMap createMap = Arguments.createMap();
createMap.putDouble("totalSpace", totalBytes);
createMap.putDouble("freeSpace", freeBytes);
createMap.putDouble("totalSpaceEx", totalBytes2);
createMap.putDouble("freeSpaceEx", freeBytes2);
promise.resolve(createMap);
}
public String getName() {
return MODULE_NAME;
}
@ReactMethod
public void hash(String str, String str2, Promise promise) {
try {
HashMap hashMap = new HashMap();
hashMap.put("md5", "MD5");
hashMap.put("sha1", "SHA-1");
hashMap.put("sha224", "SHA-224");
hashMap.put("sha256", "SHA-256");
hashMap.put("sha384", "SHA-384");
hashMap.put("sha512", "SHA-512");
if (!hashMap.containsKey(str2)) {
throw new Exception("Invalid hash algorithm");
}
File file = new File(str);
if (file.isDirectory()) {
rejectFileIsDirectory(promise);
return;
}
if (!file.exists()) {
rejectFileNotFound(promise, str);
return;
}
MessageDigest messageDigest = MessageDigest.getInstance((String) hashMap.get(str2));
FileInputStream fileInputStream = new FileInputStream(str);
byte[] bArr = new byte[10240];
while (true) {
int read = fileInputStream.read(bArr);
if (read == -1) {
break;
} else {
messageDigest.update(bArr, 0, read);
}
}
StringBuilder sb = new StringBuilder();
for (byte b2 : messageDigest.digest()) {
sb.append(String.format("%02x", Byte.valueOf(b2)));
}
promise.resolve(sb.toString());
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void mkdir(String str, ReadableMap readableMap, Promise promise) {
try {
File file = new File(str);
file.mkdirs();
if (!file.exists()) {
throw new Exception("Directory could not be created");
}
promise.resolve((Object) null);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void moveFile(String str, String str2, ReadableMap readableMap, Promise promise) {
try {
File file = new File(str);
if (file.renameTo(new File(str2))) {
promise.resolve(Boolean.TRUE);
} else {
new a(file, promise, str).execute(str, str2);
}
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void pathForBundle(String str, Promise promise) {
}
@ReactMethod
public void pathForGroup(String str, Promise promise) {
}
@ReactMethod
public void read(String str, int i2, int i3, Promise promise) {
try {
InputStream inputStream = getInputStream(str);
byte[] bArr = new byte[i2];
inputStream.skip(i3);
promise.resolve(Base64.encodeToString(bArr, 0, inputStream.read(bArr, 0, i2), 2));
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void readDir(String str, Promise promise) {
try {
File file = new File(str);
if (!file.exists()) {
throw new Exception("Folder does not exist");
}
File[] listFiles = file.listFiles();
WritableArray createArray = Arguments.createArray();
for (File file2 : listFiles) {
WritableMap createMap = Arguments.createMap();
createMap.putDouble("mtime", file2.lastModified() / 1000.0d);
createMap.putString("name", file2.getName());
createMap.putString("path", file2.getAbsolutePath());
createMap.putDouble("size", file2.length());
createMap.putInt("type", file2.isDirectory() ? 1 : 0);
createArray.pushMap(createMap);
}
promise.resolve(createArray);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void readDirAssets(String str, Promise promise) {
int i2;
try {
AssetManager assets = getReactApplicationContext().getAssets();
String[] list = assets.list(str);
WritableArray createArray = Arguments.createArray();
for (String str2 : list) {
WritableMap createMap = Arguments.createMap();
createMap.putString("name", str2);
int i3 = 1;
if (!str.isEmpty()) {
str2 = String.format("%s/%s", str, str2);
}
createMap.putString("path", str2);
try {
AssetFileDescriptor openFd = assets.openFd(str2);
if (openFd != null) {
i2 = (int) openFd.getLength();
try {
openFd.close();
i3 = 0;
} catch (IOException e2) {
e = e2;
i3 = 1 ^ (e.getMessage().contains("compressed") ? 1 : 0);
createMap.putInt("size", i2);
createMap.putInt("type", i3);
createArray.pushMap(createMap);
}
} else {
i2 = 0;
}
} catch (IOException e3) {
e = e3;
i2 = 0;
}
createMap.putInt("size", i2);
createMap.putInt("type", i3);
createArray.pushMap(createMap);
}
promise.resolve(createArray);
} catch (IOException e4) {
reject(promise, str, e4);
}
}
@ReactMethod
public void readFile(String str, Promise promise) {
try {
promise.resolve(Base64.encodeToString(getInputStreamBytes(getInputStream(str)), 2));
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void readFileAssets(String str, Promise promise) {
InputStream inputStream = null;
try {
try {
inputStream = getReactApplicationContext().getAssets().open(str, 0);
} catch (Throwable th) {
if (0 != 0) {
try {
inputStream.close();
} catch (IOException unused) {
}
}
throw th;
}
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
if (0 == 0) {
return;
}
}
if (inputStream == null) {
reject(promise, str, new Exception("Failed to open file"));
if (inputStream != null) {
try {
inputStream.close();
return;
} catch (IOException unused2) {
return;
}
}
return;
}
byte[] bArr = new byte[inputStream.available()];
inputStream.read(bArr);
promise.resolve(Base64.encodeToString(bArr, 2));
try {
inputStream.close();
} catch (IOException unused3) {
}
}
@ReactMethod
public void readFileRes(String str, Promise promise) {
InputStream inputStream = null;
try {
try {
inputStream = getReactApplicationContext().getResources().openRawResource(getResIdentifier(str));
} catch (Throwable th) {
if (0 != 0) {
try {
inputStream.close();
} catch (IOException unused) {
}
}
throw th;
}
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
if (0 == 0) {
return;
}
}
if (inputStream == null) {
reject(promise, str, new Exception("Failed to open file"));
if (inputStream != null) {
try {
inputStream.close();
return;
} catch (IOException unused2) {
return;
}
}
return;
}
byte[] bArr = new byte[inputStream.available()];
inputStream.read(bArr);
promise.resolve(Base64.encodeToString(bArr, 2));
try {
inputStream.close();
} catch (IOException unused3) {
}
}
@ReactMethod
public void removeListeners(Integer num) {
}
@ReactMethod
public void scanFile(String str, Promise promise) {
MediaScannerConnection.scanFile(getReactApplicationContext(), new String[]{str}, null, new i(promise));
}
@ReactMethod
public void setReadable(String str, Boolean bool, Boolean bool2, Promise promise) {
try {
File file = new File(str);
if (!file.exists()) {
throw new Exception("File does not exist");
}
file.setReadable(bool.booleanValue(), bool2.booleanValue());
promise.resolve(Boolean.TRUE);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void stat(String str, Promise promise) {
try {
String originalFilepath = getOriginalFilepath(str, true);
File file = new File(originalFilepath);
if (!file.exists()) {
throw new Exception("File does not exist");
}
WritableMap createMap = Arguments.createMap();
createMap.putInt("ctime", (int) (file.lastModified() / 1000));
createMap.putInt("mtime", (int) (file.lastModified() / 1000));
createMap.putDouble("size", file.length());
createMap.putInt("type", file.isDirectory() ? 1 : 0);
createMap.putString("originalFilepath", originalFilepath);
promise.resolve(createMap);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void stopDownload(int i2) {
com.rnfs.e eVar = this.downloaders.get(i2);
if (eVar != null) {
eVar.h();
}
}
@ReactMethod
public void stopUpload(int i2) {
k kVar = this.uploaders.get(i2);
if (kVar != null) {
kVar.f();
}
}
@ReactMethod
public void touch(String str, double d2, double d3, Promise promise) {
try {
promise.resolve(Boolean.valueOf(new File(str).setLastModified((long) d2)));
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void unlink(String str, Promise promise) {
try {
File file = new File(str);
if (!file.exists()) {
throw new Exception("File does not exist");
}
DeleteRecursive(file);
promise.resolve((Object) null);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void uploadFiles(ReadableMap readableMap, Promise promise) {
String str;
try {
ReadableArray array = readableMap.getArray("files");
URL url = new URL(readableMap.getString("toUrl"));
int i2 = readableMap.getInt("jobId");
ReadableMap map = readableMap.getMap("headers");
ReadableMap map2 = readableMap.getMap("fields");
String string = readableMap.getString("method");
boolean z = readableMap.getBoolean("binaryStreamOnly");
boolean z2 = readableMap.getBoolean("hasBeginCallback");
boolean z3 = readableMap.getBoolean("hasProgressCallback");
ArrayList<ReadableMap> arrayList = new ArrayList<>();
com.rnfs.i iVar = new com.rnfs.i();
str = "toUrl";
for (int i3 = 0; i3 < array.size(); i3++) {
try {
arrayList.add(array.getMap(i3));
} catch (Exception e2) {
e = e2;
e.printStackTrace();
reject(promise, readableMap.getString(str), e);
return;
}
}
iVar.a = url;
iVar.b = arrayList;
iVar.e = map;
iVar.g = string;
iVar.f = map2;
iVar.c = z;
iVar.h = new f(i2, promise, readableMap);
if (z2) {
iVar.j = new g(i2);
}
if (z3) {
iVar.i = new h(i2);
}
k kVar = new k();
kVar.execute(iVar);
this.uploaders.put(i2, kVar);
} catch (Exception e3) {
e = e3;
str = "toUrl";
}
}
@ReactMethod
public void write(String str, String str2, int i2, Promise promise) {
try {
byte[] decode = Base64.decode(str2, 0);
if (i2 < 0) {
OutputStream outputStream = getOutputStream(str, true);
outputStream.write(decode);
outputStream.close();
} else {
RandomAccessFile randomAccessFile = new RandomAccessFile(str, "rw");
randomAccessFile.seek(i2);
randomAccessFile.write(decode);
randomAccessFile.close();
}
promise.resolve((Object) null);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
@ReactMethod
public void writeFile(String str, String str2, ReadableMap readableMap, Promise promise) {
try {
byte[] decode = Base64.decode(str2, 0);
OutputStream outputStream = getOutputStream(str, false);
outputStream.write(decode);
outputStream.close();
promise.resolve((Object) null);
} catch (Exception e2) {
e2.printStackTrace();
reject(promise, str, e2);
}
}
}