|
19 | 19 | import com.loopj.android.http.AsyncHttpClient;
|
20 | 20 | import com.loopj.android.http.AsyncHttpResponseHandler;
|
21 | 21 | import com.loopj.android.http.Base64;
|
| 22 | +import com.loopj.android.http.MySSLSocketFactory; |
22 | 23 | import com.loopj.android.http.RequestParams;
|
23 | 24 |
|
24 | 25 | import java.io.ByteArrayOutputStream;
|
25 | 26 | import java.io.File;
|
| 27 | +import java.security.KeyStore; |
26 | 28 | import java.util.HashMap;
|
27 | 29 | import java.util.Map;
|
28 | 30 |
|
@@ -158,183 +160,12 @@ public void readStream(String path, String encoding, int bufferSize) {
|
158 | 160 |
|
159 | 161 | @ReactMethod
|
160 | 162 | public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
|
161 |
| - |
162 |
| - RNFetchBlobConfig config = new RNFetchBlobConfig(options); |
163 |
| - |
164 |
| - // use download manager instead of default HTTP implementation |
165 |
| - if(config.addAndroidDownloads != null && config.addAndroidDownloads.hasKey("useDownloadManager")) { |
166 |
| - |
167 |
| - if(config.addAndroidDownloads.getBoolean("useDownloadManager")) { |
168 |
| - Uri uri = Uri.parse(url); |
169 |
| - DownloadManager.Request req = new DownloadManager.Request(uri); |
170 |
| - if(config.path != null) { |
171 |
| - Uri dest = null; |
172 |
| - dest = Uri.parse(config.path); |
173 |
| - req.setDestinationUri(dest); |
174 |
| - } |
175 |
| - // set headers |
176 |
| - ReadableMapKeySetIterator it = headers.keySetIterator(); |
177 |
| - while (it.hasNextKey()) { |
178 |
| - String key = it.nextKey(); |
179 |
| - req.addRequestHeader(key, headers.getString(key)); |
180 |
| - } |
181 |
| - DownloadManager dm = (DownloadManager) this.getReactApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE); |
182 |
| - dm.enqueue(req); |
183 |
| - return; |
184 |
| - } |
185 |
| - |
186 |
| - } |
187 |
| - |
188 |
| - try { |
189 |
| - AsyncHttpClient req = new AsyncHttpClient(); |
190 |
| - |
191 |
| - AbstractHttpEntity entity = null; |
192 |
| - |
193 |
| - // set headers |
194 |
| - ReadableMapKeySetIterator it = headers.keySetIterator(); |
195 |
| - while (it.hasNextKey()) { |
196 |
| - String key = it.nextKey(); |
197 |
| - req.addHeader(key, headers.getString(key)); |
198 |
| - } |
199 |
| - |
200 |
| - // set body for POST and PUT |
201 |
| - if(body != null && method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) { |
202 |
| - |
203 |
| - byte [] blob; |
204 |
| - // upload from storage |
205 |
| - if(body.startsWith(filePathPrefix)) { |
206 |
| - String filePath = body.substring(filePathPrefix.length()); |
207 |
| - entity = new FileEntity(new File(filePath)); |
208 |
| - } |
209 |
| - else { |
210 |
| - blob = Base64.decode(body, 0); |
211 |
| - entity = new ByteArrayEntity(blob); |
212 |
| - } |
213 |
| - entity.setContentType(headers.getString("Content-Type")); |
214 |
| - } |
215 |
| - |
216 |
| - AsyncHttpResponseHandler handler; |
217 |
| - |
218 |
| - // create handler |
219 |
| - if(config.fileCache || config.path != null) { |
220 |
| - handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback); |
221 |
| - // if path format invalid, throw error |
222 |
| - if (!((RNFetchBlobFileHandler)handler).isValid) { |
223 |
| - callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path +"` is not a valid path."); |
224 |
| - return; |
225 |
| - } |
226 |
| - } |
227 |
| - else |
228 |
| - handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback); |
229 |
| - |
230 |
| - // send request |
231 |
| - switch(method.toLowerCase()) { |
232 |
| - case "get" : |
233 |
| - req.get(url, handler); |
234 |
| - break; |
235 |
| - case "post" : |
236 |
| - req.post(this.getReactApplicationContext(), url, entity, "octet-stream", handler); |
237 |
| - break; |
238 |
| - case "put" : |
239 |
| - req.put(this.getReactApplicationContext(), url, entity, "octet-stream",handler); |
240 |
| - break; |
241 |
| - case "delete" : |
242 |
| - req.delete(url, handler); |
243 |
| - break; |
244 |
| - } |
245 |
| - } catch(Exception error) { |
246 |
| - callback.invoke( "RNFetchBlob serialize request data failed: " + error.getMessage() + error.getCause()); |
247 |
| - } |
248 |
| - |
| 163 | + new RNFetchBlobReq(this.getReactApplicationContext(), options, taskId, method, url, headers, body, callback).run(); |
249 | 164 | }
|
250 | 165 |
|
251 | 166 | @ReactMethod
|
252 | 167 | public void fetchBlobForm(ReadableMap options, String taskId, String method, String url, ReadableMap headers, ReadableArray body, final Callback callback) {
|
253 |
| - |
254 |
| - RNFetchBlobConfig config = new RNFetchBlobConfig(options); |
255 |
| - try { |
256 |
| - |
257 |
| - AsyncHttpClient req = new AsyncHttpClient(); |
258 |
| - |
259 |
| - HttpEntity entity = null; |
260 |
| - |
261 |
| - // set headers |
262 |
| - if(headers != null) { |
263 |
| - ReadableMapKeySetIterator it = headers.keySetIterator(); |
264 |
| - while (it.hasNextKey()) { |
265 |
| - String key = it.nextKey(); |
266 |
| - req.addHeader(key, headers.getString(key)); |
267 |
| - } |
268 |
| - } |
269 |
| - |
270 |
| - // set body for POST and PUT |
271 |
| - if(body != null && method.equalsIgnoreCase("post") || method.equalsIgnoreCase("put")) { |
272 |
| - Long tsLong = System.currentTimeMillis()/1000; |
273 |
| - String ts = tsLong.toString(); |
274 |
| - String boundary = "RNFetchBlob".concat(ts); |
275 |
| - MultipartEntityBuilder form = MultipartEntityBuilder.create(); |
276 |
| - form.setBoundary(boundary); |
277 |
| - for( int i = 0; i< body.size(); i++) { |
278 |
| - ReadableMap map = body.getMap(i); |
279 |
| - String name = map.getString("name"); |
280 |
| - if(!map.hasKey("data")) |
281 |
| - continue; |
282 |
| - String data = map.getString("data"); |
283 |
| - // file field |
284 |
| - if(map.hasKey("filename")) { |
285 |
| - String filename = map.getString("filename"); |
286 |
| - // upload from storage |
287 |
| - if(data.startsWith(filePathPrefix)) { |
288 |
| - File file = new File(data.substring(filePathPrefix.length())); |
289 |
| - form.addBinaryBody(name, file, ContentType.APPLICATION_OCTET_STREAM, filename); |
290 |
| - } |
291 |
| - // base64 embedded file content |
292 |
| - else { |
293 |
| - form.addBinaryBody(name, Base64.decode(data, 0), ContentType.APPLICATION_OCTET_STREAM, filename); |
294 |
| - } |
295 |
| - } |
296 |
| - // data field |
297 |
| - else { |
298 |
| - form.addTextBody(name, map.getString("data")); |
299 |
| - } |
300 |
| - } |
301 |
| - entity = form.build(); |
302 |
| - req.addHeader("Content-Type", headers.getString("Content-Type") + "; charset=utf8; boundary=" + boundary); |
303 |
| - } |
304 |
| - |
305 |
| - AsyncHttpResponseHandler handler; |
306 |
| - |
307 |
| - // create handler |
308 |
| - if(config.fileCache || config.path != null) { |
309 |
| - handler = new RNFetchBlobFileHandler(this.getReactApplicationContext(), taskId, config, callback); |
310 |
| - // if path format invalid, throw error |
311 |
| - if (!((RNFetchBlobFileHandler)handler).isValid) { |
312 |
| - callback.invoke("RNFetchBlob fetch error, configuration path `"+ config.path +"` is not a valid path."); |
313 |
| - return; |
314 |
| - } |
315 |
| - } |
316 |
| - else |
317 |
| - handler = new RNFetchBlobBinaryHandler(this.getReactApplicationContext(), taskId, callback); |
318 |
| - |
319 |
| - // send request |
320 |
| - switch(method.toLowerCase()) { |
321 |
| - case "get" : |
322 |
| - req.get(url, handler); |
323 |
| - break; |
324 |
| - case "post" : |
325 |
| - req.post(this.getReactApplicationContext(), url, entity, "multipart/form-data; charset=utf8", handler); |
326 |
| - break; |
327 |
| - case "put" : |
328 |
| - req.put(this.getReactApplicationContext(), url, entity, "multipart/form-data",handler); |
329 |
| - break; |
330 |
| - case "delete" : |
331 |
| - req.delete(url, handler); |
332 |
| - break; |
333 |
| - } |
334 |
| - } catch(Exception error) { |
335 |
| - callback.invoke( "RNFetchBlob serialize request data failed: " + error.getMessage() + error.getCause()); |
336 |
| - } |
337 |
| - |
| 168 | + new RNFetchBlobReq(this.getReactApplicationContext(), options, taskId, method, url, headers, body, callback).run(); |
338 | 169 | }
|
339 | 170 |
|
340 | 171 | }
|
|
0 commit comments