Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit ee0f38e

Browse files
committed
Fix #66
1 parent fbdebf0 commit ee0f38e

File tree

11 files changed

+126
-36
lines changed

11 files changed

+126
-36
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
public class RNFetchBlob extends ReactContextBaseJavaModule {
1414

15-
String filePathPrefix = "RNFetchBlob-file://";
1615
static ReactApplicationContext RCTContext;
1716

1817
public RNFetchBlob(ReactApplicationContext reactContext) {
@@ -165,6 +164,16 @@ public void slice(String src, String dest, int start, int end, String encode, Ca
165164
RNFetchBlobFS.slice(src, dest, start, end, encode, callback);
166165
}
167166

167+
@ReactMethod
168+
public void enableProgressReport(String taskId) {
169+
RNFetchBlobReq.progressReport.put(taskId, true);
170+
}
171+
172+
@ReactMethod
173+
public void enableUploadProgressReport(String taskId) {
174+
RNFetchBlobReq.uploadProgressReport.put(taskId, true);
175+
}
176+
168177
@ReactMethod
169178
public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
170179
new RNFetchBlobReq(options, taskId, method, url, headers, body, null, callback).run();

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobBody.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,11 @@ public void write(Buffer source, long byteCount) throws IOException {
335335
args.putString("written", String.valueOf(bytesWritten));
336336
args.putString("total", String.valueOf(contentLength));
337337

338-
// emit event to js context
339-
RNFetchBlob.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
340-
.emit(RNFetchBlobConst.EVENT_UPLOAD_PROGRESS, args);
338+
if(RNFetchBlobReq.isReportUploadProgress(mTaskId)) {
339+
// emit event to js context
340+
RNFetchBlob.RCTContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
341+
.emit(RNFetchBlobConst.EVENT_UPLOAD_PROGRESS, args);
342+
}
341343
}
342344
}
343345
}

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ enum ResponseType {
6464
FileStorage
6565
};
6666

67-
static HashMap<String, Call> taskTable = new HashMap<>();
67+
public static HashMap<String, Call> taskTable = new HashMap<>();
68+
static HashMap<String, Boolean> progressReport = new HashMap<>();
69+
static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
6870

