导航菜单

页面标题

页面副标题

Housing v14.7.8 - RNFSManager.java 源代码

正在查看: Housing v14.7.8 应用的 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 androidx.camera.camera2.internal.t2;
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.newrelic.agent.android.api.v2.TraceFieldInterface;
import com.newrelic.agent.android.instrumentation.AsyncTaskInstrumentation;
import com.newrelic.agent.android.instrumentation.Instrumented;
import com.newrelic.agent.android.tracing.Trace;
import com.newrelic.agent.android.tracing.TraceMachine;
import com.quickblox.core.QBErrors;
import com.rnfs.a;
import com.rnfs.g;
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;
import org.jivesoftware.smack.compress.packet.Compressed;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;

@ReactModule(name = RNFSManager.MODULE_NAME)
@Instrumented
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.d> downloaders;
    private ReactApplicationContext reactContext;
    private SparseArray<com.rnfs.j> uploaders;

    public class a extends j {
        final String val$filepath;
        final File val$inFile;
        final Promise val$promise;

        public a(File file, Promise promise, String str) {
            super();
            this.val$inFile = file;
            this.val$promise = promise;
            this.val$filepath = str;
        }

        @Override
        public final void onPostExecute(Object obj) {
            Exception exc = (Exception) obj;
            if (exc == null) {
                this.val$inFile.delete();
                this.val$promise.resolve(Boolean.TRUE);
            } else {
                exc.printStackTrace();
                RNFSManager.this.reject(this.val$promise, this.val$filepath, exc);
            }
        }
    }

    public class b extends j {
        final String val$filepath;
        final Promise val$promise;

        public b(Promise promise, String str) {
            super();
            this.val$promise = promise;
            this.val$filepath = str;
        }

        @Override
        public final void onPostExecute(Object obj) {
            Exception exc = (Exception) obj;
            if (exc == null) {
                this.val$promise.resolve((Object) null);
            } else {
                exc.printStackTrace();
                RNFSManager.this.reject(this.val$promise, this.val$filepath, exc);
            }
        }
    }

    public class c implements a.c {
        final int val$jobId;
        final ReadableMap val$options;
        final Promise val$promise;

        public c(int i, Promise promise, ReadableMap readableMap) {
            this.val$jobId = i;
            this.val$promise = promise;
            this.val$options = readableMap;
        }

        public final void a(com.rnfs.b bVar) {
            if (bVar.exception != null) {
                RNFSManager.this.reject(this.val$promise, this.val$options.getString("toFile"), bVar.exception);
                return;
            }
            WritableMap createMap = Arguments.createMap();
            createMap.putInt("jobId", this.val$jobId);
            createMap.putInt("statusCode", bVar.statusCode);
            createMap.putDouble("bytesWritten", bVar.bytesWritten);
            this.val$promise.resolve(createMap);
        }
    }

    public class d implements a.InterfaceC0010a {
        final int val$jobId;

        public d(int i) {
            this.val$jobId = i;
        }

        public final void a(int i, long j, HashMap hashMap) {
            WritableMap createMap = Arguments.createMap();
            for (Map.Entry entry : hashMap.entrySet()) {
                createMap.putString((String) entry.getKey(), (String) entry.getValue());
            }
            WritableMap createMap2 = Arguments.createMap();
            createMap2.putInt("jobId", this.val$jobId);
            createMap2.putInt("statusCode", i);
            createMap2.putDouble("contentLength", j);
            createMap2.putMap("headers", createMap);
            RNFSManager rNFSManager = RNFSManager.this;
            rNFSManager.sendEvent(rNFSManager.getReactApplicationContext(), "DownloadBegin", createMap2);
        }
    }

    public class e implements a.b {
        final int val$jobId;

        public e(int i) {
            this.val$jobId = i;
        }
    }

    public class f implements g.b {
        final int val$jobId;
        final ReadableMap val$options;
        final Promise val$promise;

        public f(int i, Promise promise, ReadableMap readableMap) {
            this.val$jobId = i;
            this.val$promise = promise;
            this.val$options = readableMap;
        }

        public final void a(com.rnfs.h hVar) {
            if (hVar.exception != null) {
                RNFSManager.this.reject(this.val$promise, this.val$options.getString("toUrl"), hVar.exception);
                return;
            }
            WritableMap createMap = Arguments.createMap();
            createMap.putInt("jobId", this.val$jobId);
            createMap.putInt("statusCode", hVar.statusCode);
            createMap.putMap("headers", hVar.headers);
            createMap.putString(Message.BODY, hVar.body);
            this.val$promise.resolve(createMap);
        }
    }

    public class g implements g.a {
        final int val$jobId;

        public g(int i) {
            this.val$jobId = i;
        }
    }

    public class h implements g.c {
        final int val$jobId;

        public h(int i) {
            this.val$jobId = i;
        }
    }

    public class i implements MediaScannerConnection.MediaScannerConnectionClient {
        final Promise val$promise;

        public i(Promise promise) {
            this.val$promise = promise;
        }

        @Override
        public final void onMediaScannerConnected() {
        }

        @Override
        public final void onScanCompleted(String str, Uri uri) {
            this.val$promise.resolve(str);
        }
    }

    @Instrumented
    public class j extends AsyncTask implements TraceFieldInterface {
        public Trace _nr_trace;

        public j() {
        }

        public final void _nr_setTrace(Trace trace) {
            try {
                this._nr_trace = trace;
            } catch (Exception unused) {
            }
        }

        @Override
        public final Object doInBackground(Object[] objArr) {
            Exception exc = null;
            try {
                TraceMachine.enterMethod(this._nr_trace, "RNFSManager$CopyFileTask#doInBackground", (ArrayList) null);
            } catch (NoSuchFieldError unused) {
                TraceMachine.enterMethod((Trace) null, "RNFSManager$CopyFileTask#doInBackground", (ArrayList) null);
            }
            String[] strArr = (String[]) objArr;
            try {
                String str = strArr[0];
                String str2 = strArr[1];
                InputStream inputStream = RNFSManager.this.getInputStream(str);
                OutputStream b = RNFSManager.b(RNFSManager.this, str2);
                byte[] bArr = new byte[1024];
                while (true) {
                    int read = inputStream.read(bArr);
                    if (read <= 0) {
                        break;
                    }
                    b.write(bArr, 0, read);
                    Thread.yield();
                }
                inputStream.close();
                b.close();
            } catch (Exception e) {
                exc = e;
            }
            TraceMachine.exitMethod();
            return exc;
        }
    }

    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();
    }

    public static OutputStream b(RNFSManager rNFSManager, String str) {
        return rNFSManager.getOutputStream(str, false);
    }

    private void copyInputStream(InputStream inputStream, String str, String str2, Promise promise) {
        OutputStream outputStream;
        OutputStream outputStream2 = null;
        try {
            try {
                try {
                    outputStream = getOutputStream(str2, false);
                } catch (IOException unused) {
                    return;
                }
            } catch (Exception e2) {
                e = e2;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            byte[] bArr = new byte[10240];
            while (true) {
                int read = inputStream.read(bArr);
                if (read == -1) {
                    break;
                } else {
                    outputStream.write(bArr, 0, read);
                }
            }
            promise.resolve((Object) null);
            try {
                inputStream.close();
            } catch (IOException unused2) {
            }
        } catch (Exception e3) {
            e = e3;
            outputStream2 = outputStream;
            reject(promise, str, new Exception("Failed to copy '" + str + "' to " + str2 + " (" + e.getLocalizedMessage() + ")"));
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException unused3) {
                }
            }
            if (outputStream2 != null) {
                outputStream2.close();
            }
            return;
        } catch (Throwable th2) {
            th = th2;
            outputStream2 = outputStream;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException unused4) {
                }
            }
            if (outputStream2 == null) {
                throw th;
            }
            try {
                outputStream2.close();
                throw th;
            } catch (IOException unused5) {
                throw th;
            }
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }

    private Uri getFileUri(String str, boolean z) throws com.rnfs.e {
        Uri parse = Uri.parse(str);
        if (parse.getScheme() != null) {
            return parse;
        }
        File file = new File(str);
        if (!z && file.isDirectory()) {
            throw new com.rnfs.e("EISDIR", t2.p("EISDIR: illegal operation on a directory, read '", str, "'"));
        }
        return Uri.parse("file://" + str);
    }

    public InputStream getInputStream(String str) throws com.rnfs.e {
        try {
            InputStream openInputStream = this.reactContext.getContentResolver().openInputStream(getFileUri(str, false));
            if (openInputStream != null) {
                return openInputStream;
            }
            throw new com.rnfs.e("ENOENT", t2.p("ENOENT: could not open an input stream for '", str, "'"));
        } catch (FileNotFoundException e2) {
            throw new com.rnfs.e("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.e {
        Uri fileUri = getFileUri(str, z);
        if (fileUri.getScheme().equals("content")) {
            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;
    }

    private OutputStream getOutputStream(String str, boolean z) throws com.rnfs.e {
        try {
            OutputStream openOutputStream = this.reactContext.getContentResolver().openOutputStream(getFileUri(str, false), z ? "wa" : getWriteAccessByAPILevel());
            if (openOutputStream != null) {
                return openOutputStream;
            }
            throw new com.rnfs.e("ENOENT", t2.p("ENOENT: could not open an output stream for '", str, "'"));
        } catch (FileNotFoundException e2) {
            throw new com.rnfs.e("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, z ? "drawable" : "raw", getReactApplicationContext().getPackageName());
    }

    private String getWriteAccessByAPILevel() {
        return Build.VERSION.SDK_INT <= 28 ? "w" : "rwt";
    }

    public void reject(Promise promise, String str, Exception exc) {
        if (exc instanceof FileNotFoundException) {
            rejectFileNotFound(promise, str);
        } else if (!(exc instanceof com.rnfs.e)) {
            promise.reject((String) null, exc.getMessage());
        } else {
            com.rnfs.e eVar = (com.rnfs.e) exc;
            promise.reject(eVar.getCode(), eVar.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) {
        AsyncTaskInstrumentation.execute(new b(promise, str), new String[]{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(t2.p("Asset '", str, "' could not be opened")));
        }
    }

    @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(t2.p("Res '", str, "' could not be opened")));
        }
    }

    @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 i5 = readableMap.getInt("readTimeout");
            int i6 = readableMap.getInt("connectionTimeout");
            boolean z = readableMap.getBoolean("hasBeginCallback");
            boolean z2 = readableMap.getBoolean("hasProgressCallback");
            com.rnfs.a aVar = new com.rnfs.a();
            aVar.src = url;
            aVar.dest = file;
            aVar.headers = map;
            aVar.progressInterval = i3;
            aVar.progressDivider = i4;
            aVar.readTimeout = i5;
            aVar.connectionTimeout = i6;
            aVar.onTaskCompleted = new c(i2, promise, readableMap);
            if (z) {
                aVar.onDownloadBegin = new d(i2);
            }
            if (z2) {
                aVar.onDownloadProgress = new e(i2);
            }
            com.rnfs.d dVar = new com.rnfs.d();
            AsyncTaskInstrumentation.execute(dVar, new com.rnfs.a[]{aVar});
            this.downloaders.put(i2, dVar);
        } 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 {
            if (getResIdentifier(str) > 0) {
                promise.resolve(Boolean.TRUE);
            } else {
                promise.resolve(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());
            return hashMap;
        }
        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) {
        int i2;
        try {
            HashMap hashMap = new HashMap();
            hashMap.put("md5", StringUtils.MD5);
            hashMap.put("sha1", StringUtils.SHA1);
            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 {
                AsyncTaskInstrumentation.execute(new a(file, promise, str), new String[]{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);
                if (!str.isEmpty()) {
                    str2 = str + "/" + str2;
                }
                createMap.putString("path", str2);
                int i3 = 1;
                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.ELEMENT) ? 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 {
                try {
                    InputStream open = getReactApplicationContext().getAssets().open(str, 0);
                    if (open == null) {
                        reject(promise, str, new Exception("Failed to open file"));
                        if (open == null) {
                            return;
                        }
                    } else {
                        byte[] bArr = new byte[open.available()];
                        open.read(bArr);
                        promise.resolve(Base64.encodeToString(bArr, 2));
                    }
                    open.close();
                } 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) {
                    inputStream.close();
                }
            }
        } catch (IOException unused2) {
        }
    }

    @ReactMethod
    public void readFileRes(String str, Promise promise) {
        InputStream inputStream = null;
        try {
            try {
                try {
                    InputStream openRawResource = getReactApplicationContext().getResources().openRawResource(getResIdentifier(str));
                    if (openRawResource == null) {
                        reject(promise, str, new Exception("Failed to open file"));
                        if (openRawResource == null) {
                            return;
                        }
                    } else {
                        byte[] bArr = new byte[openRawResource.available()];
                        openRawResource.read(bArr);
                        promise.resolve(Base64.encodeToString(bArr, 2));
                    }
                    openRawResource.close();
                } 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) {
                    inputStream.close();
                }
            }
        } catch (IOException unused2) {
        }
    }

    @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(QBErrors.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(QBErrors.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.d dVar = this.downloaders.get(i2);
        if (dVar != null) {
            dVar.c();
        }
    }

    @ReactMethod
    public void stopUpload(int i2) {
        com.rnfs.j jVar = this.uploaders.get(i2);
        if (jVar != null) {
            jVar.d();
        }
    }

    @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(QBErrors.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) {
        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.g gVar = new com.rnfs.g();
            for (int i3 = 0; i3 < array.size(); i3++) {
                arrayList.add(array.getMap(i3));
            }
            gVar.src = url;
            gVar.files = arrayList;
            gVar.headers = map;
            gVar.method = string;
            gVar.fields = map2;
            gVar.binaryStreamOnly = z;
            gVar.onUploadComplete = new f(i2, promise, readableMap);
            if (z2) {
                gVar.onUploadBegin = new g(i2);
            }
            if (z3) {
                gVar.onUploadProgress = new h(i2);
            }
            com.rnfs.j jVar = new com.rnfs.j();
            AsyncTaskInstrumentation.execute(jVar, new com.rnfs.g[]{gVar});
            this.uploaders.put(i2, jVar);
        } catch (Exception e2) {
            e2.printStackTrace();
            reject(promise, readableMap.getString("toUrl"), e2);
        }
    }

    @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);
        }
    }
}