Brak opisu

RNFetchBlobFS.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. package com.RNFetchBlob;
  2. import android.app.LoaderManager;
  3. import android.content.ContentResolver;
  4. import android.content.CursorLoader;
  5. import android.content.res.AssetFileDescriptor;
  6. import android.database.Cursor;
  7. import android.media.MediaScannerConnection;
  8. import android.net.Uri;
  9. import android.os.AsyncTask;
  10. import android.os.Environment;
  11. import android.os.Looper;
  12. import android.provider.MediaStore;
  13. import android.util.Base64;
  14. import com.facebook.react.bridge.Arguments;
  15. import com.facebook.react.bridge.Callback;
  16. import com.facebook.react.bridge.Promise;
  17. import com.facebook.react.bridge.ReactApplicationContext;
  18. import com.facebook.react.bridge.ReadableArray;
  19. import com.facebook.react.bridge.WritableArray;
  20. import com.facebook.react.bridge.WritableMap;
  21. import com.facebook.react.bridge.WritableNativeArray;
  22. import com.facebook.react.modules.core.DeviceEventManagerModule;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.OutputStream;
  30. import java.nio.charset.Charset;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. import java.util.UUID;
  34. /**
  35. * Created by wkh237 on 2016/5/26.
  36. */
  37. public class RNFetchBlobFS {
  38. ReactApplicationContext mCtx;
  39. DeviceEventManagerModule.RCTDeviceEventEmitter emitter;
  40. String encoding = "base64";
  41. boolean append = false;
  42. OutputStream writeStreamInstance = null;
  43. static HashMap<String, RNFetchBlobFS> fileStreams = new HashMap<>();
  44. RNFetchBlobFS(ReactApplicationContext ctx) {
  45. this.mCtx = ctx;
  46. this.emitter = ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
  47. }
  48. static String getExternalFilePath(ReactApplicationContext ctx, String taskId, RNFetchBlobConfig config) {
  49. if(config.path != null)
  50. return config.path;
  51. else if(config.fileCache && config.appendExt != null)
  52. return RNFetchBlobFS.getTmpPath(ctx, taskId) + "." + config.appendExt;
  53. else
  54. return RNFetchBlobFS.getTmpPath(ctx, taskId);
  55. }
  56. /**
  57. * Write string with encoding to file
  58. * @param path Destination file path.
  59. * @param encoding Encoding of the string.
  60. * @param data Array passed from JS context.
  61. * @param promise
  62. */
  63. static public void writeFile(String path, String encoding, String data, final boolean append, final Promise promise) {
  64. AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
  65. @Override
  66. protected Integer doInBackground(String... args) {
  67. try {
  68. String path = args[0];
  69. String encoding = args[1];
  70. String data = args[2];
  71. int written = 0;
  72. File f = new File(path);
  73. File dir = f.getParentFile();
  74. if(!dir.exists())
  75. dir.mkdirs();
  76. FileOutputStream fout = new FileOutputStream(f, append);
  77. // write data from a file
  78. if(encoding.equalsIgnoreCase(RNFetchBlobConst.DATA_ENCODE_URI)) {
  79. File src = new File(data);
  80. if(!src.exists()) {
  81. promise.reject("RNfetchBlob writeFileError", "source file : " + data + "not exists");
  82. fout.close();
  83. return null;
  84. }
  85. FileInputStream fin = new FileInputStream(src);
  86. byte [] buffer = new byte [10240];
  87. int read = fin.read(buffer);
  88. written = read;
  89. while(read > 0) {
  90. fout.write(buffer, 0, read);
  91. read = fin.read(buffer);
  92. written += read;
  93. }
  94. fin.close();
  95. }
  96. else {
  97. byte[] bytes = stringToBytes(data, encoding);
  98. fout.write(bytes);
  99. written = bytes.length;
  100. }
  101. fout.close();
  102. promise.resolve(written);
  103. } catch (Exception e) {
  104. promise.reject("RNFetchBlob writeFileError", e.getLocalizedMessage());
  105. }
  106. return null;
  107. }
  108. };
  109. task.execute(path, encoding, data);
  110. }
  111. /**
  112. * Write array of bytes into file
  113. * @param path Destination file path.
  114. * @param data Array passed from JS context.
  115. * @param promise
  116. */
  117. static public void writeFile(String path, ReadableArray data, final boolean append, final Promise promise) {
  118. AsyncTask<Object, Void, Void> task = new AsyncTask<Object, Void, Void>() {
  119. @Override
  120. protected Void doInBackground(Object... args) {
  121. try {
  122. String path = (String)args[0];
  123. ReadableArray data = (ReadableArray) args[1];
  124. File f = new File(path);
  125. File dir = f.getParentFile();
  126. if(!dir.exists())
  127. dir.mkdirs();
  128. FileOutputStream os = new FileOutputStream(f, append);
  129. byte [] bytes = new byte[data.size()];
  130. for(int i=0;i<data.size();i++) {
  131. bytes[i] = (byte) data.getInt(i);
  132. }
  133. os.write(bytes);
  134. os.close();
  135. promise.resolve(data.size());
  136. } catch (Exception e) {
  137. promise.reject("RNFetchBlob writeFileError", e.getLocalizedMessage());
  138. }
  139. return null;
  140. }
  141. };
  142. task.execute(path, data);
  143. }
  144. /**
  145. * Read file with a buffer that has the same size as the target file.
  146. * @param path Path of the file.
  147. * @param encoding Encoding of read stream.
  148. * @param promise
  149. */
  150. static public void readFile(String path, String encoding, final Promise promise ) {
  151. path = normalizePath(path);
  152. AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
  153. @Override
  154. protected Integer doInBackground(String... strings) {
  155. try {
  156. String path = strings[0];
  157. String encoding = strings[1];
  158. byte[] bytes;
  159. if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
  160. String assetName = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
  161. long length = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
  162. bytes = new byte[(int) length];
  163. InputStream in = RNFetchBlob.RCTContext.getAssets().open(assetName);
  164. in.read(bytes, 0, (int) length);
  165. in.close();
  166. }
  167. else {
  168. File f = new File(path);
  169. int length = (int) f.length();
  170. bytes = new byte[length];
  171. FileInputStream in = new FileInputStream(f);
  172. in.read(bytes);
  173. in.close();
  174. }
  175. switch (encoding.toLowerCase()) {
  176. case "base64" :
  177. promise.resolve(Base64.encodeToString(bytes, Base64.NO_WRAP));
  178. break;
  179. case "ascii" :
  180. WritableArray asciiResult = Arguments.createArray();
  181. for(byte b : bytes) {
  182. asciiResult.pushInt((int)b);
  183. }
  184. promise.resolve(asciiResult);
  185. break;
  186. case "utf8" :
  187. promise.resolve(new String(bytes));
  188. break;
  189. default:
  190. promise.resolve(new String(bytes));
  191. break;
  192. }
  193. }
  194. catch(Exception err) {
  195. promise.reject("ReadFile Error", err.getLocalizedMessage());
  196. }
  197. return null;
  198. }
  199. };
  200. task.execute(path, encoding);
  201. }
  202. /**
  203. * Static method that returns system folders to JS context
  204. * @param ctx React Native application context
  205. */
  206. static public Map<String, Object> getSystemfolders(ReactApplicationContext ctx) {
  207. Map<String, Object> res = new HashMap<>();
  208. res.put("DocumentDir", ctx.getFilesDir().getAbsolutePath());
  209. res.put("CacheDir", ctx.getCacheDir().getAbsolutePath());
  210. res.put("DCIMDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath());
  211. res.put("PictureDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
  212. res.put("MusicDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath());
  213. res.put("DownloadDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
  214. res.put("MovieDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath());
  215. res.put("RingtoneDir", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES).getAbsolutePath());
  216. return res;
  217. }
  218. /**
  219. * Static method that returns a temp file path
  220. * @param ctx React Native application context
  221. * @param taskId An unique string for identify
  222. * @return
  223. */
  224. static public String getTmpPath(ReactApplicationContext ctx, String taskId) {
  225. return RNFetchBlob.RCTContext.getFilesDir() + "/RNFetchBlobTmp_" + taskId;
  226. }
  227. /**
  228. * Create a file stream for read
  229. * @param path File stream target path
  230. * @param encoding File stream decoder, should be one of `base64`, `utf8`, `ascii`
  231. * @param bufferSize Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
  232. */
  233. public void readStream( String path, String encoding, int bufferSize) {
  234. path = normalizePath(path);
  235. AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
  236. @Override
  237. protected Integer doInBackground(String ... args) {
  238. String path = args[0];
  239. String encoding = args[1];
  240. int bufferSize = Integer.parseInt(args[2]);
  241. String eventName = "RNFetchBlobStream+" + path;
  242. try {
  243. int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
  244. if(bufferSize > 0)
  245. chunkSize = bufferSize;
  246. InputStream fs;
  247. if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
  248. fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
  249. }
  250. else {
  251. fs = new FileInputStream(new File(path));
  252. }
  253. byte[] buffer = new byte[chunkSize];
  254. int cursor = 0;
  255. boolean error = false;
  256. if (encoding.equalsIgnoreCase("utf8")) {
  257. while ((cursor = fs.read(buffer)) != -1) {
  258. String chunk = new String(buffer, 0, cursor, "UTF-8");
  259. emitStreamEvent(eventName, "data", chunk);
  260. }
  261. } else if (encoding.equalsIgnoreCase("ascii")) {
  262. while ((cursor = fs.read(buffer)) != -1) {
  263. WritableArray chunk = Arguments.createArray();
  264. for(int i =0;i<cursor;i++)
  265. {
  266. chunk.pushInt((int)buffer[i]);
  267. }
  268. emitStreamEvent(eventName, "data", chunk);
  269. }
  270. } else if (encoding.equalsIgnoreCase("base64")) {
  271. while ((cursor = fs.read(buffer)) != -1) {
  272. if(cursor < chunkSize) {
  273. byte [] copy = new byte[cursor];
  274. for(int i =0;i<cursor;i++) {
  275. copy[i] = buffer[i];
  276. }
  277. emitStreamEvent(eventName, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
  278. }
  279. else
  280. emitStreamEvent(eventName, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
  281. }
  282. } else {
  283. String msg = "unrecognized encoding `" + encoding + "`";
  284. emitStreamEvent(eventName, "error", msg);
  285. error = true;
  286. }
  287. if(!error)
  288. emitStreamEvent(eventName, "end", "");
  289. fs.close();
  290. buffer = null;
  291. } catch (Exception err) {
  292. emitStreamEvent(eventName, "error", err.getLocalizedMessage());
  293. }
  294. return null;
  295. }
  296. };
  297. task.execute(path, encoding, String.valueOf(bufferSize));
  298. }
  299. /**
  300. * Create a write stream and store its instance in RNFetchBlobFS.fileStreams
  301. * @param path Target file path
  302. * @param encoding Should be one of `base64`, `utf8`, `ascii`
  303. * @param append Flag represents if the file stream overwrite existing content
  304. * @param callback
  305. */
  306. public void writeStream(String path, String encoding, boolean append, Callback callback) {
  307. File dest = new File(path);
  308. if(!dest.exists() || dest.isDirectory()) {
  309. callback.invoke("write stream error: target path `" + path + "` may not exists or it's a folder");
  310. return;
  311. }
  312. try {
  313. OutputStream fs = new FileOutputStream(path, append);
  314. this.encoding = encoding;
  315. this.append = append;
  316. String streamId = UUID.randomUUID().toString();
  317. RNFetchBlobFS.fileStreams.put(streamId, this);
  318. this.writeStreamInstance = fs;
  319. callback.invoke(null, streamId);
  320. } catch(Exception err) {
  321. callback.invoke("write stream error: failed to create write stream at path `"+path+"` "+ err.getLocalizedMessage());
  322. }
  323. }
  324. /**
  325. * Write a chunk of data into a file stream.
  326. * @param streamId File stream ID
  327. * @param data Data chunk in string format
  328. * @param callback JS context callback
  329. */
  330. static void writeChunk(String streamId, String data, Callback callback) {
  331. RNFetchBlobFS fs = fileStreams.get(streamId);
  332. OutputStream stream = fs.writeStreamInstance;
  333. byte [] chunk = RNFetchBlobFS.stringToBytes(data, fs.encoding);
  334. try {
  335. stream.write(chunk);
  336. callback.invoke();
  337. } catch (Exception e) {
  338. callback.invoke(e.getLocalizedMessage());
  339. }
  340. }
  341. /**
  342. * Write data using ascii array
  343. * @param streamId File stream ID
  344. * @param data Data chunk in ascii array format
  345. * @param callback JS context callback
  346. */
  347. static void writeArrayChunk(String streamId, ReadableArray data, Callback callback) {
  348. RNFetchBlobFS fs = fileStreams.get(streamId);
  349. OutputStream stream = fs.writeStreamInstance;
  350. byte [] chunk = new byte[data.size()];
  351. for(int i =0; i< data.size();i++) {
  352. chunk[i] = (byte) data.getInt(i);
  353. }
  354. try {
  355. stream.write(chunk);
  356. callback.invoke();
  357. chunk = null;
  358. } catch (Exception e) {
  359. callback.invoke(e.getLocalizedMessage());
  360. }
  361. }
  362. /**
  363. * Close file write stream by ID
  364. * @param streamId Stream ID
  365. * @param callback JS context callback
  366. */
  367. static void closeStream(String streamId, Callback callback) {
  368. try {
  369. RNFetchBlobFS fs = fileStreams.get(streamId);
  370. OutputStream stream = fs.writeStreamInstance;
  371. fileStreams.remove(streamId);
  372. stream.close();
  373. callback.invoke();
  374. } catch(Exception err) {
  375. callback.invoke(err.getLocalizedMessage());
  376. }
  377. }
  378. /**
  379. * Unlink file at path
  380. * @param path Path of target
  381. * @param callback JS context callback
  382. */
  383. static void unlink(String path, Callback callback) {
  384. try {
  385. RNFetchBlobFS.deleteRecursive(new File(path));
  386. callback.invoke(null, true);
  387. } catch(Exception err) {
  388. if(err != null)
  389. callback.invoke(err.getLocalizedMessage(), false);
  390. }
  391. }
  392. static void deleteRecursive(File fileOrDirectory) {
  393. if (fileOrDirectory.isDirectory()) {
  394. for (File child : fileOrDirectory.listFiles()) {
  395. deleteRecursive(child);
  396. }
  397. }
  398. fileOrDirectory.delete();
  399. }
  400. /**
  401. * Make a folder
  402. * @param path Source path
  403. * @param callback JS context callback
  404. */
  405. static void mkdir(String path, Callback callback) {
  406. File dest = new File(path);
  407. if(dest.exists()) {
  408. callback.invoke("mkdir error: failed to create folder at `" + path + "` folder already exists");
  409. return;
  410. }
  411. dest.mkdirs();
  412. callback.invoke();
  413. }
  414. /**
  415. * Copy file to destination path
  416. * @param path Source path
  417. * @param dest Target path
  418. * @param callback JS context callback
  419. */
  420. static void cp(String path, String dest, Callback callback) {
  421. path = normalizePath(path);
  422. InputStream in = null;
  423. OutputStream out = null;
  424. try {
  425. if(!isPathExists(path)) {
  426. callback.invoke("cp error: source file at path`" + path + "` not exists");
  427. return;
  428. }
  429. if(!new File(dest).exists())
  430. new File(dest).createNewFile();
  431. in = inputStreamFromPath(path);
  432. out = new FileOutputStream(dest);
  433. byte[] buf = new byte[1024];
  434. int len;
  435. while ((len = in.read(buf)) > 0) {
  436. out.write(buf, 0, len);
  437. }
  438. } catch (Exception err) {
  439. if(err != null)
  440. callback.invoke(err.getLocalizedMessage());
  441. } finally {
  442. try {
  443. in.close();
  444. out.close();
  445. callback.invoke();
  446. } catch (IOException e) {
  447. callback.invoke(e.getLocalizedMessage());
  448. }
  449. }
  450. }
  451. /**
  452. * Move file
  453. * @param path Source file path
  454. * @param dest Destination file path
  455. * @param callback JS context callback
  456. */
  457. static void mv(String path, String dest, Callback callback) {
  458. File src = new File(path);
  459. if(!src.exists()) {
  460. callback.invoke("mv error: source file at path `" + path + "` does not exists");
  461. return;
  462. }
  463. src.renameTo(new File(dest));
  464. callback.invoke();
  465. }
  466. /**
  467. * Check if the path exists, also check if it is a folder when exists.
  468. * @param path Path to check
  469. * @param callback JS context callback
  470. */
  471. static void exists(String path, Callback callback) {
  472. path = normalizePath(path);
  473. if(isAsset(path)) {
  474. try {
  475. String filename = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
  476. AssetFileDescriptor fd = RNFetchBlob.RCTContext.getAssets().openFd(filename);
  477. callback.invoke(true, false);
  478. } catch (IOException e) {
  479. callback.invoke(false, false);
  480. }
  481. }
  482. else {
  483. boolean exist = new File(path).exists();
  484. boolean isDir = new File(path).isDirectory();
  485. callback.invoke(exist, isDir);
  486. }
  487. }
  488. /**
  489. * List content of folder
  490. * @param path Target folder
  491. * @param callback JS context callback
  492. */
  493. static void ls(String path, Callback callback) {
  494. path = normalizePath(path);
  495. File src = new File(path);
  496. if (!src.exists() || !src.isDirectory()) {
  497. callback.invoke("ls error: failed to list path `" + path + "` for it is not exist or it is not a folder");
  498. return;
  499. }
  500. String[] files = new File(path).list();
  501. WritableArray arg = Arguments.createArray();
  502. for (String i : files) {
  503. arg.pushString(i);
  504. }
  505. callback.invoke(null, arg);
  506. }
  507. /**
  508. * Create a file by slicing given file path
  509. * @param src Source file path
  510. * @param dest Destination of created file
  511. * @param start Start byte offset in source file
  512. * @param end End byte offset
  513. * @param encode
  514. * @param callback
  515. */
  516. public static void slice(String src, String dest, int start, int end, String encode, Callback callback) {
  517. try {
  518. long expected = end - start;
  519. long now = 0;
  520. FileInputStream in = new FileInputStream(new File(src));
  521. FileOutputStream out = new FileOutputStream(new File(dest));
  522. in.skip(start);
  523. byte [] buffer = new byte[10240];
  524. while(now < expected) {
  525. long read = in.read(buffer, 0, 10240);
  526. if(read <= 0) {
  527. break;
  528. }
  529. now += read;
  530. out.write(buffer, 0, (int) read);
  531. }
  532. in.close();
  533. out.close();
  534. callback.invoke(null, dest);
  535. } catch (Exception e) {
  536. e.printStackTrace();
  537. }
  538. }
  539. static void lstat(String path, final Callback callback) {
  540. path = normalizePath(path);
  541. new AsyncTask<String, Integer, Integer>() {
  542. @Override
  543. protected Integer doInBackground(String ...args) {
  544. WritableArray res = Arguments.createArray();
  545. File src = new File(args[0]);
  546. if(!src.exists()) {
  547. callback.invoke("lstat error: failed to list path `" + args[0] + "` for it is not exist or it is not a folder");
  548. return 0;
  549. }
  550. if(src.isDirectory()) {
  551. String [] files = src.list();
  552. for(String p : files) {
  553. res.pushMap(statFile ( src.getPath() + "/" + p));
  554. }
  555. }
  556. else {
  557. res.pushMap(statFile(src.getAbsolutePath()));
  558. }
  559. callback.invoke(null, res);
  560. return 0;
  561. }
  562. }.execute(path);
  563. }
  564. /**
  565. * show status of a file or directory
  566. * @param path
  567. * @param callback
  568. */
  569. static void stat(String path, Callback callback) {
  570. try {
  571. WritableMap result = statFile(path);
  572. if(result == null)
  573. callback.invoke("stat error: failed to list path `" + path + "` for it is not exist or it is not a folder", null);
  574. else
  575. callback.invoke(null, result);
  576. } catch(Exception err) {
  577. callback.invoke(err.getLocalizedMessage());
  578. }
  579. }
  580. /**
  581. * Basic stat method
  582. * @param path
  583. * @return Stat result of a file or path
  584. */
  585. static WritableMap statFile(String path) {
  586. try {
  587. path = normalizePath(path);
  588. WritableMap stat = Arguments.createMap();
  589. if(isAsset(path)) {
  590. String name = path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
  591. AssetFileDescriptor fd = RNFetchBlob.RCTContext.getAssets().openFd(name);
  592. stat.putString("filename", name);
  593. stat.putString("path", path);
  594. stat.putString("type", "asset");
  595. stat.putString("size", String.valueOf(fd.getLength()));
  596. stat.putString("lastModified", "0");
  597. }
  598. else {
  599. File target = new File(path);
  600. if (!target.exists()) {
  601. return null;
  602. }
  603. stat.putString("filename", target.getName());
  604. stat.putString("path", target.getPath());
  605. stat.putString("type", target.isDirectory() ? "directory" : "file");
  606. stat.putString("size", String.valueOf(target.length()));
  607. String lastModified = String.valueOf(target.lastModified());
  608. stat.putString("lastModified", lastModified);
  609. }
  610. return stat;
  611. } catch(Exception err) {
  612. return null;
  613. }
  614. }
  615. /**
  616. * Media scanner scan file
  617. * @param path
  618. * @param mimes
  619. * @param callback
  620. */
  621. void scanFile(String [] path, String[] mimes, final Callback callback) {
  622. try {
  623. MediaScannerConnection.scanFile(mCtx, path, mimes, new MediaScannerConnection.OnScanCompletedListener() {
  624. @Override
  625. public void onScanCompleted(String s, Uri uri) {
  626. callback.invoke(null, true);
  627. }
  628. });
  629. } catch(Exception err) {
  630. callback.invoke(err.getLocalizedMessage(), null);
  631. }
  632. }
  633. /**
  634. * Create new file at path
  635. * @param path
  636. * @param data
  637. * @param encoding
  638. * @param callback
  639. */
  640. static void createFile(String path, String data, String encoding, Callback callback) {
  641. try {
  642. File dest = new File(path);
  643. boolean created = dest.createNewFile();
  644. if(encoding.equals(RNFetchBlobConst.DATA_ENCODE_URI)) {
  645. String orgPath = data.replace(RNFetchBlobConst.FILE_PREFIX, "");
  646. File src = new File(orgPath);
  647. if(!src.exists()) {
  648. callback.invoke("RNfetchBlob writeFileError", "source file : " + data + "not exists");
  649. return ;
  650. }
  651. FileInputStream fin = new FileInputStream(src);
  652. OutputStream ostream = new FileOutputStream(dest);
  653. byte [] buffer = new byte [10240];
  654. int read = fin.read(buffer);
  655. while(read > 0) {
  656. ostream.write(buffer, 0, read);
  657. read = fin.read(buffer);
  658. }
  659. fin.close();
  660. ostream.close();
  661. }
  662. else {
  663. if (!created) {
  664. callback.invoke("create file error: failed to create file at path `" + path + "` for its parent path may not exists");
  665. return;
  666. }
  667. OutputStream ostream = new FileOutputStream(dest);
  668. ostream.write(RNFetchBlobFS.stringToBytes(data, encoding));
  669. }
  670. callback.invoke(null, path);
  671. } catch(Exception err) {
  672. callback.invoke(err.getLocalizedMessage());
  673. }
  674. }
  675. /**
  676. * Create file for ASCII encoding
  677. * @param path Path of new file.
  678. * @param data Content of new file
  679. * @param callback JS context callback
  680. */
  681. static void createFileASCII(String path, ReadableArray data, Callback callback) {
  682. try {
  683. File dest = new File(path);
  684. if(dest.exists()) {
  685. callback.invoke("create file error: failed to create file at path `" + path + "`, file already exists.");
  686. return;
  687. }
  688. boolean created = dest.createNewFile();
  689. if(!created) {
  690. callback.invoke("create file error: failed to create file at path `" + path + "` for its parent path may not exists");
  691. return;
  692. }
  693. OutputStream ostream = new FileOutputStream(dest);
  694. byte [] chunk = new byte[data.size()];
  695. for(int i =0; i<data.size();i++) {
  696. chunk[i] = (byte) data.getInt(i);
  697. }
  698. ostream.write(chunk);
  699. chunk = null;
  700. callback.invoke(null, path);
  701. } catch(Exception err) {
  702. callback.invoke(err.getLocalizedMessage());
  703. }
  704. }
  705. /**
  706. * Remove files in session.
  707. * @param paths An array of file paths.
  708. * @param callback JS contest callback
  709. */
  710. static void removeSession(ReadableArray paths, final Callback callback) {
  711. AsyncTask<ReadableArray, Integer, Integer> task = new AsyncTask<ReadableArray, Integer, Integer>() {
  712. @Override
  713. protected Integer doInBackground(ReadableArray ...paths) {
  714. try {
  715. for (int i = 0; i < paths[0].size(); i++) {
  716. File f = new File(paths[0].getString(i));
  717. if (f.exists())
  718. f.delete();
  719. }
  720. callback.invoke(null, true);
  721. } catch(Exception err) {
  722. callback.invoke(err.getLocalizedMessage());
  723. }
  724. return paths[0].size();
  725. }
  726. };
  727. task.execute(paths);
  728. }
  729. /**
  730. * String to byte converter method
  731. * @param data Raw data in string format
  732. * @param encoding Decoder name
  733. * @return Converted data byte array
  734. */
  735. private static byte[] stringToBytes(String data, String encoding) {
  736. if(encoding.equalsIgnoreCase("ascii")) {
  737. return data.getBytes(Charset.forName("US-ASCII"));
  738. }
  739. else if(encoding.equalsIgnoreCase("base64")) {
  740. return Base64.decode(data, Base64.NO_WRAP);
  741. }
  742. else if(encoding.equalsIgnoreCase("utf8")) {
  743. return data.getBytes(Charset.forName("UTF-8"));
  744. }
  745. return data.getBytes(Charset.forName("US-ASCII"));
  746. }
  747. /**
  748. * Private method for emit read stream event.
  749. * @param streamName ID of the read stream
  750. * @param event Event name, `data`, `end`, `error`, etc.
  751. * @param data Event data
  752. */
  753. void emitStreamEvent(String streamName, String event, String data) {
  754. WritableMap eventData = Arguments.createMap();
  755. eventData.putString("event", event);
  756. eventData.putString("detail", data);
  757. this.emitter.emit(streamName, eventData);
  758. }
  759. void emitStreamEvent(String streamName, String event, WritableArray data) {
  760. WritableMap eventData = Arguments.createMap();
  761. eventData.putString("event", event);
  762. eventData.putArray("detail", data);
  763. this.emitter.emit(streamName, eventData);
  764. }
  765. // TODO : should we remove this ?
  766. void emitFSData(String taskId, String event, String data) {
  767. WritableMap eventData = Arguments.createMap();
  768. eventData.putString("event", event);
  769. eventData.putString("detail", data);
  770. this.emitter.emit("RNFetchBlobStream" + taskId, eventData);
  771. }
  772. /**
  773. * Get input stream of the given path, when the path is a string starts with bundle-assets://
  774. * the stream is created by Assets Manager, otherwise use FileInputStream.
  775. * @param path
  776. * @return
  777. * @throws IOException
  778. */
  779. static InputStream inputStreamFromPath(String path) throws IOException {
  780. if (path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
  781. return RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
  782. }
  783. return new FileInputStream(new File(path));
  784. }
  785. /**
  786. * Check if the asset or the file exists
  787. * @param path
  788. * @return
  789. */
  790. static boolean isPathExists(String path) {
  791. if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
  792. try {
  793. RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
  794. } catch (IOException e) {
  795. return false;
  796. }
  797. return true;
  798. }
  799. else {
  800. return new File(path).exists();
  801. }
  802. }
  803. public static boolean isAsset(String path) {
  804. return path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET);
  805. }
  806. public static String normalizePath(String path) {
  807. if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
  808. return path;
  809. }
  810. else if (path.startsWith(RNFetchBlobConst.FILE_PREFIX_CONTENT)) {
  811. String filePath = null;
  812. Uri uri = Uri.parse(path);
  813. if (uri != null && "content".equals(uri.getScheme())) {
  814. ContentResolver resolver = RNFetchBlob.RCTContext.getContentResolver();
  815. Cursor cursor = resolver.query(uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
  816. cursor.moveToFirst();
  817. filePath = cursor.getString(0);
  818. cursor.close();
  819. } else {
  820. filePath = uri.getPath();
  821. }
  822. return filePath;
  823. }
  824. return path;
  825. }
  826. }