Browse Source

Mostly RNFetchBlobNetwork cleanup

Artur Chrusciel 6 years ago
parent
commit
905ab1c2da

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

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

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

@@ -38,7 +38,7 @@ dispatch_queue_t fsQueue;
38 38
 
39 39
 + (RCTBridge *)getRCTBridge
40 40
 {
41
-    RCTRootView * rootView = [[UIApplication sharedApplication] keyWindow].rootViewController.view;
41
+    RCTRootView * rootView = (RCTRootView*) [[UIApplication sharedApplication] keyWindow].rootViewController.view;
42 42
     return rootView.bridge;
43 43
 }
44 44
 
@@ -128,7 +128,7 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
128 128
         // send HTTP request
129 129
         else
130 130
         {
131
-            __block RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
131
+            RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
132 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,38 +7,38 @@
7 7
 //
8 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 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 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,8 +34,8 @@
34 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 39
 @property (strong, nonatomic) RCTResponseSenderBlock callback;
40 40
 @property (nonatomic) RCTBridge * bridge;
41 41
 @property (nonatomic) NSString * encoding;

+ 6
- 14
ios/RNFetchBlobNetwork.h View File

@@ -20,39 +20,31 @@
20 20
 #define RNFetchBlobNetwork_h
21 21
 
22 22
 
23
-
24 23
 typedef void(^CompletionHander)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error);
25 24
 typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error);
26 25
 
27 26
 @interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
28 27
 
29 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 31
 @property (nonatomic) BOOL isServerPush;
33 32
 @property (nullable, nonatomic) NSMutableData * respData;
34
-@property (strong, nonatomic) RCTResponseSenderBlock callback;
33
+@property (nullable, strong, nonatomic) RCTResponseSenderBlock callback;
35 34
 @property (nullable, nonatomic) RCTBridge * bridge;
36 35
 @property (nullable, nonatomic) NSDictionary * options;
37 36
 @property (nullable, nonatomic) RNFetchBlobFS * fileStream;
38
-@property (strong, nonatomic) CompletionHander fileTaskCompletionHandler;
39
-@property (strong, nonatomic) DataTaskCompletionHander dataTaskCompletionHandler;
40 37
 @property (nullable, nonatomic) NSError * error;
41 38
 
42 39
 
43 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 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 46
 - (nullable id) init;
50
-- (void) sendRequest;
51 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 49
 @end
58 50
 

+ 32
- 42
ios/RNFetchBlobNetwork.m View File

@@ -70,18 +70,17 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
70 70
 
71 71
 @interface RNFetchBlobNetwork ()
72 72
 {
73
-    BOOL * respFile;
73
+    BOOL respFile;
74 74
     BOOL isNewPart;
75
-    BOOL * isIncrement;
75
+    BOOL isIncrement;
76 76
     NSMutableData * partBuffer;
77 77
     NSString * destPath;
78 78
     NSOutputStream * writeStream;
79 79
     long bodyLength;
80
-    NSMutableDictionary * respInfo;
81 80
     NSInteger respStatus;
82 81
     NSMutableArray * redirects;
83 82
     ResponseFormat responseFormat;
84
-    BOOL * followRedirect;
83
+    BOOL followRedirect;
85 84
     BOOL backgroundTask;
86 85
 }
87 86
 
@@ -97,8 +96,6 @@ NSOperationQueue *taskQueue;
97 96
 @synthesize callback;
98 97
 @synthesize bridge;
99 98
 @synthesize options;
100
-@synthesize fileTaskCompletionHandler;
101
-@synthesize dataTaskCompletionHandler;
102 99
 @synthesize error;
103 100
 
104 101
 
@@ -121,7 +118,7 @@ NSOperationQueue *taskQueue;
121 118
         {
122 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,7 +129,7 @@ NSOperationQueue *taskQueue;
132 129
         {
133 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,9 +190,8 @@ NSOperationQueue *taskQueue;
193 190
         responseFormat = AUTO;
194 191
 
195 192
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
196
-    NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
197 193
 	NSString * key = [self.options valueForKey:CONFIG_KEY];
198
-    __block NSURLSession * session;
194
+    NSURLSession * session;
199 195
 
200 196
     bodyLength = contentLength;
201 197
 
@@ -246,16 +242,15 @@ NSOperationQueue *taskQueue;
246 242
         respFile = NO;
247 243
     }
248 244
 
249
-    __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
245
+    NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
250 246
     @synchronized ([RNFetchBlobNetwork class]){
251 247
         [taskTable setObject:task forKey:taskId];
252
-        [task resume];
253 248
     }
249
+    [task resume];
254 250
 
255 251
     // network status indicator
256 252
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
257 253
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
258
-    __block UIApplication * app = [UIApplication sharedApplication];
259 254
 
260 255
 }
261 256
 
@@ -269,7 +264,7 @@ NSOperationQueue *taskQueue;
269 264
         while((key = [emu nextObject]))
270 265
         {
271 266
             RCTBridge * bridge = [RNFetchBlob getRCTBridge];
272
-            NSData * args = @{ @"taskId": key };
267
+            id args = @{ @"taskId": key };
273 268
             [bridge.eventDispatcher sendDeviceEventWithName:EVENT_EXPIRE body:args];
274 269
 
275 270
         }
@@ -352,23 +347,14 @@ NSOperationQueue *taskQueue;
352 347
             {
353 348
                 respType = @"blob";
354 349
                 // for XMLHttpRequest, switch response data handling strategy automatically
355
-                if([options valueForKey:@"auto"] == YES) {
350
+                if([options valueForKey:@"auto"]) {
356 351
                     respFile = YES;
357 352
                     destPath = [RNFetchBlobFS getTempPath:taskId withExtension:@""];
358 353
                 }
359 354
             }
360
-        }
361
-        else
355
+        } else {
362 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 359
 #pragma mark - handling cookies
374 360
         // # 153 get cookies
@@ -383,11 +369,16 @@ NSOperationQueue *taskQueue;
383 369
 
384 370
         [self.bridge.eventDispatcher
385 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 383
     else
393 384
         NSLog(@"oops");
@@ -475,8 +466,8 @@ NSOperationQueue *taskQueue;
475 466
          sendDeviceEventWithName:EVENT_PROGRESS
476 467
          body:@{
477 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 471
                 @"chunk": chunkString
481 472
                 }
482 473
          ];
@@ -494,17 +485,12 @@ NSOperationQueue *taskQueue;
494 485
 {
495 486
 
496 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 492
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
502 493
 
503
-    if(respInfo == nil)
504
-    {
505
-        respInfo = [NSNull null];
506
-    }
507
-
508 494
     if(error != nil)
509 495
     {
510 496
         errMsg = [error localizedDescription];
@@ -550,7 +536,11 @@ NSOperationQueue *taskQueue;
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 545
     @synchronized ([RNFetchBlobNetwork class])
556 546
     {
@@ -608,7 +598,7 @@ NSOperationQueue *taskQueue;
608 598
 
609 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 602
     if(!trusty)
613 603
     {
614 604
         completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);

+ 1
- 1
ios/RNFetchBlobReqBuilder.h View File

@@ -29,7 +29,7 @@
29 29
                      body:(NSString *)body
30 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 35
 @end

+ 1
- 1
ios/RNFetchBlobReqBuilder.m View File

@@ -277,7 +277,7 @@
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 282
     NSString * normalCase = [headers valueForKey:field];
283 283
     NSString * ignoredCase = [headers valueForKey:[field lowercaseString]];