123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972 |
- package com.RNFetchBlob;
-
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.res.AssetFileDescriptor;
- 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.os.SystemClock;
- import android.util.Base64;
-
- import com.RNFetchBlob.Utils.PathResolver;
- import com.facebook.react.bridge.Arguments;
- import com.facebook.react.bridge.Callback;
- import com.facebook.react.bridge.Promise;
- import com.facebook.react.bridge.ReactApplicationContext;
- import com.facebook.react.bridge.ReadableArray;
- import com.facebook.react.bridge.WritableArray;
- import com.facebook.react.bridge.WritableMap;
- import com.facebook.react.modules.core.DeviceEventManagerModule;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.nio.ByteBuffer;
- import java.nio.charset.Charset;
- import java.nio.charset.CharsetEncoder;
- import java.security.MessageDigest;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
-
- public class RNFetchBlobFS {
-
- ReactApplicationContext mCtx;
- DeviceEventManagerModule.RCTDeviceEventEmitter emitter;
- String encoding = "base64";
- boolean append = false;
- OutputStream writeStreamInstance = null;
- static HashMap<String, RNFetchBlobFS> fileStreams = new HashMap<>();
-
- RNFetchBlobFS(ReactApplicationContext ctx) {
- this.mCtx = ctx;
- this.emitter = ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
- }
-
- static String getExternalFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {
- if(config.path != null)
- return config.path;
- else if(config.fileCache && config.appendExt != null)
- return RNFetchBlobFS.getTmpPath(ctx, taskId) + "." + config.appendExt;
- else
- return RNFetchBlobFS.getTmpPath(ctx, taskId);
- }
-
- /**
- * Write string with encoding to file
- * @param path Destination file path.
- * @param encoding Encoding of the string.
- * @param data Array passed from JS context.
- * @param promise RCT Promise
- */
- static public void writeFile(String path, String encoding, String data, final boolean append, final Promise promise) {
- try {
- int written = 0;
- File f = new File(path);
- File dir = f.getParentFile();
- if(!dir.exists())
- dir.mkdirs();
- FileOutputStream fout = new FileOutputStream(f, append);
- // write data from a file
- if(encoding.equalsIgnoreCase(RNFetchBlobConst.DATA_ENCODE_URI)) {
- data = normalizePath(data);
- File src = new File(data);
- if(!src.exists()) {
- promise.reject("RNfetchBlob writeFile error", "source file : " + data + " does not exist");
- fout.close();
- return ;
- }
- FileInputStream fin = new FileInputStream(src);
- byte [] buffer = new byte [10240];
- int read;
- written = 0;
- while((read = fin.read(buffer)) > 0) {
- fout.write(buffer, 0, read);
- written += read;
- }
- fin.close();
- }
- else {
- byte[] bytes = stringToBytes(data, encoding);
- fout.write(bytes);
- written = bytes.length;
- }
- fout.close();
- promise.resolve(written);
- } catch (Exception e) {
- promise.reject("RNFetchBlob writeFile error", e.getLocalizedMessage());
- }
- }
-
- /**
- * Write array of bytes into file
- * @param path Destination file path.
- * @param data Array passed from JS context.
- * @param promise RCT Promise
- */
- static public void writeFile(String path, ReadableArray data, final boolean append, final Promise promise) {
-
- try {
-
- File f = new File(path);
- File dir = f.getParentFile();
- if(!dir.exists())
- dir.mkdirs();
- FileOutputStream os = new FileOutputStream(f, append);
- byte [] bytes = new byte[data.size()];
- for(int i=0;i<data.size();i++) {
- bytes[i] = (byte) data.getInt(i);
- }
- os.write(bytes);
- os.close();
- promise.resolve(data.size());
- } catch (Exception e) {
- promise.reject("RNFetchBlob writeFile error", e.getLocalizedMessage());
- }
- }
-
- /**
- * Read file with a buffer that has the same size as the target file.
- * @param path Path of the file.
- * @param encoding Encoding of read stream.
- * @param promise
- */
- static public void readFile(String path, String encoding, final Promise promise ) {
- String resolved = normalizePath(path);
- if(resolved != null)
- path = resolved;
- try {
- byte[] bytes;
-
- if(resolved != null && resolved.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
- String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
- long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
- bytes = new byte[(int) length];
- InputStream in = RNFetchBlob.RCTContext.getAssets().open(assetName);
- in.read(bytes, 0, (int) length);
- in.close();
- }
- // issue 287
- else if(resolved == null) {
- InputStream in = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
- int length = (int) in.available();
- bytes = new byte[length];
- in.read(bytes);
- in.close();
- }
- else {
- File f = new File(path);
- int length = (int) f.length();
- bytes = new byte[length];
- FileInputStream in = new FileInputStream(f);
- in.read(bytes);
- in.close();
- }
-
- switch (encoding.toLowerCase()) {
- case "base64" :
- promise.resolve(Base64.encodeToString(bytes, Base64.NO_WRAP));
- break;
- case "ascii" :
- WritableArray asciiResult = Arguments.createArray();
- for(byte b : bytes) {
- asciiResult.pushInt((int)b);
- }
- promise.resolve(asciiResult);
- break;
- case "utf8" :
- promise.resolve(new String(bytes));
- break;
- default:
- promise.resolve(new String(bytes));
- break;
- }
- }
- catch(Exception err) {
- promise.reject("RNFetchBlob readFile error", err.getLocalizedMessage());
- }
-
- }
-
- /**
- * Static method that returns system folders to JS context
- * @param ctx React Native application context
- */
- static public Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
- Map<String, Object> res = new HashMap<>();
-
- res.put("DocumentDir", ctx.getFilesDir().getAbsolutePath());
- res.put("CacheDir", ctx.getCacheDir().getAbsolutePath());
- res.put("DCIMDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath());
- res.put("PictureDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
- res.put("MusicDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath());
- res.put("DownloadDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
- res.put("MovieDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath());
- res.put("RingtoneDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getAbsolutePath());
- String state;
- state = Environment.getExternalStorageState();
- if (state.equals(Environment.MEDIA_MOUNTED)) {
- res.put("SDCardDir", Environment.getExternalStorageDirectory().getAbsolutePath());
- res.put("SDCardApplicationDir", ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
- }
- res.put("MainBundleDir", ctx.getApplicationInfo().dataDir);
- return res;
- }
-
- /**
- * Static method that returns a temp file path
- * @param ctx React Native application context
- * @param taskId An unique string for identify
- * @return
- */
- static public String getTmpPath(ReactApplicationContext ctx, String taskId) {
- return RNFetchBlob.RCTContext.getFilesDir() + "/RNFetchBlobTmp_" + taskId;
- }
-
- /**
- * Create a file stream for read
- * @param path File stream target path
- * @param encoding File stream decoder, should be one of `base64`, `utf8`, `ascii`
- * @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
- */
- public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
- String resolved = normalizePath(path);
- if(resolved != null)
- path = resolved;
- try {
-
- int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
- if(bufferSize > 0)
- chunkSize = bufferSize;
-
- InputStream fs;
-
- if(resolved != null && path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
- fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
-
- }
- // fix issue 287
- else if(resolved == null) {
- fs = RNFetchBlob.RCTContext.getContentResolver().openInputStream(Uri.parse(path));
- }
- else {
- fs = new FileInputStream(new File(path));
- }
-
- byte[] buffer = new byte[chunkSize];
- int cursor = 0;
- boolean error = false;
-
- if (encoding.equalsIgnoreCase("utf8")) {
- CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
- while ((cursor = fs.read(buffer)) != -1) {
- encoder.encode(ByteBuffer.wrap(buffer).asCharBuffer());
- String chunk = new String(buffer, 0, cursor);
- emitStreamEvent(streamId, "data", chunk);
- if(tick > 0)
- SystemClock.sleep(tick);
- }
- } else if (encoding.equalsIgnoreCase("ascii")) {
- while ((cursor = fs.read(buffer)) != -1) {
- WritableArray chunk = Arguments.createArray();
- for(int i =0;i<cursor;i++)
- {
- chunk.pushInt((int)buffer[i]);
- }
- emitStreamEvent(streamId, "data", chunk);
- if(tick > 0)
- SystemClock.sleep(tick);
- }
- } else if (encoding.equalsIgnoreCase("base64")) {
- while ((cursor = fs.read(buffer)) != -1) {
- if(cursor < chunkSize) {
- byte [] copy = new byte[cursor];
- for(int i =0;i<cursor;i++) {
- copy[i] = buffer[i];
- }
- emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
- }
- else
- emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
- if(tick > 0)
- SystemClock.sleep(tick);
- }
- } else {
- String msg = "unrecognized encoding `" + encoding + "`";
- emitStreamEvent(streamId, "error", msg);
- error = true;
- }
-
- if(!error)
- emitStreamEvent(streamId, "end", "");
- fs.close();
- buffer = null;
-
- } catch (Exception err) {
- emitStreamEvent(streamId, "warn", "Failed to convert data to " + encoding +
- " encoded string, this might due to the source data is not able to convert using this encoding.");
- err.printStackTrace();
- }
- }
-
- /**
- * Create a write stream and store its instance in RNFetchBlobFS.fileStreams
- * @param path Target file path
- * @param encoding Should be one of `base64`, `utf8`, `ascii`
- * @param append Flag represents if the file stream overwrite existing content
- * @param callback
- */
- public void writeStream(String path, String encoding, boolean append, Callback callback) {
- File dest = new File(path);
- if(!dest.exists() || dest.isDirectory()) {
- callback.invoke("target path `" + path + "` may not exist or it is a folder");
- return;
- }
- try {
- OutputStream fs = new FileOutputStream(path, append);
- this.encoding = encoding;
- this.append = append;
- String streamId = UUID.randomUUID().toString();
- RNFetchBlobFS.fileStreams.put(streamId, this);
- this.writeStreamInstance = fs;
- callback.invoke(null, streamId);
- } catch(Exception err) {
- callback.invoke("failed to create write stream at path `" + path + "` " + err.getLocalizedMessage());
- }
-
- }
-
- /**
- * Write a chunk of data into a file stream.
- * @param streamId File stream ID
- * @param data Data chunk in string format
- * @param callback JS context callback
- */
- static void writeChunk(String streamId, String data, Callback callback) {
-
- RNFetchBlobFS fs = fileStreams.get(streamId);
- OutputStream stream = fs.writeStreamInstance;
- byte [] chunk = RNFetchBlobFS.stringToBytes(data, fs.encoding);
-
- try {
- stream.write(chunk);
- callback.invoke();
- } catch (Exception e) {
- callback.invoke(e.getLocalizedMessage());
- }
- }
-
- /**
- * Write data using ascii array
- * @param streamId File stream ID
- * @param data Data chunk in ascii array format
- * @param callback JS context callback
- */
- static void writeArrayChunk(String streamId, ReadableArray data, Callback callback) {
-
- try {
- RNFetchBlobFS fs = fileStreams.get(streamId);
- OutputStream stream = fs.writeStreamInstance;
- byte [] chunk = new byte[data.size()];
- for(int i =0; i< data.size();i++) {
- chunk[i] = (byte) data.getInt(i);
- }
- stream.write(chunk);
- callback.invoke();
- } catch (Exception e) {
- callback.invoke(e.getLocalizedMessage());
- }
- }
-
- /**
- * Close file write stream by ID
- * @param streamId Stream ID
- * @param callback JS context callback
- */
- static void closeStream(String streamId, Callback callback) {
- try {
- RNFetchBlobFS fs = fileStreams.get(streamId);
- OutputStream stream = fs.writeStreamInstance;
- fileStreams.remove(streamId);
- stream.close();
- callback.invoke();
- } catch(Exception err) {
- callback.invoke(err.getLocalizedMessage());
- }
- }
-
- /**
- * Unlink file at path
- * @param path Path of target
- * @param callback JS context callback
- */
- static void unlink(String path, Callback callback) {
- try {
- RNFetchBlobFS.deleteRecursive(new File(path));
- callback.invoke(null, true);
- } catch(Exception err) {
- if(err != null)
- callback.invoke(err.getLocalizedMessage(), false);
- }
- }
-
- static void deleteRecursive(File fileOrDirectory) {
-
- if (fileOrDirectory.isDirectory()) {
- for (File child : fileOrDirectory.listFiles()) {
- deleteRecursive(child);
- }
- }
- fileOrDirectory.delete();
- }
-
- /**
- * Make a folder
- * @param path Source path
- * @param callback JS context callback
- */
- static void mkdir(String path, Callback callback) {
- File dest = new File(path);
- if(dest.exists()) {
- callback.invoke("mkdir failed, folder already exists at " + path);
- return;
- }
- dest.mkdirs();
- callback.invoke();
- }
-
- /**
- * Copy file to destination path
- * @param path Source path
- * @param dest Target path
- * @param callback JS context callback
- */
- static void cp(String path, String dest, Callback callback) {
-
- path = normalizePath(path);
- InputStream in = null;
- OutputStream out = null;
-
- try {
-
- if(!isPathExists(path)) {
- callback.invoke("source file at path`" + path + "` does not exist");
- return;
- }
- if(!new File(dest).exists())
- new File(dest).createNewFile();
-
- in = inputStreamFromPath(path);
- out = new FileOutputStream(dest);
-
- byte[] buf = new byte[10240];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
-
- } catch (Exception err) {
- callback.invoke(err.getLocalizedMessage());
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- callback.invoke();
- } catch (Exception e) {
- callback.invoke(e.getLocalizedMessage());
- }
- }
- }
-
- /**
- * Move file
- * @param path Source file path
- * @param dest Destination file path
- * @param callback JS context callback
- */
- static void mv(String path, String dest, Callback callback) {
- File src = new File(path);
- if(!src.exists()) {
- callback.invoke("source file at path `" + path + "` does not exist");
- return;
- }
- src.renameTo(new File(dest));
- callback.invoke();
- }
-
- /**
- * Check if the path exists, also check if it is a folder when exists.
- * @param path Path to check
- * @param callback JS context callback
- */
- static void exists(String path, Callback callback) {
-
- if(isAsset(path)) {
- try {
- String filename = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
- AssetFileDescriptor fd = RNFetchBlob.RCTContext.getAssets().openFd(filename);
- callback.invoke(true, false);
- } catch (IOException e) {
- callback.invoke(false, false);
- }
- }
- else {
- path = normalizePath(path);
- boolean exist = new File(path).exists();
- boolean isDir = new File(path).isDirectory();
- callback.invoke(exist, isDir);
- }
- }
-
- /**
- * List content of folder
- * @param path Target folder
- * @param callback JS context callback
- */
- static void ls(String path, Callback callback) {
- path = normalizePath(path);
- File src = new File(path);
- if (!src.exists() || !src.isDirectory()) {
- callback.invoke("failed to list path `" + path + "` for it is not exist or it is not a folder");
- return;
- }
- String[] files = new File(path).list();
- WritableArray arg = Arguments.createArray();
- for (String i : files) {
- arg.pushString(i);
- }
- callback.invoke(null, arg);
- }
-
- /**
- * Create a file by slicing given file path
- * @param src Source file path
- * @param dest Destination of created file
- * @param start Start byte offset in source file
- * @param end End byte offset
- * @param encode NOT IMPLEMENTED
- */
- public static void slice(String src, String dest, int start, int end, String encode, Promise promise) {
- try {
- src = normalizePath(src);
- File source = new File(src);
- if(!source.exists()) {
- promise.reject("RNFetchBlob slice error", "source file : " + src + " does not exist");
- return;
- }
- long size = source.length();
- long max = Math.min(size, end);
- long expected = max - start;
- long now = 0;
- FileInputStream in = new FileInputStream(new File(src));
- FileOutputStream out = new FileOutputStream(new File(dest));
- in.skip(start);
- byte [] buffer = new byte[10240];
- while(now < expected) {
- long read = in.read(buffer, 0, 10240);
- long remain = expected - now;
- if(read <= 0) {
- break;
- }
- out.write(buffer, 0, (int) Math.min(remain, read));
- now += read;
- }
- in.close();
- out.flush();
- out.close();
- promise.resolve(dest);
- } catch (Exception e) {
- e.printStackTrace();
- promise.reject("RNFetchBlob slice error", e.getLocalizedMessage());
- }
- }
-
- static void lstat(String path, final Callback callback) {
- path = normalizePath(path);
-
- new AsyncTask<String, Integer, Integer>() {
- @Override
- protected Integer doInBackground(String ...args) {
- WritableArray res = Arguments.createArray();
- if(args[0] == null) {
- callback.invoke("the path specified for lstat is either `null` or `undefined`.");
- return 0;
- }
- File src = new File(args[0]);
- if(!src.exists()) {
- callback.invoke("failed to lstat path `" + args[0] + "` because it does not exist or it is not a folder");
- return 0;
- }
- if(src.isDirectory()) {
- String [] files = src.list();
- for(String p : files) {
- res.pushMap(statFile(src.getPath() + "/" + p));
- }
- }
- else {
- res.pushMap(statFile(src.getAbsolutePath()));
- }
- callback.invoke(null, res);
- return 0;
- }
- }.execute(path);
- }
-
- /**
- * show status of a file or directory
- * @param path
- * @param callback
- */
- static void stat(String path, Callback callback) {
- try {
- path = normalizePath(path);
- WritableMap result = statFile(path);
- if(result == null)
- callback.invoke("failed to stat path `" + path + "` because it does not exist or it is not a folder", null);
- else
- callback.invoke(null, result);
- } catch(Exception err) {
- callback.invoke(err.getLocalizedMessage());
- }
- }
-
- /**
- * Basic stat method
- * @param path
- * @return Stat result of a file or path
- */
- static WritableMap statFile(String path) {
- try {
- path = normalizePath(path);
- WritableMap stat = Arguments.createMap();
- if(isAsset(path)) {
- String name = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
- AssetFileDescriptor fd = RNFetchBlob.RCTContext.getAssets().openFd(name);
- stat.putString("filename", name);
- stat.putString("path", path);
- stat.putString("type", "asset");
- stat.putString("size", String.valueOf(fd.getLength()));
- stat.putInt("lastModified", 0);
- }
- else {
- File target = new File(path);
- if (!target.exists()) {
- return null;
- }
- stat.putString("filename", target.getName());
- stat.putString("path", target.getPath());
- stat.putString("type", target.isDirectory() ? "directory" : "file");
- stat.putString("size", String.valueOf(target.length()));
- String lastModified = String.valueOf(target.lastModified());
- stat.putString("lastModified", lastModified);
-
- }
- return stat;
- } catch(Exception err) {
- return null;
- }
- }
-
- /**
- * Media scanner scan file
- * @param path
- * @param mimes
- * @param callback
- */
- void scanFile(String [] path, String[] mimes, final Callback callback) {
- try {
- MediaScannerConnection.scanFile(mCtx, path, mimes, new MediaScannerConnection.OnScanCompletedListener() {
- @Override
- public void onScanCompleted(String s, Uri uri) {
- callback.invoke(null, true);
- }
- });
- } catch(Exception err) {
- callback.invoke(err.getLocalizedMessage(), null);
- }
- }
-
- static void hash(String path, String algorithm, Promise promise) {
- try {
- Map<String, String> algorithms = new HashMap<>();
-
- algorithms.put("md5", "MD5");
- algorithms.put("sha1", "SHA-1");
- algorithms.put("sha224", "SHA-224");
- algorithms.put("sha256", "SHA-256");
- algorithms.put("sha384", "SHA-384");
- algorithms.put("sha512", "SHA-512");
-
- if (!algorithms.containsKey(algorithm)) throw new Exception("Invalid hash algorithm");
-
- File file = new File(path);
-
- if (file.isDirectory()) {
- promise.reject("hash error", "EISDIR: illegal operation on a directory, read");
- return;
- }
-
- if (!file.exists()) {
- promise.reject("hash error", "ENOENT: no such file or directory, open '" + path + "'");
- return;
- }
-
- MessageDigest md = MessageDigest.getInstance(algorithms.get(algorithm));
-
- FileInputStream inputStream = new FileInputStream(path);
- byte[] buffer = new byte[(int)file.length()];
-
- int read;
- while ((read = inputStream.read(buffer)) != -1) {
- md.update(buffer, 0, read);
- }
-
- StringBuilder hexString = new StringBuilder();
- for (byte digestByte : md.digest())
- hexString.append(String.format("%02x", digestByte));
-
- promise.resolve(hexString.toString());
- } catch (Exception ex) {
- ex.printStackTrace();
- promise.reject("hash error", ex.getLocalizedMessage());
- }
- }
-
- /**
- * Create new file at path
- * @param path The destination path of the new file.
- * @param data Initial data of the new file.
- * @param encoding Encoding of initial data.
- * @param callback RCT bridge callback.
- */
- static void createFile(String path, String data, String encoding, Callback callback) {
- try {
- File dest = new File(path);
- boolean created = dest.createNewFile();
- if(encoding.equals(RNFetchBlobConst.DATA_ENCODE_URI)) {
- String orgPath = data.replace(RNFetchBlobConst.FILE_PREFIX, "");
- File src = new File(orgPath);
- if(!src.exists()) {
- callback.invoke("source file : " + data + " does not exist");
- return ;
- }
- FileInputStream fin = new FileInputStream(src);
- OutputStream ostream = new FileOutputStream(dest);
- byte[] buffer = new byte[10240];
- int read = fin.read(buffer);
- while (read > 0) {
- ostream.write(buffer, 0, read);
- read = fin.read(buffer);
- }
- fin.close();
- ostream.close();
- } else {
- if (!created) {
- callback.invoke("failed to create new file at path `" + path + "` because its parent path " +
- "may not exist, or the file already exists. If you intended to overwrite the " +
- "existing file use fs.writeFile instead.");
- return;
- }
- OutputStream ostream = new FileOutputStream(dest);
- ostream.write(RNFetchBlobFS.stringToBytes(data, encoding));
- }
- callback.invoke(null, path);
- } catch(Exception err) {
- callback.invoke(err.getLocalizedMessage());
- }
- }
-
- /**
- * Create file for ASCII encoding
- * @param path Path of new file.
- * @param data Content of new file
- * @param callback JS context callback
- */
- static void createFileASCII(String path, ReadableArray data, Callback callback) {
- try {
- File dest = new File(path);
- if(dest.exists()) {
- callback.invoke("failed to create new file at path `" + path + "`, file already exists.");
- return;
- }
- boolean created = dest.createNewFile();
- if(!created) {
- callback.invoke("failed to create new file at path `" + path + "` because its parent path may not exist");
- return;
- }
- OutputStream ostream = new FileOutputStream(dest);
- byte [] chunk = new byte[data.size()];
- for(int i =0; i<data.size();i++) {
- chunk[i] = (byte) data.getInt(i);
- }
- ostream.write(chunk);
- chunk = null;
- callback.invoke(null, path);
- } catch(Exception err) {
- callback.invoke(err.getLocalizedMessage());
- }
- }
-
- static void df(Callback callback) {
- StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
- WritableMap args = Arguments.createMap();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
- args.putString("internal_free", String.valueOf(stat.getFreeBytes()));
- args.putString("internal_total", String.valueOf(stat.getTotalBytes()));
- StatFs statEx = new StatFs(Environment.getExternalStorageDirectory().getPath());
- args.putString("external_free", String.valueOf(statEx.getFreeBytes()));
- args.putString("external_total", String.valueOf(statEx.getTotalBytes()));
-
- }
- callback.invoke(null ,args);
- }
-
- /**
- * Remove files in session.
- * @param paths An array of file paths.
- * @param callback JS contest callback
- */
- static void removeSession(ReadableArray paths, final Callback callback) {
-
- AsyncTask<ReadableArray, Integer, Integer> task = new AsyncTask<ReadableArray, Integer, Integer>() {
- @Override
- protected Integer doInBackground(ReadableArray ...paths) {
- try {
- for (int i = 0; i < paths[0].size(); i++) {
- File f = new File(paths[0].getString(i));
- if (f.exists())
- f.delete();
- }
- callback.invoke(null, true);
- } catch(Exception err) {
- callback.invoke(err.getLocalizedMessage());
- }
- return paths[0].size();
- }
- };
- task.execute(paths);
- }
-
- /**
- * String to byte converter method
- * @param data Raw data in string format
- * @param encoding Decoder name
- * @return Converted data byte array
- */
- private static byte[] stringToBytes(String data, String encoding) {
- if(encoding.equalsIgnoreCase("ascii")) {
- return data.getBytes(Charset.forName("US-ASCII"));
- }
- else if(encoding.toLowerCase().contains("base64")) {
- return Base64.decode(data, Base64.NO_WRAP);
-
- }
- else if(encoding.equalsIgnoreCase("utf8")) {
- return data.getBytes(Charset.forName("UTF-8"));
- }
- return data.getBytes(Charset.forName("US-ASCII"));
- }
-
- /**
- * Private method for emit read stream event.
- * @param streamName ID of the read stream
- * @param event Event name, `data`, `end`, `error`, etc.
- * @param data Event data
- */
- private void emitStreamEvent(String streamName, String event, String data) {
- WritableMap eventData = Arguments.createMap();
- eventData.putString("event", event);
- eventData.putString("detail", data);
- this.emitter.emit(streamName, eventData);
- }
-
- private void emitStreamEvent(String streamName, String event, WritableArray data) {
- WritableMap eventData = Arguments.createMap();
- eventData.putString("event", event);
- eventData.putArray("detail", data);
- this.emitter.emit(streamName, eventData);
- }
-
- // TODO : should we remove this ?
- void emitFSData(String taskId, String event, String data) {
- WritableMap eventData = Arguments.createMap();
- eventData.putString("event", event);
- eventData.putString("detail", data);
- this.emitter.emit("RNFetchBlobStream" + taskId, eventData);
- }
-
- /**
- * Get input stream of the given path, when the path is a string starts with bundle-assets://
- * the stream is created by Assets Manager, otherwise use FileInputStream.
- * @param path The file to open stream
- * @return InputStream instance
- * @throws IOException
- */
- static InputStream inputStreamFromPath(String path) throws IOException {
- if (path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
- return RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
- }
- return new FileInputStream(new File(path));
- }
-
- /**
- * Check if the asset or the file exists
- * @param path A file path URI string
- * @return A boolean value represents if the path exists.
- */
- static boolean isPathExists(String path) {
- if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
- try {
- RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
- } catch (IOException e) {
- return false;
- }
- return true;
- }
- else {
- return new File(path).exists();
- }
-
- }
-
- static boolean isAsset(String path) {
- if(path != null)
- return path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET);
- return false;
- }
-
- /**
- * Normalize the path, remove URI scheme (xxx://) so that we can handle it.
- * @param path URI string.
- * @return Normalized string
- */
- static String normalizePath(String path) {
- if(path == null)
- return null;
- if(!path.matches("\\w+\\:.*"))
- return path;
- if(path.startsWith("file://")) {
- return path.replace("file://", "");
- }
-
- Uri uri = Uri.parse(path);
- if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
- return path;
- }
- else
- return PathResolver.getRealPathFromURI(RNFetchBlob.RCTContext, uri);
- }
-
- }
|