No Description

RNFetchBlob.java 11KB

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