Нема описа

RNFetchBlobFS.java 31KB

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