|
@@ -24,6 +24,8 @@
|
24
|
24
|
////////////////////////////////////////
|
25
|
25
|
|
26
|
26
|
NSMutableDictionary * taskTable;
|
|
27
|
+NSMutableDictionary * progressTable;
|
|
28
|
+NSMutableDictionary * uploadProgressTable;
|
27
|
29
|
|
28
|
30
|
@interface RNFetchBlobNetwork ()
|
29
|
31
|
{
|
|
@@ -61,12 +63,30 @@ NSOperationQueue *taskQueue;
|
61
|
63
|
if(taskTable == nil) {
|
62
|
64
|
taskTable = [[NSMutableDictionary alloc] init];
|
63
|
65
|
}
|
|
66
|
+ if(progressTable == nil)
|
|
67
|
+ {
|
|
68
|
+ progressTable = [[NSMutableDictionary alloc] init];
|
|
69
|
+ }
|
|
70
|
+ if(uploadProgressTable == nil)
|
|
71
|
+ {
|
|
72
|
+ uploadProgressTable = [[NSMutableDictionary alloc] init];
|
|
73
|
+ }
|
64
|
74
|
return self;
|
65
|
75
|
}
|
66
|
76
|
|
|
77
|
++ (void) enableProgressReport:(NSString *) taskId
|
|
78
|
+{
|
|
79
|
+ [progressTable setValue:@YES forKey:taskId];
|
|
80
|
+}
|
|
81
|
+
|
|
82
|
++ (void) enableUploadProgress:(NSString *) taskId
|
|
83
|
+{
|
|
84
|
+ [uploadProgressTable setValue:@YES forKey:taskId];
|
|
85
|
+}
|
67
|
86
|
|
68
|
87
|
// removing case from headers
|
69
|
|
-+ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
|
|
88
|
++ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers
|
|
89
|
+{
|
70
|
90
|
|
71
|
91
|
NSMutableDictionary * mheaders = [[NSMutableDictionary alloc]init];
|
72
|
92
|
for(NSString * key in headers) {
|
|
@@ -149,7 +169,7 @@ NSOperationQueue *taskQueue;
|
149
|
169
|
respFile = NO;
|
150
|
170
|
}
|
151
|
171
|
NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
|
152
|
|
- [taskTable setValue:task forKey:taskId];
|
|
172
|
+ [taskTable setValue:@{ @"task": task, KEY_REPORT_PROGRESS : @NO, KEY_REPORT_UPLOAD_PROGRESS : @NO} forKey:taskId];
|
153
|
173
|
[task resume];
|
154
|
174
|
|
155
|
175
|
// network status indicator
|
|
@@ -248,15 +268,18 @@ NSOperationQueue *taskQueue;
|
248
|
268
|
{
|
249
|
269
|
[writeStream write:[data bytes] maxLength:[data length]];
|
250
|
270
|
}
|
251
|
|
-
|
252
|
|
- [self.bridge.eventDispatcher
|
253
|
|
- sendDeviceEventWithName:@"RNFetchBlobProgress"
|
254
|
|
- body:@{
|
255
|
|
- @"taskId": taskId,
|
256
|
|
- @"written": [NSString stringWithFormat:@"%d", receivedBytes],
|
257
|
|
- @"total": [NSString stringWithFormat:@"%d", expectedBytes]
|
258
|
|
- }
|
259
|
|
- ];
|
|
271
|
+
|
|
272
|
+ if([progressTable valueForKey:taskId] == @YES)
|
|
273
|
+ {
|
|
274
|
+ [self.bridge.eventDispatcher
|
|
275
|
+ sendDeviceEventWithName:@"RNFetchBlobProgress"
|
|
276
|
+ body:@{
|
|
277
|
+ @"taskId": taskId,
|
|
278
|
+ @"written": [NSString stringWithFormat:@"%d", receivedBytes],
|
|
279
|
+ @"total": [NSString stringWithFormat:@"%d", expectedBytes]
|
|
280
|
+ }
|
|
281
|
+ ];
|
|
282
|
+ }
|
260
|
283
|
|
261
|
284
|
}
|
262
|
285
|
|
|
@@ -286,24 +309,30 @@ NSOperationQueue *taskQueue;
|
286
|
309
|
[respData base64EncodedStringWithOptions:0]
|
287
|
310
|
]);
|
288
|
311
|
}
|
|
312
|
+
|
|
313
|
+ [taskTable removeObjectForKey:taskId];
|
|
314
|
+ [uploadProgressTable removeObjectForKey:taskId];
|
|
315
|
+ [progressTable removeObjectForKey:taskId];
|
289
|
316
|
}
|
290
|
317
|
|
291
|
318
|
// upload progress handler
|
292
|
319
|
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesWritten totalBytesExpectedToSend:(int64_t)totalBytesExpectedToWrite
|
293
|
320
|
{
|
294
|
|
- [self.bridge.eventDispatcher
|
295
|
|
- sendDeviceEventWithName:@"RNFetchBlobProgress-upload"
|
296
|
|
- body:@{
|
297
|
|
- @"taskId": taskId,
|
298
|
|
- @"written": [NSString stringWithFormat:@"%d", totalBytesWritten],
|
299
|
|
- @"total": [NSString stringWithFormat:@"%d", bodyLength]
|
300
|
|
- }
|
301
|
|
- ];
|
|
321
|
+ if([uploadProgressTable valueForKey:taskId] == @YES) {
|
|
322
|
+ [self.bridge.eventDispatcher
|
|
323
|
+ sendDeviceEventWithName:@"RNFetchBlobProgress-upload"
|
|
324
|
+ body:@{
|
|
325
|
+ @"taskId": taskId,
|
|
326
|
+ @"written": [NSString stringWithFormat:@"%d", totalBytesWritten],
|
|
327
|
+ @"total": [NSString stringWithFormat:@"%d", bodyLength]
|
|
328
|
+ }
|
|
329
|
+ ];
|
|
330
|
+ }
|
302
|
331
|
}
|
303
|
332
|
|
304
|
333
|
+ (void) cancelRequest:(NSString *)taskId
|
305
|
334
|
{
|
306
|
|
- NSURLSessionDataTask * task = (NSURLSessionDataTask *)[taskTable objectForKey:taskId];
|
|
335
|
+ NSURLSessionDataTask * task = (NSURLSessionDataTask *)[[taskTable objectForKey:taskId] objectForKey:@"task"];
|
307
|
336
|
if(task != nil && task.state == NSURLSessionTaskStateRunning)
|
308
|
337
|
[task cancel];
|
309
|
338
|
}
|