No Description

RNFetchBlob.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package com.RNFetchBlob;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import com.facebook.react.bridge.Callback;
  5. import com.facebook.react.bridge.Promise;
  6. import com.facebook.react.bridge.ReactApplicationContext;
  7. import com.facebook.react.bridge.ReactContextBaseJavaModule;
  8. import com.facebook.react.bridge.ReactMethod;
  9. import com.facebook.react.bridge.ReadableArray;
  10. import com.facebook.react.bridge.ReadableMap;
  11. import java.util.Map;
  12. import java.util.concurrent.LinkedBlockingQueue;
  13. import java.util.concurrent.ThreadPoolExecutor;
  14. import java.util.concurrent.TimeUnit;
  15. public class RNFetchBlob extends ReactContextBaseJavaModule {
  16. static ReactApplicationContext RCTContext;
  17. static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
  18. static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
  19. static LinkedBlockingQueue<Runnable> fsTaskQueue = new LinkedBlockingQueue<>();
  20. static ThreadPoolExecutor fsThreadPool = new ThreadPoolExecutor(2, 10, 5000, TimeUnit.MILLISECONDS, taskQueue);
  21. public RNFetchBlob(ReactApplicationContext reactContext) {
  22. super(reactContext);
  23. RCTContext = reactContext;
  24. }
  25. @Override
  26. public String getName() {
  27. return "RNFetchBlob";
  28. }
  29. @Override
  30. public Map<String, Object> getConstants() {
  31. return RNFetchBlobFS.getSystemfolders(this.getReactApplicationContext());
  32. }
  33. @ReactMethod
  34. public void createFile(final String path, final String content, final String encode, final Callback callback) {
  35. threadPool.execute(new Runnable() {
  36. @Override
  37. public void run() {
  38. RNFetchBlobFS.createFile(path, content, encode, callback);
  39. }
  40. });
  41. }
  42. @ReactMethod
  43. public void actionViewIntent(String path, String mime, Promise promise) {
  44. try {
  45. Intent intent= new Intent(Intent.ACTION_VIEW)
  46. .setDataAndType(Uri.parse("file://" + path), mime);
  47. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  48. this.getReactApplicationContext().startActivity(intent);
  49. promise.resolve(null);
  50. } catch(Exception ex) {
  51. promise.reject(ex.getLocalizedMessage());
  52. }
  53. }
  54. @ReactMethod
  55. public void createFileASCII(final String path, final ReadableArray dataArray, final Callback callback) {
  56. threadPool.execute(new Runnable() {
  57. @Override
  58. public void run() {
  59. RNFetchBlobFS.createFileASCII(path, dataArray, callback);
  60. }
  61. });
  62. }
  63. @ReactMethod
  64. public void writeArrayChunk(final String streamId, final ReadableArray dataArray, final Callback callback) {
  65. RNFetchBlobFS.writeArrayChunk(streamId, dataArray, callback);
  66. }
  67. @ReactMethod
  68. public void unlink(String path, Callback callback) {
  69. RNFetchBlobFS.unlink(path, callback);
  70. }
  71. @ReactMethod
  72. public void mkdir(String path, Callback callback) {
  73. RNFetchBlobFS.mkdir(path, callback);
  74. }
  75. @ReactMethod
  76. public void exists(String path, Callback callback) {
  77. RNFetchBlobFS.exists(path, callback);
  78. }
  79. @ReactMethod
  80. public void cp(final String path, final String dest, final Callback callback) {
  81. threadPool.execute(new Runnable() {
  82. @Override
  83. public void run() {
  84. RNFetchBlobFS.cp(path, dest, callback);
  85. }
  86. });
  87. }
  88. @ReactMethod
  89. public void mv(String path, String dest, Callback callback) {
  90. RNFetchBlobFS.mv(path, dest, callback);
  91. }
  92. @ReactMethod
  93. public void ls(String path, Callback callback) {
  94. RNFetchBlobFS.ls(path, callback);
  95. }
  96. @ReactMethod
  97. public void writeStream(String path, String encode, boolean append, Callback callback) {
  98. new RNFetchBlobFS(this.getReactApplicationContext()).writeStream(path, encode, append, callback);
  99. }
  100. @ReactMethod
  101. public void writeChunk(String streamId, String data, Callback callback) {
  102. RNFetchBlobFS.writeChunk(streamId, data, callback);
  103. }
  104. @ReactMethod
  105. public void closeStream(String streamId, Callback callback) {
  106. RNFetchBlobFS.closeStream(streamId, callback);
  107. }
  108. @ReactMethod
  109. public void removeSession(ReadableArray paths, Callback callback) {
  110. RNFetchBlobFS.removeSession(paths, callback);
  111. }
  112. @ReactMethod
  113. public void readFile(final String path, final String encoding, final Promise promise) {
  114. threadPool.execute(new Runnable() {
  115. @Override
  116. public void run() {
  117. RNFetchBlobFS.readFile(path, encoding, promise);
  118. }
  119. });
  120. }
  121. @ReactMethod
  122. public void writeFileArray(final String path, final ReadableArray data, final boolean append, final Promise promise) {
  123. threadPool.execute(new Runnable() {
  124. @Override
  125. public void run() {
  126. RNFetchBlobFS.writeFile(path, data, append, promise);
  127. }
  128. });
  129. }
  130. @ReactMethod
  131. public void writeFile(final String path, final String encoding, final String data, final boolean append, final Promise promise) {
  132. threadPool.execute(new Runnable() {
  133. @Override
  134. public void run() {
  135. RNFetchBlobFS.writeFile(path, encoding, data, append, promise);
  136. }
  137. });
  138. }
  139. @ReactMethod
  140. public void lstat(String path, Callback callback) {
  141. RNFetchBlobFS.lstat(path, callback);
  142. }
  143. @ReactMethod
  144. public void stat(String path, Callback callback) {
  145. RNFetchBlobFS.stat(path, callback);
  146. }
  147. @ReactMethod
  148. public void scanFile(final ReadableArray pairs, final Callback callback) {
  149. final ReactApplicationContext ctx = this.getReactApplicationContext();
  150. threadPool.execute(new Runnable() {
  151. @Override
  152. public void run() {
  153. int size = pairs.size();
  154. String [] p = new String[size];
  155. String [] m = new String[size];
  156. for(int i=0;i<size;i++) {
  157. ReadableMap pair = pairs.getMap(i);
  158. if(pair.hasKey("path")) {
  159. p[i] = pair.getString("path");
  160. if(pair.hasKey("mime"))
  161. m[i] = pair.getString("mime");
  162. else
  163. m[i] = null;
  164. }
  165. }
  166. new RNFetchBlobFS(ctx).scanFile(p, m, callback);
  167. }
  168. });
  169. }
  170. @ReactMethod
  171. /**
  172. * @param path Stream file path
  173. * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
  174. * @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
  175. */
  176. public void readStream(final String path, final String encoding, final int bufferSize, final int tick, final String streamId) {
  177. final ReactApplicationContext ctx = this.getReactApplicationContext();
  178. fsThreadPool.execute(new Runnable() {
  179. @Override
  180. public void run() {
  181. RNFetchBlobFS fs = new RNFetchBlobFS(ctx);
  182. fs.readStream(path, encoding, bufferSize, tick, streamId);
  183. }
  184. });
  185. }
  186. @ReactMethod
  187. public void cancelRequest(String taskId, Callback callback) {
  188. try {
  189. RNFetchBlobReq.cancelTask(taskId);
  190. callback.invoke(null, taskId);
  191. } catch (Exception ex) {
  192. callback.invoke(ex.getLocalizedMessage(), null);
  193. }
  194. }
  195. @ReactMethod
  196. public void slice(String src, String dest, int start, int end, Promise promise) {
  197. RNFetchBlobFS.slice(src, dest, start, end, "", promise);
  198. }
  199. @ReactMethod
  200. public void enableProgressReport(String taskId, int interval, int count) {
  201. RNFetchBlobProgressConfig config = new RNFetchBlobProgressConfig(true, interval, count, RNFetchBlobProgressConfig.ReportType.Download);
  202. RNFetchBlobReq.progressReport.put(taskId, config);
  203. }
  204. @ReactMethod
  205. public void enableUploadProgressReport(String taskId, int interval, int count) {
  206. RNFetchBlobProgressConfig config = new RNFetchBlobProgressConfig(true, interval, count, RNFetchBlobProgressConfig.ReportType.Upload);
  207. RNFetchBlobReq.uploadProgressReport.put(taskId, config);
  208. }
  209. @ReactMethod
  210. public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
  211. new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();
  212. }
  213. @ReactMethod
  214. public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
  215. new RNFetchBlobReq(options, taskId, method, url, headers, null, body, callback).run();
  216. }
  217. }