Browse Source

Mostly RNFetchBlobNetwork cleanup

Artur Chrusciel 6 years ago
parent
commit
905ab1c2da

+ 0
- 1
ios/RNFetchBlob/RNFetchBlob.h View File

39
 @property (retain) UIDocumentInteractionController * documentController;
39
 @property (retain) UIDocumentInteractionController * documentController;
40
 
40
 
41
 + (RCTBridge *)getRCTBridge;
41
 + (RCTBridge *)getRCTBridge;
42
-+ (void) checkExpiredSessions;
43
 
42
 
44
 @end
43
 @end
45
 
44
 

+ 2
- 2
ios/RNFetchBlob/RNFetchBlob.m View File

38
 
38
 
39
 + (RCTBridge *)getRCTBridge
39
 + (RCTBridge *)getRCTBridge
40
 {
40
 {
41
-    RCTRootView * rootView = [[UIApplication sharedApplication] keyWindow].rootViewController.view;
41
+    RCTRootView * rootView = (RCTRootView*) [[UIApplication sharedApplication] keyWindow].rootViewController.view;
42
     return rootView.bridge;
42
     return rootView.bridge;
43
 }
43
 }
44
 
44
 
128
         // send HTTP request
128
         // send HTTP request
129
         else
129
         else
130
         {
130
         {
131
-            __block RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
131
+            RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
132
             [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
132
             [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
133
         }
133
         }
134
     }];
134
     }];

+ 28
- 28
ios/RNFetchBlobConst.m View File

7
 //
7
 //
8
 #import "RNFetchBlobConst.h"
8
 #import "RNFetchBlobConst.h"
9
 
9
 
10
-extern NSString *const FILE_PREFIX = @"RNFetchBlob-file://";
11
-extern NSString *const ASSET_PREFIX = @"bundle-assets://";
12
-extern NSString *const AL_PREFIX = @"assets-library://";
10
+NSString *const FILE_PREFIX = @"RNFetchBlob-file://";
11
+NSString *const ASSET_PREFIX = @"bundle-assets://";
12
+NSString *const AL_PREFIX = @"assets-library://";
13
 
13
 
14
 // fetch configs
14
 // fetch configs
15
-extern NSString *const CONFIG_USE_TEMP = @"fileCache";
16
-extern NSString *const CONFIG_FILE_PATH = @"path";
17
-extern NSString *const CONFIG_FILE_EXT = @"appendExt";
18
-extern NSString *const CONFIG_TRUSTY = @"trusty";
19
-extern NSString *const CONFIG_INDICATOR = @"indicator";
20
-extern NSString *const CONFIG_KEY = @"key";
21
-extern NSString *const CONFIG_EXTRA_BLOB_CTYPE = @"binaryContentTypes";
15
+NSString *const CONFIG_USE_TEMP = @"fileCache";
16
+NSString *const CONFIG_FILE_PATH = @"path";
17
+NSString *const CONFIG_FILE_EXT = @"appendExt";
18
+NSString *const CONFIG_TRUSTY = @"trusty";
19
+NSString *const CONFIG_INDICATOR = @"indicator";
20
+NSString *const CONFIG_KEY = @"key";
21
+NSString *const CONFIG_EXTRA_BLOB_CTYPE = @"binaryContentTypes";
22
 
22
 
23
-extern NSString *const EVENT_STATE_CHANGE = @"RNFetchBlobState";
24
-extern NSString *const EVENT_SERVER_PUSH = @"RNFetchBlobServerPush";
25
-extern NSString *const EVENT_PROGRESS = @"RNFetchBlobProgress";
26
-extern NSString *const EVENT_PROGRESS_UPLOAD = @"RNFetchBlobProgress-upload";
27
-extern NSString *const EVENT_EXPIRE = @"RNFetchBlobExpire";
23
+NSString *const EVENT_STATE_CHANGE = @"RNFetchBlobState";
24
+NSString *const EVENT_SERVER_PUSH = @"RNFetchBlobServerPush";
25
+NSString *const EVENT_PROGRESS = @"RNFetchBlobProgress";
26
+NSString *const EVENT_PROGRESS_UPLOAD = @"RNFetchBlobProgress-upload";
27
+NSString *const EVENT_EXPIRE = @"RNFetchBlobExpire";
28
 
