|
@@ -11,24 +11,10 @@
|
11
|
11
|
#import "RNFetchBlobFS.h"
|
12
|
12
|
#import "RNFetchBlobConst.h"
|
13
|
13
|
#import "RNFetchBlobReqBuilder.h"
|
14
|
|
-#if __has_include(<React/RCTLog.h>)
|
15
|
|
-#import <React/RCTLog.h>
|
16
|
|
-#else
|
17
|
|
-#import "RCTLog.h"
|
18
|
|
-#endif
|
19
|
14
|
|
20
|
15
|
#import "IOS7Polyfill.h"
|
21
|
16
|
#import <CommonCrypto/CommonDigest.h>
|
22
|
17
|
|
23
|
|
-NSMapTable * taskTable;
|
24
|
|
-
|
25
|
|
-__attribute__((constructor))
|
26
|
|
-static void initialize_tables() {
|
27
|
|
- if(taskTable == nil)
|
28
|
|
- {
|
29
|
|
- taskTable = [[NSMapTable alloc] init];
|
30
|
|
- }
|
31
|
|
-}
|
32
|
18
|
|
33
|
19
|
typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
34
|
20
|
UTF8,
|
|
@@ -50,7 +36,6 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
50
|
36
|
ResponseFormat responseFormat;
|
51
|
37
|
BOOL followRedirect;
|
52
|
38
|
BOOL backgroundTask;
|
53
|
|
- BOOL uploadTask;
|
54
|
39
|
}
|
55
|
40
|
|
56
|
41
|
@end
|
|
@@ -97,16 +82,6 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
97
|
82
|
self.options = options;
|
98
|
83
|
|
99
|
84
|
backgroundTask = [[options valueForKey:@"IOSBackgroundTask"] boolValue];
|
100
|
|
- uploadTask = [options valueForKey:@"IOSUploadTask"] == nil ? NO : [[options valueForKey:@"IOSUploadTask"] boolValue];
|
101
|
|
-
|
102
|
|
- NSString * filepath = [options valueForKey:@"uploadFilePath"];
|
103
|
|
-
|
104
|
|
- if (uploadTask && ![[NSFileManager defaultManager] fileExistsAtPath:[NSURL URLWithString:filepath].path]) {
|
105
|
|
- RCTLog(@"[RNFetchBlobRequest] sendRequest uploadTask file doesn't exist %@", filepath);
|
106
|
|
- callback(@[@"uploadTask file doesn't exist", @"", [NSNull null]]);
|
107
|
|
- return;
|
108
|
|
- }
|
109
|
|
-
|
110
|
85
|
// when followRedirect not set in options, defaults to TRUE
|
111
|
86
|
followRedirect = [options valueForKey:@"followRedirect"] == nil ? YES : [[options valueForKey:@"followRedirect"] boolValue];
|
112
|
87
|
isIncrement = [[options valueForKey:@"increment"] boolValue];
|
|
@@ -129,6 +104,7 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
129
|
104
|
|
130
|
105
|
NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
|
131
|
106
|
NSString * key = [self.options valueForKey:CONFIG_KEY];
|
|
107
|
+ NSURLSession * session;
|
132
|
108
|
|
133
|
109
|
bodyLength = contentLength;
|
134
|
110
|
|
|
@@ -141,7 +117,6 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
141
|
117
|
defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:taskId];
|
142
|
118
|
}
|
143
|
119
|
|
144
|
|
-
|
145
|
120
|
// request timeout, -1 if not set in options
|
146
|
121
|
float timeout = [options valueForKey:@"timeout"] == nil ? -1 : [[options valueForKey:@"timeout"] floatValue];
|
147
|
122
|
|
|
@@ -150,7 +125,7 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
150
|
125
|
}
|
151
|
126
|
|
152
|
127
|
defaultConfigObject.HTTPMaximumConnectionsPerHost = 10;
|
153
|
|
- _session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:operationQueue];
|
|
128
|
+ session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:operationQueue];
|
154
|
129
|
|
155
|
130
|
if (path || [self.options valueForKey:CONFIG_USE_TEMP]) {
|
156
|
131
|
respFile = YES;
|
|
@@ -182,19 +157,8 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
182
|
157
|
respFile = NO;
|
183
|
158
|
}
|
184
|
159
|
|
185
|
|
- __block NSURLSessionTask * task;
|
186
|
|
-
|
187
|
|
- if(uploadTask)
|
188
|
|
- {
|
189
|
|
- task = [_session uploadTaskWithRequest:req fromFile:[NSURL URLWithString:filepath]];
|
190
|
|
- }
|
191
|
|
- else
|
192
|
|
- {
|
193
|
|
- task = [_session dataTaskWithRequest:req];
|
194
|
|
- }
|
195
|
|
-
|
196
|
|
- [taskTable setObject:task forKey:taskId];
|
197
|
|
- [task resume];
|
|
160
|
+ self.task = [session dataTaskWithRequest:req];
|
|
161
|
+ [self.task resume];
|
198
|
162
|
|
199
|
163
|
// network status indicator
|
200
|
164
|
if ([[options objectForKey:CONFIG_INDICATOR] boolValue]) {
|
|
@@ -218,7 +182,6 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
218
|
182
|
// set expected content length on response received
|
219
|
183
|
- (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
|
220
|
184
|
{
|
221
|
|
- NSLog(@"sess didReceiveResponse");
|
222
|
185
|
expectedBytes = [response expectedContentLength];
|
223
|
186
|
|
224
|
187
|
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
|
|
@@ -244,7 +207,7 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
244
|
207
|
|
245
|
208
|
partBuffer = [[NSMutableData alloc] init];
|
246
|
209
|
completionHandler(NSURLSessionResponseAllow);
|
247
|
|
-
|
|
210
|
+
|
248
|
211
|
return;
|
249
|
212
|
} else {
|
250
|
213
|
self.isServerPush = [[respCType lowercaseString] RNFBContainsString:@"multipart/x-mixed-replace;"];
|
|
@@ -306,6 +269,42 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
306
|
269
|
NSLog(@"oops");
|
307
|
270
|
}
|
308
|
271
|
|
|
272
|
+ if (respFile)
|
|
273
|
+ {
|
|
274
|
+ @try{
|
|
275
|
+ NSFileManager * fm = [NSFileManager defaultManager];
|
|
276
|
+ NSString * folder = [destPath stringByDeletingLastPathComponent];
|
|
277
|
+
|
|
278
|
+ if (![fm fileExistsAtPath:folder]) {
|
|
279
|
+ [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:nil];
|
|
280
|
+ }
|
|
281
|
+
|
|
282
|
+ // if not set overwrite in options, defaults to TRUE
|
|
283
|
+ BOOL overwrite = [options valueForKey:@"overwrite"] == nil ? YES : [[options valueForKey:@"overwrite"] boolValue];
|
|
284
|
+ BOOL appendToExistingFile = [destPath RNFBContainsString:@"?append=true"];
|
|
285
|
+
|
|
286
|
+ appendToExistingFile = !overwrite;
|
|
287
|
+
|
|
288
|
+ // For solving #141 append response data if the file already exists
|
|
289
|
+ // base on PR#139 @kejinliang
|
|
290
|
+ if (appendToExistingFile) {
|
|
291
|
+ destPath = [destPath stringByReplacingOccurrencesOfString:@"?append=true" withString:@""];
|
|
292
|
+ }
|
|
293
|
+
|
|
294
|
+ if (![fm fileExistsAtPath:destPath]) {
|
|
295
|
+ [fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
|
|
296
|
+ }
|
|
297
|
+
|
|
298
|
+ writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:appendToExistingFile];
|
|
299
|
+ [writeStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
|
300
|
+ [writeStream open];
|
|
301
|
+ }
|
|
302
|
+ @catch(NSException * ex)
|
|
303
|
+ {
|
|
304
|
+ NSLog(@"write file error");
|
|
305
|
+ }
|
|
306
|
+ }
|
|
307
|
+
|
309
|
308
|
completionHandler(NSURLSessionResponseAllow);
|
310
|
309
|
}
|
311
|
310
|
|
|
@@ -329,7 +328,11 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
329
|
328
|
chunkString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
330
|
329
|
}
|
331
|
330
|
|
332
|
|
- [respData appendData:data];
|
|
331
|
+ if (respFile) {
|
|
332
|
+ [writeStream write:[data bytes] maxLength:[data length]];
|
|
333
|
+ } else {
|
|
334
|
+ [respData appendData:data];
|
|
335
|
+ }
|
333
|
336
|
|
334
|
337
|
if (expectedBytes == 0) {
|
335
|
338
|
return;
|
|
@@ -350,16 +353,8 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
350
|
353
|
}
|
351
|
354
|
}
|
352
|
355
|
|
353
|
|
-- (void) cancelRequest:(NSString *)taskId
|
354
|
|
-{
|
355
|
|
- NSURLSessionDataTask * task = [taskTable objectForKey:taskId];
|
356
|
|
- if(task != nil && task.state == NSURLSessionTaskStateRunning)
|
357
|
|
- [task cancel];
|
358
|
|
-}
|
359
|
|
-
|
360
|
356
|
- (void) URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error
|
361
|
357
|
{
|
362
|
|
- RCTLog(@"[RNFetchBlobRequest] session didBecomeInvalidWithError %@", [error description]);
|
363
|
358
|
if ([session isEqual:session]) {
|
364
|
359
|
session = nil;
|
365
|
360
|
}
|
|
@@ -368,7 +363,7 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
368
|
363
|
|
369
|
364
|
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
|
370
|
365
|
{
|
371
|
|
- RCTLog(@"[RNFetchBlobRequest] session didCompleteWithError %@", [error description]);
|
|
366
|
+
|
372
|
367
|
self.error = error;
|
373
|
368
|
NSString * errMsg;
|
374
|
369
|
NSString * respStr;
|
|
@@ -421,17 +416,10 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
421
|
416
|
respStr ?: [NSNull null]
|
422
|
417
|
]);
|
423
|
418
|
|
424
|
|
- @synchronized(taskTable)
|
425
|
|
- {
|
426
|
|
- if([taskTable objectForKey:taskId] == nil)
|
427
|
|
- NSLog(@"object released by ARC.");
|
428
|
|
- else
|
429
|
|
- [taskTable removeObjectForKey:taskId];
|
430
|
|
- }
|
431
|
|
-
|
432
|
419
|
respData = nil;
|
433
|
420
|
receivedBytes = 0;
|
434
|
421
|
[session finishTasksAndInvalidate];
|
|
422
|
+
|
435
|
423
|
}
|
436
|
424
|
|
437
|
425
|
// upload progress handler
|
|
@@ -442,7 +430,7 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
442
|
430
|
}
|
443
|
431
|
|
444
|
432
|
NSNumber * now = [NSNumber numberWithFloat:((float)totalBytesWritten/(float)totalBytesExpectedToWrite)];
|
445
|
|
-
|
|
433
|
+
|
446
|
434
|
if ([self.uploadProgressConfig shouldReport:now]) {
|
447
|
435
|
[self.bridge.eventDispatcher
|
448
|
436
|
sendDeviceEventWithName:EVENT_PROGRESS_UPLOAD
|
|
@@ -468,19 +456,7 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
|
468
|
456
|
|
469
|
457
|
- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
|
470
|
458
|
{
|
471
|
|
- RCTLog(@"[RNFetchBlobRequest] session done in background");
|
472
|
|
- dispatch_async(dispatch_get_main_queue(), ^{
|
473
|
|
- id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
|
474
|
|
- SEL selector = NSSelectorFromString(@"backgroundTransferCompletionHandler");
|
475
|
|
- if ([appDelegate respondsToSelector:selector]) {
|
476
|
|
- void(^completionHandler)() = [appDelegate performSelector:selector];
|
477
|
|
- if (completionHandler != nil) {
|
478
|
|
- completionHandler();
|
479
|
|
- completionHandler = nil;
|
480
|
|
- }
|
481
|
|
- }
|
482
|
|
-
|
483
|
|
- });
|
|
459
|
+ NSLog(@"sess done in background");
|
484
|
460
|
}
|
485
|
461
|
|
486
|
462
|
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler
|