Keine Beschreibung

RNFetchBlob.java 9.3KB

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