28
 
29
-extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";
30
-extern NSString *const MSG_EVENT_LOG = @"log";
31
-extern NSString *const MSG_EVENT_WARN = @"warn";
32
-extern NSString *const MSG_EVENT_ERROR = @"error";
33
-extern NSString *const FS_EVENT_DATA = @"data";
34
-extern NSString *const FS_EVENT_END = @"end";
35
-extern NSString *const FS_EVENT_WARN = @"warn";
36
-extern NSString *const FS_EVENT_ERROR = @"error";
29
+NSString *const MSG_EVENT = @"RNFetchBlobMessage";
30
+NSString *const MSG_EVENT_LOG = @"log";
31
+NSString *const MSG_EVENT_WARN = @"warn";
32
+NSString *const MSG_EVENT_ERROR = @"error";
33
+NSString *const FS_EVENT_DATA = @"data";
34
+NSString *const FS_EVENT_END = @"end";
35
+NSString *const FS_EVENT_WARN = @"warn";
36
+NSString *const FS_EVENT_ERROR = @"error";
37
 
37
 
38
-extern NSString *const KEY_REPORT_PROGRESS = @"reportProgress";
39
-extern NSString *const KEY_REPORT_UPLOAD_PROGRESS = @"reportUploadProgress";
38
+NSString *const KEY_REPORT_PROGRESS = @"reportProgress";
39
+NSString *const KEY_REPORT_UPLOAD_PROGRESS = @"reportUploadProgress";
40
 
40
 
41
 // response type
41
 // response type
42
-extern NSString *const RESP_TYPE_BASE64 = @"base64";
43
-extern NSString *const RESP_TYPE_UTF8 = @"utf8";
44
-extern NSString *const RESP_TYPE_PATH = @"path";
42
+NSString *const RESP_TYPE_BASE64 = @"base64";
43
+NSString *const RESP_TYPE_UTF8 = @"utf8";
44
+NSString *const RESP_TYPE_PATH = @"path";

+ 2
- 2
ios/RNFetchBlobFS.h View File

34
     NSString * streamId;
34
     NSString * streamId;
35
 }
35
 }
36
 
36
 
37
-@property (nonatomic) NSOutputStream * outStream;
38
-@property (nonatomic) NSInputStream * inStream;
37
+@property (nonatomic) NSOutputStream * _Nullable outStream;
38
+@property (nonatomic) NSInputStream * _Nullable inStream;
39
 @property (strong, nonatomic) RCTResponseSenderBlock callback;
39
 @property (strong, nonatomic) RCTResponseSenderBlock callback;
40
 @property (nonatomic) RCTBridge * bridge;
40
 @property (nonatomic) RCTBridge * bridge;
41
 @property (nonatomic) NSString * encoding;
41
 @property (nonatomic) NSString * encoding;

+ 6
- 14
ios/RNFetchBlobNetwork.h View File

20
 #define RNFetchBlobNetwork_h
20
 #define RNFetchBlobNetwork_h
21
 
21
 
22
 
22
 
23
-
24
 typedef void(^CompletionHander)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error);
23
 typedef void(^CompletionHander)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error);
25
 typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error);
24
 typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error);
26
 
25
 
27
 @interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
26
 @interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
28
 
27
 
29
 @property (nullable, nonatomic) NSString * taskId;
28
 @property (nullable, nonatomic) NSString * taskId;
30
-@property (nonatomic) int expectedBytes;
31
-@property (nonatomic) int receivedBytes;
29
+@property (nonatomic) long long expectedBytes;
30
+@property (nonatomic) long long receivedBytes;
32
 @property (nonatomic) BOOL isServerPush;
31
 @property (nonatomic) BOOL isServerPush;
