|
@@ -17,7 +17,6 @@ import com.facebook.react.bridge.ReactApplicationContext;
|
17
|
17
|
import com.facebook.react.bridge.ReadableArray;
|
18
|
18
|
import com.facebook.react.bridge.ReadableMap;
|
19
|
19
|
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
20
|
|
-import com.facebook.react.bridge.WritableArray;
|
21
|
20
|
import com.facebook.react.bridge.WritableMap;
|
22
|
21
|
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
23
|
22
|
|
|
@@ -33,7 +32,6 @@ import java.nio.ByteBuffer;
|
33
|
32
|
import java.nio.charset.CharacterCodingException;
|
34
|
33
|
import java.nio.charset.Charset;
|
35
|
34
|
import java.nio.charset.CharsetEncoder;
|
36
|
|
-import java.util.ArrayList;
|
37
|
35
|
import java.util.HashMap;
|
38
|
36
|
import java.util.concurrent.TimeUnit;
|
39
|
37
|
|
|
@@ -74,7 +72,6 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
74
|
72
|
|
75
|
73
|
ReactApplicationContext ctx;
|
76
|
74
|
RNFetchBlobConfig options;
|
77
|
|
- ArrayList<String> redirects = new ArrayList<>();
|
78
|
75
|
String taskId;
|
79
|
76
|
String method;
|
80
|
77
|
String url;
|
|
@@ -107,7 +104,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
107
|
104
|
responseType = ResponseType.KeepInMemory;
|
108
|
105
|
|
109
|
106
|
|
110
|
|
- if (body != null)
|
|
107
|
+ if (body != null)
|
111
|
108
|
requestType = RequestType.SingleFile;
|
112
|
109
|
else if (arrayBody != null)
|
113
|
110
|
requestType = RequestType.Form;
|
|
@@ -159,21 +156,21 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
159
|
156
|
|
160
|
157
|
// find cached result if `key` property exists
|
161
|
158
|
String cacheKey = this.taskId;
|
162
|
|
- String ext = this.options.appendExt.isEmpty() ? "." + this.options.appendExt : "";
|
|
159
|
+ String ext = this.options.appendExt.isEmpty() ? "." + this.options.appendExt : "";
|
163
|
160
|
|
164
|
|
- if (this.options.key != null) {
|
165
|
|
- cacheKey = RNFetchBlobUtils.getMD5(this.options.key);
|
166
|
|
- if (cacheKey == null) {
|
167
|
|
- cacheKey = this.taskId;
|
168
|
|
- }
|
|
161
|
+ if (this.options.key != null) {
|
|
162
|
+ cacheKey = RNFetchBlobUtils.getMD5(this.options.key);
|
|
163
|
+ if (cacheKey == null) {
|
|
164
|
+ cacheKey = this.taskId;
|
|
165
|
+ }
|
169
|
166
|
|
170
|
|
- File file = new File(RNFetchBlobFS.getTmpPath(RNFetchBlob.RCTContext, cacheKey) + ext);
|
|
167
|
+ File file = new File(RNFetchBlobFS.getTmpPath(RNFetchBlob.RCTContext, cacheKey) + ext);
|
171
|
168
|
|
172
|
|
- if (file.exists()) {
|
173
|
|
- callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, file.getAbsolutePath());
|
174
|
|
- return;
|
175
|
|
- }
|
176
|
|
- }
|
|
169
|
+ if (file.exists()) {
|
|
170
|
+ callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, file.getAbsolutePath());
|
|
171
|
+ return;
|
|
172
|
+ }
|
|
173
|
+ }
|
177
|
174
|
|
178
|
175
|
if(this.options.path != null)
|
179
|
176
|
this.destPath = this.options.path;
|
|
@@ -239,35 +236,33 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
239
|
236
|
requestType = RequestType.WithoutBody;
|
240
|
237
|
}
|
241
|
238
|
|
|
239
|
+ boolean isChunkedRequest = getHeaderIgnoreCases(mheaders, "Transfer-Encoding").equalsIgnoreCase("chunked");
|
242
|
240
|
|
243
|
241
|
// set request body
|
244
|
242
|
switch (requestType) {
|
245
|
243
|
case SingleFile:
|
246
|
|
- requestBody = new RNFetchBlobBody(
|
247
|
|
- taskId,
|
248
|
|
- requestType,
|
249
|
|
- rawRequestBody,
|
250
|
|
- MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type"))
|
251
|
|
- );
|
|
244
|
+ requestBody = new RNFetchBlobBody(taskId)
|
|
245
|
+ .chunkedEncoding(isChunkedRequest)
|
|
246
|
+ .setRequestType(requestType)
|
|
247
|
+ .setBody(rawRequestBody)
|
|
248
|
+ .setMIME(MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type")));
|
252
|
249
|
builder.method(method, requestBody);
|
253
|
250
|
break;
|
254
|
251
|
case AsIs:
|
255
|
|
- requestBody = new RNFetchBlobBody(
|
256
|
|
- taskId,
|
257
|
|
- requestType,
|
258
|
|
- rawRequestBody,
|
259
|
|
- MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type"))
|
260
|
|
- );
|
|
252
|
+ requestBody = new RNFetchBlobBody(taskId)
|
|
253
|
+ .chunkedEncoding(isChunkedRequest)
|
|
254
|
+ .setRequestType(requestType)
|
|
255
|
+ .setBody(rawRequestBody)
|
|
256
|
+ .setMIME(MediaType.parse(getHeaderIgnoreCases(mheaders, "content-type")));
|
261
|
257
|
builder.method(method, requestBody);
|
262
|
258
|
break;
|
263
|
259
|
case Form:
|
264
|
260
|
String boundary = "RNFetchBlob-" + taskId;
|
265
|
|
- requestBody = new RNFetchBlobBody(
|
266
|
|
- taskId,
|
267
|
|
- requestType,
|
268
|
|
- rawRequestBodyArray,
|
269
|
|
- MediaType.parse("multipart/form-data; boundary="+ boundary)
|
270
|
|
- );
|
|
261
|
+ requestBody = new RNFetchBlobBody(taskId)
|
|
262
|
+ .chunkedEncoding(isChunkedRequest)
|
|
263
|
+ .setRequestType(requestType)
|
|
264
|
+ .setBody(rawRequestBodyArray)
|
|
265
|
+ .setMIME(MediaType.parse("multipart/form-data; boundary="+ boundary));
|
271
|
266
|
builder.method(method, requestBody);
|
272
|
267
|
break;
|
273
|
268
|
|
|
@@ -283,15 +278,6 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
283
|
278
|
|
284
|
279
|
final Request req = builder.build();
|
285
|
280
|
|
286
|
|
- // intercept network redirections
|
287
|
|
- clientBuilder.addNetworkInterceptor(new Interceptor() {
|
288
|
|
- @Override
|
289
|
|
- public Response intercept(Chain chain) throws IOException {
|
290
|
|
- redirects.add(chain.request().url().toString());
|
291
|
|
- return chain.proceed(chain.request());
|
292
|
|
- }
|
293
|
|
- });
|
294
|
|
-
|
295
|
281
|
// Add request interceptor for upload progress event
|
296
|
282
|
clientBuilder.addInterceptor(new Interceptor() {
|
297
|
283
|
@Override
|
|
@@ -468,7 +454,9 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
468
|
454
|
// It uses customized response body which is able to report download progress
|
469
|
455
|
// and write response data to destination path.
|
470
|
456
|
resp.body().bytes();
|
471
|
|
- } catch (Exception ignored) { }
|
|
457
|
+ } catch (Exception ignored) {
|
|
458
|
+ ignored.printStackTrace();
|
|
459
|
+ }
|
472
|
460
|
callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
|
473
|
461
|
break;
|
474
|
462
|
default:
|
|
@@ -521,11 +509,6 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
521
|
509
|
headers.putString(resp.headers().name(i), resp.headers().value(i));
|
522
|
510
|
}
|
523
|
511
|
info.putMap("headers", headers);
|
524
|
|
- WritableArray redirectList = Arguments.createArray();
|
525
|
|
- for(String r : redirects) {
|
526
|
|
- redirectList.pushString(r);
|
527
|
|
- }
|
528
|
|
- info.putArray("redirects", redirectList);
|
529
|
512
|
Headers h = resp.headers();
|
530
|
513
|
if(isBlobResp) {
|
531
|
514
|
info.putString("respType", "blob");
|