Ei kuvausta

RNFetchBlobBody.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package com.RNFetchBlob;
  2. import android.util.Base64;
  3. import android.util.Log;
  4. import com.facebook.react.bridge.Arguments;
  5. import com.facebook.react.bridge.ReactApplicationContext;
  6. import com.facebook.react.bridge.ReadableArray;
  7. import com.facebook.react.bridge.ReadableMap;
  8. import com.facebook.react.bridge.WritableMap;
  9. import com.facebook.react.modules.core.DeviceEventManagerModule;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import okhttp3.MediaType;
  18. import okhttp3.RequestBody;
  19. import okhttp3.FormBody;
  20. import okio.Buffer;
  21. import okio.BufferedSink;
  22. import okio.ForwardingSink;
  23. import okio.Okio;
  24. import okio.Sink;
  25. /**
  26. * Created by wkh237 on 2016/7/11.
  27. */
  28. public class RNFetchBlobBody extends RequestBody{
  29. InputStream requestStream;
  30. long contentLength;
  31. long bytesWritten = 0;
  32. ReadableArray form;
  33. String mTaskId;
  34. String rawBody;
  35. RNFetchBlobReq.RequestType requestType;
  36. MediaType mime;
  37. public RNFetchBlobBody(String taskId, RNFetchBlobReq.RequestType type, ReadableArray form, MediaType contentType) {
  38. this.mTaskId = taskId;
  39. this.form = form;
  40. requestType = type;
  41. mime = contentType;
  42. }
  43. public RNFetchBlobBody(String taskId, RNFetchBlobReq.RequestType type, String rawBody, MediaType contentType) {
  44. this.mTaskId = taskId;
  45. requestType = type;
  46. this.rawBody = rawBody;
  47. mime = contentType;
  48. }
  49. @Override
  50. public MediaType contentType() {
  51. return mime;
  52. }
  53. @Override
  54. public void writeTo(BufferedSink sink) throws IOException {
  55. ProgressReportingSource source = new ProgressReportingSource(sink, mTaskId);
  56. BufferedSink buffer = Okio.buffer(source);
  57. switch (requestType) {
  58. case Form:
  59. writeFormData(sink);
  60. break;
  61. case SingleFile:
  62. writeOctetData(sink);
  63. break;
  64. case AsIs:
  65. writeRawData(sink);
  66. break;
  67. }
  68. buffer.flush();
  69. }
  70. private void writeFormData(BufferedSink sink) throws IOException {
  71. String boundary = "RNFetchBlob-" + mTaskId;
  72. ArrayList<FormField> fields = countFormDataLength();
  73. ReactApplicationContext ctx = RNFetchBlob.RCTContext;
  74. for(int i = 0;i < fields.size(); i++) {
  75. FormField field = fields.get(i);
  76. String data = field.data;
  77. String name = field.name;
  78. // skip invalid fields
  79. if(name == null || data == null)
  80. continue;
  81. // form begin
  82. String header = "--" + boundary + "\r\n";
  83. if (field.filename != null) {
  84. header += "Content-Disposition: form-data; name=" + name + "; filename=" + field.filename + "\r\n";
  85. header += "Content-Type: " + field.mime+ "\r\n\r\n";
  86. sink.write(header.getBytes());
  87. // file field header end
  88. // upload from storage
  89. if (data.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
  90. String orgPath = data.substring(RNFetchBlobConst.FILE_PREFIX.length());
  91. orgPath = RNFetchBlobFS.normalizePath(orgPath);
  92. // path starts with content://
  93. if (RNFetchBlobFS.isAsset(orgPath)) {
  94. try {
  95. String assetName = orgPath.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
  96. InputStream in = ctx.getAssets().open(assetName);
  97. pipeStreamToSink(in, sink);
  98. } catch (IOException e) {
  99. Log.e("RNFetchBlob", "Failed to create form data asset :" + orgPath + ", " + e.getLocalizedMessage() );
  100. }
  101. }
  102. // data from normal files
  103. else {
  104. File file = new File(RNFetchBlobFS.normalizePath(orgPath));
  105. if(file.exists()) {
  106. FileInputStream fs = new FileInputStream(file);
  107. pipeStreamToSink(fs, sink);
  108. }
  109. else {
  110. Log.e("RNFetchBlob", "Failed to create form data from path :" + orgPath + "file not exists.");
  111. }
  112. }
  113. }
  114. // base64 embedded file content
  115. else {
  116. byte[] b = Base64.decode(data, 0);
  117. sink.write(b);
  118. bytesWritten += b.length;
  119. emitUploadProgress();
  120. }
  121. }
  122. // data field
  123. else {
  124. header += "Content-Disposition: form-data; name=" + name + "\r\n";
  125. header += "Content-Type: " + field.mime + "\r\n\r\n";
  126. sink.write(header.getBytes());
  127. byte[] fieldData = field.data.getBytes();
  128. bytesWritten += fieldData.length;
  129. sink.write(fieldData);
  130. }
  131. // form end
  132. sink.write("\r\n".getBytes());
  133. }
  134. // close the form
  135. byte[] end = ("--" + boundary + "--\r\n").getBytes();
  136. sink.write(end);
  137. }
  138. /**
  139. * Write octet stream data to request body
  140. * @param sink
  141. */
  142. private void writeOctetData(BufferedSink sink) throws IOException {
  143. // upload from storage
  144. if (rawBody.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
  145. String orgPath = rawBody.substring(RNFetchBlobConst.FILE_PREFIX.length());
  146. orgPath = RNFetchBlobFS.normalizePath(orgPath);
  147. // upload file from assets
  148. if (RNFetchBlobFS.isAsset(orgPath)) {
  149. try {
  150. String assetName = orgPath.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
  151. contentLength = RNFetchBlob.RCTContext.getAssets().openFd(assetName).getLength();
  152. requestStream = RNFetchBlob.RCTContext.getAssets().open(assetName);
  153. } catch (IOException e) {
  154. // e.printStackTrace();
  155. }
  156. } else {
  157. File f = new File(RNFetchBlobFS.normalizePath(orgPath));
  158. try {
  159. if(!f.exists())
  160. f.createNewFile();
  161. contentLength = f.length();
  162. requestStream = new FileInputStream(f);
  163. } catch (Exception e) {
  164. // callback.invoke(e.getLocalizedMessage(), null);
  165. }
  166. }
  167. } else {
  168. byte[] bytes = Base64.decode(rawBody, 0);
  169. contentLength = bytes.length;
  170. requestStream = new ByteArrayInputStream(bytes);
  171. }
  172. if(requestStream != null)
  173. pipeStreamToSink(requestStream, sink);
  174. }
  175. /**
  176. * Write data to request body as-is
  177. * @param sink
  178. */
  179. private void writeRawData(BufferedSink sink) throws IOException {
  180. sink.write(rawBody.getBytes());
  181. }
  182. /**
  183. * Pipe input stream to request body output stream
  184. * @param stream The input stream
  185. * @param sink The request body buffer sink
  186. * @throws IOException
  187. */
  188. private void pipeStreamToSink(InputStream stream, BufferedSink sink) throws IOException {
  189. byte [] chunk = new byte[10240];
  190. int read = stream.read(chunk, 0, 10240);
  191. if(read > 0) {
  192. sink.write(chunk, 0, read);
  193. }
  194. bytesWritten += read;
  195. while(read > 0) {
  196. read = stream.read(chunk, 0, 10240);
  197. if(read > 0) {
  198. sink.write(chunk, 0, read);
  199. bytesWritten += read;
  200. emitUploadProgress();
  201. }
  202. }
  203. stream.close();
  204. }
  205. private void writeBufferToSink(byte [] bytes, BufferedSink sink) throws IOException {
  206. bytesWritten += bytes.length;
  207. sink.write(bytes);
  208. emitUploadProgress();
  209. }
  210. /**
  211. * Compute approximate content length for form data
  212. * @return
  213. */
  214. private ArrayList<FormField> countFormDataLength() {
  215. long total = 0;
  216. ArrayList<FormField> list = new ArrayList<>();
  217. ReactApplicationContext ctx = RNFetchBlob.RCTContext;
  218. for(int i = 0;i < form.size(); i++) {
  219. ReadableMap field = form.getMap(i);
  220. list.add(new FormField(field));
  221. String data = field.getString("data");
  222. if (field.hasKey("filename")) {
  223. // upload from storage
  224. if (data.startsWith(RNFetchBlobConst.FILE_PREFIX)) {
  225. String orgPath = data.substring(RNFetchBlobConst.FILE_PREFIX.length());
  226. orgPath = RNFetchBlobFS.normalizePath(orgPath);
  227. // path starts with asset://
  228. if (RNFetchBlobFS.isAsset(orgPath)) {
  229. try {
  230. String assetName = orgPath.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, "");
  231. long length = ctx.getAssets().open(assetName).available();
  232. total += length;
  233. } catch (IOException e) {
  234. }
  235. }
  236. // general files
  237. else {
  238. File file = new File(RNFetchBlobFS.normalizePath(orgPath));
  239. total += file.length();
  240. }
  241. }
  242. // base64 embedded file content
  243. else {
  244. byte[] bytes = Base64.decode(data, 0);
  245. total += bytes.length;
  246. }
  247. }
  248. // data field
  249. else {
  250. total += field.getString("data").length();
  251. }
  252. }
  253. contentLength = total;
  254. return list;
  255. }
  256. /**
  257. * Since ReadableMap could only be access once, we have to store the field into a map for
  258. * repeatedly access.
  259. */
  260. private class FormField {
  261. public String name;
  262. public String filename;
  263. public String mime;
  264. public String data;
  265. public FormField(ReadableMap rawData) {
  266. if(rawData.hasKey("name"))
  267. name = rawData.getString("name");
  268. if(rawData.hasKey("filename"))
  269. filename = rawData.getString("filename");
  270. if(rawData.hasKey("type"))
  271. mime = rawData.getString("type");
  272. else {
  273. mime = filename == null ? "text/plain" : "application/octet-stream";
  274. }
  275. if(rawData.hasKey("data"))
  276. data = rawData.getString("data");
  277. }
  278. }
  279. private void emitUploadProgress() {
  280. WritableMap args = Arguments.createMap();
  281. args.putString("taskId", mTaskId);
  282. args.putString("written", String.valueOf(bytesWritten));
  283. args.putString("total", String.valueOf(contentLength));
  284. // emit event to js context
  285. RNFetchBlob.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  286. .emit(RNFetchBlobConst.EVENT_UPLOAD_PROGRESS, args);
  287. }
  288. private final class ProgressReportingSource extends ForwardingSink {
  289. private long bytesWritten = 0;
  290. private String mTaskId;
  291. private Sink delegate;
  292. public ProgressReportingSource (Sink delegate, String taskId) {
  293. super(delegate);
  294. this.mTaskId = taskId;
  295. this.delegate = delegate;
  296. }
  297. @Override
  298. public void write(Buffer source, long byteCount) throws IOException {
  299. delegate.write(source, byteCount);
  300. // on progress, emit RNFetchBlobProgress upload progress event with ticketId,
  301. // bytesWritten, and totalSize
  302. bytesWritten += byteCount;
  303. WritableMap args = Arguments.createMap();
  304. args.putString("taskId", mTaskId);
  305. args.putString("written", String.valueOf(bytesWritten));
  306. args.putString("total", String.valueOf(contentLength));
  307. // emit event to js context
  308. RNFetchBlob.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  309. .emit(RNFetchBlobConst.EVENT_UPLOAD_PROGRESS, args);
  310. }
  311. }
  312. }