33
 @property (nullable, nonatomic) NSMutableData * respData;
32
 @property (nullable, nonatomic) NSMutableData * respData;
34
-@property (strong, nonatomic) RCTResponseSenderBlock callback;
33
+@property (nullable, strong, nonatomic) RCTResponseSenderBlock callback;
35
 @property (nullable, nonatomic) RCTBridge * bridge;
34
 @property (nullable, nonatomic) RCTBridge * bridge;
36
 @property (nullable, nonatomic) NSDictionary * options;
35
 @property (nullable, nonatomic) NSDictionary * options;
37
 @property (nullable, nonatomic) RNFetchBlobFS * fileStream;
36
 @property (nullable, nonatomic) RNFetchBlobFS * fileStream;
38
-@property (strong, nonatomic) CompletionHander fileTaskCompletionHandler;
39
-@property (strong, nonatomic) DataTaskCompletionHander dataTaskCompletionHandler;
40
 @property (nullable, nonatomic) NSError * error;
37
 @property (nullable, nonatomic) NSError * error;
41
 
38
 
42
 
39
 
43
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
40
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
44
-+ (void) cancelRequest:(NSString *)taskId;
45
-+ (void) enableProgressReport:(NSString *) taskId;
46
-+ (void) enableUploadProgress:(NSString *) taskId;
41
++ (void) cancelRequest:(NSString * _Nonnull)taskId;
47
 + (void) emitExpiredTasks;
42
 + (void) emitExpiredTasks;
43
++ (void) enableProgressReport:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
44
++ (void) enableUploadProgress:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
48
 
45
 
49
 - (nullable id) init;
46
 - (nullable id) init;
50
-- (void) sendRequest;
51
 - (void) sendRequest:(NSDictionary  * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
47
 - (void) sendRequest:(NSDictionary  * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
52
-+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config;
53
-+ (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config;
54
-
55
-
56
 
48
 
57
 @end
49
 @end
58
 
50
 

+ 32
- 42
ios/RNFetchBlobNetwork.m View File

70
 
70
 
71
 @interface RNFetchBlobNetwork ()
71
 @interface RNFetchBlobNetwork ()
72
 {
72
 {
73
-    BOOL * respFile;
73
+    BOOL respFile;
74
     BOOL isNewPart;
74
     BOOL isNewPart;
75
-    BOOL * isIncrement;
75
+    BOOL isIncrement;
76
     NSMutableData * partBuffer;
76
     NSMutableData * partBuffer;
77
     NSString * destPath;
77
     NSString * destPath;
78
     NSOutputStream * writeStream;
78
     NSOutputStream * writeStream;
79
     long bodyLength;
79
     long bodyLength;
80
-    NSMutableDictionary * respInfo;
81
     NSInteger respStatus;
80
     NSInteger respStatus;
82
     NSMutableArray * redirects;
81
     NSMutableArray * redirects;
83
     ResponseFormat responseFormat;
82
     ResponseFormat responseFormat;
84
-    BOOL * followRedirect;
83
+    BOOL followRedirect;
85
     BOOL backgroundTask;
84
     BOOL backgroundTask;
86
 }
85
 }
87
 
86
 
97
 @synthesize callback;
96
 @synthesize callback;
98
 @synthesize bridge;
97
 @synthesize bridge;
99
 @synthesize options;
98
 @synthesize options;
100
-@synthesize fileTaskCompletionHandler;
101
-@synthesize dataTaskCompletionHandler;
102
 @synthesize error;
99
 @synthesize error;
103
 
100
 
104
 
101
 
121
         {
118
         {
122
             progressTable = [[NSMutableDictionary alloc] init];
119
             progressTable = [[NSMutableDictionary alloc] init];
123
         }
120
         }
124
-        [progressTable setValue:config forKey:taskId];
121
+        if (config) [progressTable setValue:config forKey:taskId];
125
     }
122
     }
126
 }
123
 }
127
 
124
 