6971
MediaType contentType = RNFetchBlobConst.MIME_OCTET;
7072
ReactApplicationContext ctx;
@@ -81,8 +83,10 @@ enum ResponseType {
8183
long downloadManagerId;
8284
RequestType requestType;
8385
ResponseType responseType;
84-
boolean timeout = false;
8586
WritableMap respInfo;
87+
boolean timeout = false;
88+
public boolean reportProgress = false;
89+
public boolean reportUploadProgress = false;
8690

8791
public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
8892
this.method = method;
@@ -304,7 +308,7 @@ public Response intercept(Chain chain) throws IOException {
304308
}
305309

306310
OkHttpClient client = clientBuilder.build();
307-
Call call = client.newCall(req);
311+
Call call = client.newCall(req);
308312
taskTable.put(taskId, call);
309313
call.enqueue(new okhttp3.Callback() {
310314

@@ -321,6 +325,7 @@ public void onFailure(Call call, IOException e) {
321325
}
322326
else
323327
callback.invoke(e.getLocalizedMessage(), respInfo, null);
328+
removeTaskInfo();
324329
}
325330

326331
@Override
@@ -356,6 +361,18 @@ public void onResponse(Call call, Response response) throws IOException {
356361
}
357362
}
358363

364+
/**
365+
* Remove cached information of the HTTP task
366+
*/
367+
private void removeTaskInfo() {
368+
if(taskTable.containsKey(taskId))
369+
taskTable.remove(taskId);
370+
if(uploadProgressReport.containsKey(taskId))
371+
uploadProgressReport.remove(taskId);
372+
if(progressReport.containsKey(taskId))
373+
progressReport.remove(taskId);
374+
}
375+
359376
/**
360377
* Send response data back to javascript context.
361378
* @param resp OkHttp response object
@@ -408,8 +425,17 @@ private void done(Response resp) {
408425
}
409426
break;
410427
}
411-
if(taskTable.containsKey(taskId))
412-
taskTable.remove(taskId);
428+
removeTaskInfo();
429+
}
430+
431+
public static boolean isReportProgress(String taskId) {
432+
if(!progressReport.containsKey(taskId)) return false;
433+
return progressReport.get(taskId);
434+
}
435+
436+
public static boolean isReportUploadProgress(String taskId) {
437+
if(!uploadProgressReport.containsKey(taskId)) return false;
438+
return uploadProgressReport.get(taskId);
413439
}
414440

415441
private WritableMap getResponseInfo(Response resp) {

src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobDefaultResp.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.RNFetchBlob.Response;
22

33
import com.RNFetchBlob.RNFetchBlob;
4+
import com.RNFetchBlob.RNFetchBlobReq;
45
import com.facebook.react.bridge.Arguments;
56
import com.facebook.react.bridge.ReactApplicationContext;
67
import com.facebook.react.bridge.WritableMap;
@@ -64,12 +65,14 @@ public long read(Buffer sink, long byteCount) throws IOException {
6465

6566
long read = mOriginalSource.read(sink, byteCount);
6667
bytesRead += read > 0 ? read : 0;
67-
WritableMap args = Arguments.createMap();
68-
args.putString("taskId", mTaskId);
69-
args.putString("written", String.valueOf(bytesRead));
70-
args.putString("total", String.valueOf(contentLength()));
71-
rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
72-
.emit("RNFetchBlobProgress", args);
68+
if(RNFetchBlobReq.isReportProgress(mTaskId)) {
69+
WritableMap args = Arguments.createMap();
70+
args.putString("taskId", mTaskId);
71+
args.putString("written", String.valueOf(bytesRead));
72+
args.putString("total", String.valueOf(contentLength()));
73+
rctContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
74+
.emit("RNFetchBlobProgress", args);
75+
}
7376
return read;
7477
}
7578

src/android/src/main/java/com/RNFetchBlob/Response/RNFetchBlobFileResp.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.util.Log;
44

5+
import com.RNFetchBlob.RNFetchBlobReq;
56
import com.facebook.react.bridge.Arguments;
67
import com.facebook.react.bridge.ReactApplicationContext;
78
import com.facebook.react.bridge.WritableMap;
@@ -69,8 +70,10 @@ public long read(Buffer sink, long byteCount) throws IOException {
6970
long read = originalBody.byteStream().read(bytes, 0, (int) byteCount);
7071
bytesDownloaded += read > 0 ? read : 0;
7172
Log.i("bytes downloaded", String.valueOf(byteCount) +"/"+ String.valueOf(read) + "=" + String.valueOf(bytesDownloaded));
72-
if(read > 0) {
73+
if(read > 0 ) {
7374
ofStream.write(bytes, 0, (int) read);
75+
}
76+
if(RNFetchBlobReq.isReportProgress(mTaskId)) {
7477
WritableMap args = Arguments.createMap();
7578
args.putString("taskId", mTaskId);
7679
args.putString("written", String.valueOf(bytesDownloaded));

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,12 @@ function fetch(...args:any):Promise {
175175
// method for register progress event handler and cancel request.
176176
promise.progress = (fn) => {
177177
promise.onProgress = fn
178+
RNFetchBlob.enableProgressReport(taskId)
178179
return promise
179180
}
180181
promise.uploadProgress = (fn) => {
181182
promise.onUploadProgress = fn
183+
RNFetchBlob.enableUploadProgressReport(taskId)
182184
return promise
183185
}
184186
promise.stateChange = (fn) => {

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ - (NSDictionary *)constantsToExport
358358

359359
}
360360

361+
RCT_EXPORT_METHOD(enableProgressReport:(NSString *)taskId {
362+
[RNFetchBlobNetwork enableProgressReport:taskId];
363+
})
364+
365+
RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId {
366+
[RNFetchBlobNetwork enableUploadProgress:taskId];
367+
})
368+
361369
RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(NSNumber *)start end:(NSNumber *)end resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
362370
{
363371
[RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];

src/ios/RNFetchBlobConst.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ extern NSString *const FS_EVENT_END;
3636
extern NSString *const FS_EVENT_WARN;
3737
extern NSString *const FS_EVENT_ERROR;
3838

39+
extern NSString *const KEY_REPORT_PROGRESS;
40+
extern NSString *const KEY_REPORT_UPLOAD_PROGRESS;
41+
3942

4043

4144
#endif /* RNFetchBlobConst_h */

src/ios/RNFetchBlobConst.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@
3030
extern NSString *const FS_EVENT_END = @"end";
3131
extern NSString *const FS_EVENT_WARN = @"warn";
3232
extern NSString *const FS_EVENT_ERROR = @"error";
33+
34+
extern NSString *const KEY_REPORT_PROGRESS = @"reportProgress";
35+
extern NSString *const KEY_REPORT_UPLOAD_PROGRESS = @"reportUploadProgress";

src/ios/RNFetchBlobNetwork.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
3636

3737
+ (NSMutableDictionary * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
3838
+ (void) cancelRequest:(NSString *)taskId;
39+
+ (void) enableProgressReport:(NSString *) taskId;
40+
+ (void) enableUploadProgress:(NSString *) taskId;
3941
- (void) sendRequest:(NSDictionary * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
4042

4143

0 commit comments

Comments
 (0)