|
@@ -0,0 +1,231 @@
|
|
1
|
+package com.RNFetchBlob;
|
|
2
|
+
|
|
3
|
+import android.app.DownloadManager;
|
|
4
|
+import android.content.Context;
|
|
5
|
+import android.net.Uri;
|
|
6
|
+
|
|
7
|
+import com.facebook.react.bridge.Callback;
|
|
8
|
+import com.facebook.react.bridge.ReactApplicationContext;
|
|
9
|
+import com.facebook.react.bridge.ReadableArray;
|
|
10
|
+import com.facebook.react.bridge.ReadableMap;
|
|
11
|
+import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
12
|
+import com.loopj.android.http.AsyncHttpClient;
|
|
13
|
+import com.loopj.android.http.AsyncHttpResponseHandler;
|
|
14
|
+import com.loopj.android.http.Base64;
|
|
15
|
+import com.loopj.android.http.MySSLSocketFactory;
|
|
16
|
+
|
|
17
|
+import java.io.File;
|
|
18
|
+import java.nio.charset.Charset;
|
|
19
|
+import java.security.KeyStore;
|
|
20
|
+
|
|
21
|
+import cz.msebera.android.httpclient.HttpEntity;
|
|
22
|
+import cz.msebera.android.httpclient.entity.AbstractHttpEntity;
|
|
23
|
+import cz.msebera.android.httpclient.entity.ByteArrayEntity;
|
|
24
|
+import cz.msebera.android.httpclient.entity.ContentType;
|
|
25
|
+import cz.msebera.android.httpclient.entity.FileEntity;
|
|
26
|
+import cz.msebera.android.httpclient.entity.mime.MultipartEntityBuilder;
|
|
27
|
+
|
|
28
|
+/**
|
|
29
|
+ * Created by wkh237 on 2016/6/21.
|
|
30
|
+ */
|
|
31
|
+public class RNFetchBlobReq implements Runnable{
|
|
32
|
+
|
|
33
|
+ final String filePathPrefix = "RNFetchBlob-file://";
|
|
34
|
+ ReactApplicationContext ctx;
|
|
35
|
+ RNFetchBlobConfig options;
|
|
36
|
+ String taskId;
|
|
37
|
+ String method;
|
|
38
|
+ String url;
|
|
39
|
+ String boundary;
|
|
40
|
+ ReadableMap headers;
|
|
41
|
+ Callback callback;
|
|
42
|
+ HttpEntity entity;
|
|
43
|
+ AsyncHttpClient req;
|
|
44
|
+ String type;
|
|
45
|
+
|
|
46
|
+ public RNFetchBlobReq(ReactApplicationContext ctx, ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
|
|
47
|
+ this.ctx = ctx;
|
|
48
|
+ this.method = method;
|
|
49
|
+ this.options= new RNFetchBlobConfig(options);
|
|
50
|
+ this.taskId = taskId;
|
|
51
|
+ this.url = url;
|
|
52
|
+ this.headers = headers;
|
|
53
|
+ this.callback = callback;
|
|
54
|
+ this.req = new AsyncHttpClient();
|
|
55
|
+ if(body != null) {
|
|
56
|
+ type = "octet";
|
|
57
|
+ buildEntity(body);
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ public RNFetchBlobReq(ReactApplicationContext ctx, ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
|
|
62
|
+ this.ctx = ctx;
|
|
63
|
+ this.method = method;
|
|
64
|
+ this.options= new RNFetchBlobConfig(options);
|
|
65
|
+ this.taskId = taskId;
|
|
66
|
+ this.url = url;
|
|
67
|
+ this.headers = headers;
|
|
68
|
+ this.callback = callback;
|
|
69
|
+ this.req = new AsyncHttpClient();
|
|
70
|
+ if(body != null) {
|
|
71
|
+ type = "form";
|
|
72
|
+ buildFormEntity(body);
|
|
73
|
+ }
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ @Override
|
|
77
|
+ public void run() {
|
|
78
|
+
|
|
79
|
+ // use download manager instead of default HTTP implementation
|
|
80
|
+ if(options.addAndroidDownloads != null && options.addAndroidDownloads.hasKey("useDownloadManager")) {
|
|
81
|
+
|
|
82
|
+ if(options.addAndroidDownloads.getBoolean("useDownloadManager")) {
|
|
83
|
+ Uri uri = Uri.parse(url);
|
|
84
|
+ DownloadManager.Request req = new DownloadManager.Request(uri);
|
|
85
|
+ if(options.path != null) {
|
|
86
|
+ Uri dest = null;
|
|
87
|
+ dest = Uri.parse(options.path);
|
|
88
|
+ req.setDestinationUri(dest);
|
|
89
|
+ }
|
|
90
|
+ // set headers
|
|
91
|
+ ReadableMapKeySetIterator it = headers.keySetIterator();
|
|
92
|
+ while (it.hasNextKey()) {
|
|
93
|
+ String key = it.nextKey();
|
|
94
|
+ req.addRequestHeader(key, headers.getString(key));
|
|
95
|
+ }
|
|
96
|
+ DownloadManager dm = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
97
|
+ dm.enqueue(req);
|
|
98
|
+ return;
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ try {
|
|
104
|
+
|
|
105
|
+ // use trusty SSL socket
|
|
106
|
+ if(this.options.trusty) {
|
|
107
|
+ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
108
|
+ trustStore.load(null, null);
|
|
109
|
+ MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
|
|
110
|
+ sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
|
111
|
+ req.setSSLSocketFactory(sf);
|
|
112
|
+ }
|
|
113
|
+
|
|
114
|
+ // set headers
|
|
115
|
+ if(headers != null) {
|
|
116
|
+ ReadableMapKeySetIterator it = headers.keySetIterator();
|
|
117
|
+ while (it.hasNextKey()) {
|
|
118
|
+ String key = it.nextKey();
|
|
119
|
+ req.addHeader(key, headers.getString(key));
|
|
120
|
+ }
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ if(type != null)
|
|
124
|
+ {
|
|
125
|
+ if(type == "octet")
|
|
126
|
+ req.addHeader("Content-Type", "application/octet-stream");
|
|
127
|
+ else if(type == "form")
|
|
128
|
+ req.addHeader("Content-Type", "multipart/form-data; charset=utf8; boundary="+boundary);
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ AsyncHttpResponseHandler handler;
|
|
132
|
+
|
|
133
|
+ // create handler
|
|
134
|
+ if(options.fileCache || options.path != null) {
|
|
135
|
+ handler = new RNFetchBlobFileHandler(ctx, taskId, options, callback);
|
|
136
|
+ // if path format invalid, throw error
|
|
137
|
+ if (!((RNFetchBlobFileHandler)handler).isValid) {
|
|
138
|
+ callback.invoke("RNFetchBlob fetch error, configuration path `"+ options.path +"` is not a valid path.");
|
|
139
|
+ return;
|
|
140
|
+ }
|
|
141
|
+ }
|
|
142
|
+ else
|
|
143
|
+ handler = new RNFetchBlobBinaryHandler(this.ctx, taskId, callback);
|
|
144
|
+
|
|
145
|
+ // send request
|
|
146
|
+ switch(method.toLowerCase()) {
|
|
147
|
+ case "get" :
|
|
148
|
+ req.get(url, handler);
|
|
149
|
+ break;
|
|
150
|
+ case "post" :
|
|
151
|
+ if(this.type == null || this.type.equalsIgnoreCase("octet"))
|
|
152
|
+ req.post(ctx, url, entity, "application/octet-stream", handler);
|
|
153
|
+ else
|
|
154
|
+ req.post(ctx, url, entity, "multipart/form-data", handler);
|
|
155
|
+ break;
|
|
156
|
+ case "put" :
|
|
157
|
+ if(this.type == null || this.type.equalsIgnoreCase("octet"))
|
|
158
|
+ req.post(ctx, url, entity, "application/octet-stream", handler);
|
|
159
|
+ else
|
|
160
|
+ req.post(ctx, url, entity, "multipart/form-data", handler);
|
|
161
|
+ break;
|
|
162
|
+ case "delete" :
|
|
163
|
+ req.delete(url, handler);
|
|
164
|
+ break;
|
|
165
|
+ }
|
|
166
|
+ } catch(Exception error) {
|
|
167
|
+ callback.invoke( "RNFetchBlob serialize request data failed: " + error.getMessage() + error.getCause());
|
|
168
|
+ }
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ /**
|
|
172
|
+ * Build Mutipart body
|
|
173
|
+ * @param body Body in array format
|
|
174
|
+ */
|
|
175
|
+ void buildFormEntity(ReadableArray body) {
|
|
176
|
+ if(body != null && (method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put"))) {
|
|
177
|
+ Long tsLong = System.currentTimeMillis()/1000;
|
|
178
|
+ String ts = tsLong.toString();
|
|
179
|
+ boundary = "RNFetchBlob".concat(ts);
|
|
180
|
+ MultipartEntityBuilder form = MultipartEntityBuilder.create();
|
|
181
|
+ form.setBoundary(boundary);
|
|
182
|
+ for( int i = 0; i< body.size(); i++) {
|
|
183
|
+ ReadableMap map = body.getMap(i);
|
|
184
|
+ String name = map.getString("name");
|
|
185
|
+ if(!map.hasKey("data"))
|
|
186
|
+ continue;
|
|
187
|
+ String data = map.getString("data");
|
|
188
|
+ // file field
|
|
189
|
+ if(map.hasKey("filename")) {
|
|
190
|
+ String filename = map.getString("filename");
|
|
191
|
+ // upload from storage
|
|
192
|
+ if(data.startsWith(filePathPrefix)) {
|
|
193
|
+ File file = new File(data.substring(filePathPrefix.length()));
|
|
194
|
+ form.addBinaryBody(name, file, ContentType.APPLICATION_OCTET_STREAM, filename);
|
|
195
|
+ }
|
|
196
|
+ // base64 embedded file content
|
|
197
|
+ else {
|
|
198
|
+ form.addBinaryBody(name, Base64.decode(data, 0), ContentType.APPLICATION_OCTET_STREAM, filename);
|
|
199
|
+ }
|
|
200
|
+ }
|
|
201
|
+ // data field
|
|
202
|
+ else {
|
|
203
|
+ form.addTextBody(name, map.getString("data"));
|
|
204
|
+ }
|
|
205
|
+ }
|
|
206
|
+ entity = form.build();
|
|
207
|
+ }
|
|
208
|
+ }
|
|
209
|
+
|
|
210
|
+ /**
|
|
211
|
+ * Build Octet-Stream body
|
|
212
|
+ * @param body Body in string format
|
|
213
|
+ */
|
|
214
|
+ void buildEntity(String body) {
|
|
215
|
+ // set body for POST and PUT
|
|
216
|
+ if(body != null && (method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put"))) {
|
|
217
|
+
|
|
218
|
+ byte [] blob;
|
|
219
|
+ // upload from storage
|
|
220
|
+ if(body.startsWith(filePathPrefix)) {
|
|
221
|
+ String filePath = body.substring(filePathPrefix.length());
|
|
222
|
+ entity = new FileEntity(new File(filePath));
|
|
223
|
+ }
|
|
224
|
+ else {
|
|
225
|
+ blob = Base64.decode(body, 0);
|
|
226
|
+ entity = new ByteArrayEntity(blob);
|
|
227
|
+ }
|
|
228
|
+ }
|
|
229
|
+
|
|
230
|
+ }
|
|
231
|
+}
|