132
         {
129
         {
133
             uploadProgressTable = [[NSMutableDictionary alloc] init];
130
             uploadProgressTable = [[NSMutableDictionary alloc] init];
134
         }
131
         }
135
-        [uploadProgressTable setValue:config forKey:taskId];
132
+        if (config) [uploadProgressTable setValue:config forKey:taskId];
136
     }
133
     }
137
 }
134
 }
138
 
135
 
193
         responseFormat = AUTO;
190
         responseFormat = AUTO;
194
 
191
 
195
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
192
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
196
-    NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
197
 	NSString * key = [self.options valueForKey:CONFIG_KEY];
193
 	NSString * key = [self.options valueForKey:CONFIG_KEY];
198
-    __block NSURLSession * session;
194
+    NSURLSession * session;
199
 
195
 
200
     bodyLength = contentLength;
196
     bodyLength = contentLength;
201
 
197
 
246
         respFile = NO;
242
         respFile = NO;
247
     }
243
     }
248
 
244
 
249
-    __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
245
+    NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
250
     @synchronized ([RNFetchBlobNetwork class]){
246
     @synchronized ([RNFetchBlobNetwork class]){
251
         [taskTable setObject:task forKey:taskId];
247
         [taskTable setObject:task forKey:taskId];
252
-        [task resume];
253
     }
248
     }
249
+    [task resume];
254
 
250
 
255
     // network status indicator
251
     // network status indicator
256
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
252
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
257
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
253
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
258
-    __block UIApplication * app = [UIApplication sharedApplication];
259
 
254
 
260
 }
255
 }
261
 
256
 
269
         while((key = [emu nextObject]))
264
         while((key = [emu nextObject]))
270
         {
265
         {
271
             RCTBridge * bridge = [RNFetchBlob getRCTBridge];
266
             RCTBridge * bridge = [RNFetchBlob getRCTBridge];
272
-            NSData * args = @{ @"taskId": key };
267
+            id args = @{ @"taskId": key };
273
             [bridge.eventDispatcher sendDeviceEventWithName:EVENT_EXPIRE body:args];
268
             [bridge.eventDispatcher sendDeviceEventWithName:EVENT_EXPIRE body:args];
274
 
269
 
275
         }
270
         }
352
             {
347
             {
353
                 respType = @"blob";
348
                 respType = @"blob";
354
                 // for XMLHttpRequest, switch response data handling strategy automatically
349
                 // for XMLHttpRequest, switch response data handling strategy automatically
355
-                if([options valueForKey:@"auto"] == YES) {
350
+                if([options valueForKey:@"auto"]) {
356
                     respFile = YES;
351
                     respFile = YES;
357
                     destPath = [RNFetchBlobFS getTempPath:taskId withExtension:@""];
352
                     destPath = [RNFetchBlobFS getTempPath:taskId withExtension:@""];
358
                 }
353
                 }
359
             }
354
             }
360
-        }
361
-        else
355
+        } else {
362
             respType = @"text";
356
             respType = @"text";
363
-        respInfo = @{
364
-                     @"taskId": taskId,
365
-                     @"state": @"2",
366
-                     @"headers": headers,
367
-                     @"redirects": redirects,
368
-                     @"respType" : respType,
369
-                     @"timeout" : @NO,
370
-                     @"status": [NSNumber numberWithInteger:statusCode]
371
-                    };
357
+        }
372
 
358
 
373
 #pragma mark - handling cookies
359
 #pragma mark - handling cookies
374
         // # 153 get cookies
360
         // # 153 get cookies
383
 
369
 
384
         [self.bridge.eventDispatcher
370
         [self.bridge.eventDispatcher
385
          sendDeviceEventWithName: EVENT_STATE_CHANGE
371
          sendDeviceEventWithName: EVENT_STATE_CHANGE
386
-         body:respInfo
372
+         body:@{
373
+                @"taskId": taskId,
374
+                @"state": @"2",
375
+                @"headers": headers,
376
+                @"redirects": redirects,
377
+                @"respType" : respType,
378
+                @"timeout" : @NO,
379
+                @"status": [NSNumber numberWithInteger:statusCode]
380
+                }
387
         ];
381
         ];
388
-        headers = nil;
389
-        respInfo = nil;
390
-
391
     }
382
     }
392
     else
383
     else
393
         NSLog(@"oops");
384
         NSLog(@"oops");
475
          sendDeviceEventWithName:EVENT_PROGRESS
466
          sendDeviceEventWithName:EVENT_PROGRESS
476
          body:@{
467
          body:@{
477
                 @"taskId": taskId,
468
                 @"taskId": taskId,
478
-                @"written": [NSString stringWithFormat:@"%d", receivedBytes],
479
-                @"total": [NSString stringWithFormat:@"%d", expectedBytes],
469
+                @"written": [NSString stringWithFormat:@"%ld", (long) receivedBytes],
470
+                @"total": [NSString stringWithFormat:@"%ld", (long) expectedBytes],
480
                 @"chunk": chunkString
471
                 @"chunk": chunkString
481
                 }
472
                 }
482
          ];
473
          ];
494
 {
485
 {
495
 
486
 
496
     self.error = error;
487
     self.error = error;
497
-    NSString * errMsg = [NSNull null];
498
-    NSString * respStr = [NSNull null];
499
-    NSString * rnfbRespType = @"";
488
+    NSString * errMsg;
489
+    NSString * respStr;
490
+    NSString * rnfbRespType;
500
 
491
 
501
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
492
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
502
 
493
 
503
-    if(respInfo == nil)
504
-    {
505
-        respInfo = [NSNull null];
506
-    }
507
-
508
     if(error != nil)
494
     if(error != nil)
509
     {
495
     {
510
         errMsg = [error localizedDescription];
496
         errMsg = [error localizedDescription];
550
     }
536
     }
551
 
537
 
552
 
538
 
553
-    callback(@[ errMsg, rnfbRespType, respStr]);
539
+    callback(@[
540
+               errMsg ?: [NSNull null],
541
+               rnfbRespType ?: @"",
542
+               respStr ?: [NSNull null]
543
+               ]);
554
 
544
 
555
     @synchronized ([RNFetchBlobNetwork class])
545
     @synchronized ([RNFetchBlobNetwork class])
556
     {
546
     {
608
 
598
 
609
 - (void) URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable credantial))completionHandler
599
 - (void) URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable credantial))completionHandler
610
 {
600
 {
611
-    BOOL trusty = [options valueForKey:CONFIG_TRUSTY];
601
+    BOOL trusty = [[options valueForKey:CONFIG_TRUSTY] boolValue];
612
     if(!trusty)
602
     if(!trusty)
613
     {
603
     {
614
         completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
604
         completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);

+ 1
- 1
ios/RNFetchBlobReqBuilder.h View File

29
                      body:(NSString *)body
29
                      body:(NSString *)body
30
                onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete;
30
                onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete;
31
 
31
 
32
-+(NSString *) getHeaderIgnoreCases:(NSString *)field fromHeaders:(NSMutableArray *) headers;
32
++(NSString *) getHeaderIgnoreCases:(NSString *)field fromHeaders:(NSDictionary *) headers;
33
 
33
 
34
 
34
 
35
 @end
35
 @end

+ 1
- 1
ios/RNFetchBlobReqBuilder.m View File

277
     }
277
     }
278
 }
278
 }
279
 
279
 
280
-+(NSString *) getHeaderIgnoreCases:(NSString *)field fromHeaders:(NSMutableDictionary *) headers {
280
++(NSString *) getHeaderIgnoreCases:(NSString *)field fromHeaders:(NSDictionary *) headers {
281
 
281
 
282
     NSString * normalCase = [headers valueForKey:field];
282
     NSString * normalCase = [headers valueForKey:field];
283
     NSString * ignoredCase = [headers valueForKey:[field lowercaseString]];
283
     NSString * ignoredCase = [headers valueForKey:[field lowercaseString]];