Skip to content

Commit 905ab1c

Browse files
author
Artur Chrusciel
committed
Mostly RNFetchBlobNetwork cleanup
1 parent 38f4d75 commit 905ab1c

File tree

8 files changed

+72
-91
lines changed

8 files changed

+72
-91
lines changed

ios/RNFetchBlob/RNFetchBlob.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
@property (retain) UIDocumentInteractionController * documentController;
4040

4141
+ (RCTBridge *)getRCTBridge;
42-
+ (void) checkExpiredSessions;
4342

4443
@end
4544

ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ - (dispatch_queue_t) methodQueue {
3838

3939
+ (RCTBridge *)getRCTBridge
4040
{
41-
RCTRootView * rootView = [[UIApplication sharedApplication] keyWindow].rootViewController.view;
41+
RCTRootView * rootView = (RCTRootView*) [[UIApplication sharedApplication] keyWindow].rootViewController.view;
4242
return rootView.bridge;
4343
}
4444

@@ -128,7 +128,7 @@ - (NSDictionary *)constantsToExport
128128
// send HTTP request
129129
else
130130
{
131-
__block RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
131+
RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
132132
[utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
133133
}
134134
}];

ios/RNFetchBlobConst.m

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,38 @@
77
//
88
#import "RNFetchBlobConst.h"
99

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://";
1313

1414
// 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";
2222

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";
2828

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";
3737

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";
4040

4141
// 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";

ios/RNFetchBlobFS.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
NSString * streamId;
3535
}
3636

37-
@property (nonatomic) NSOutputStream * outStream;
38-
@property (nonatomic) NSInputStream * inStream;
37+
@property (nonatomic) NSOutputStream * _Nullable outStream;
38+
@property (nonatomic) NSInputStream * _Nullable inStream;
3939
@property (strong, nonatomic) RCTResponseSenderBlock callback;
4040
@property (nonatomic) RCTBridge * bridge;
4141
@property (nonatomic) NSString * encoding;

ios/RNFetchBlobNetwork.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,31 @@
2020
#define RNFetchBlobNetwork_h
2121

2222

23-
2423
typedef void(^CompletionHander)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error);
2524
typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error);
2625

2726
@interface RNFetchBlobNetwork : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
2827

2928
@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;
3231
@property (nonatomic) BOOL isServerPush;
3332
@property (nullable, nonatomic) NSMutableData * respData;
34-
@property (strong, nonatomic) RCTResponseSenderBlock callback;
33+
@property (nullable, strong, nonatomic) RCTResponseSenderBlock callback;
3534
@property (nullable, nonatomic) RCTBridge * bridge;
3635
@property (nullable, nonatomic) NSDictionary * options;
3736
@property (nullable, nonatomic) RNFetchBlobFS * fileStream;
38-
@property (strong, nonatomic) CompletionHander fileTaskCompletionHandler;
39-
@property (strong, nonatomic) DataTaskCompletionHander dataTaskCompletionHandler;
4037
@property (nullable, nonatomic) NSError * error;
4138

4239

4340
+ (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;
4742
+ (void) emitExpiredTasks;
43+
+ (void) enableProgressReport:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
44+
+ (void) enableUploadProgress:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
4845

4946
- (nullable id) init;
50-
- (void) sendRequest;
5147
- (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-
5648

5749
@end
5850

ios/RNFetchBlobNetwork.m

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,17 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
7070

7171
@interface RNFetchBlobNetwork ()
7272
{
73-
BOOL * respFile;
73+
BOOL respFile;
7474
BOOL isNewPart;
75-
BOOL * isIncrement;
75+
BOOL isIncrement;
7676
NSMutableData * partBuffer;
7777
NSString * destPath;
7878
NSOutputStream * writeStream;
7979
long bodyLength;
80-
NSMutableDictionary * respInfo;
8180
NSInteger respStatus;
8281
NSMutableArray * redirects;
8382
ResponseFormat responseFormat;
84-
BOOL * followRedirect;
83+
BOOL followRedirect;
8584
BOOL backgroundTask;
8685
}
8786

@@ -97,8 +96,6 @@ @implementation RNFetchBlobNetwork
9796
@synthesize callback;
9897
@synthesize bridge;
9998
@synthesize options;
100-
@synthesize fileTaskCompletionHandler;
101-
@synthesize dataTaskCompletionHandler;
10299
@synthesize error;
103100

104101

@@ -121,7 +118,7 @@ + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)
121118
{
122119
progressTable = [[NSMutableDictionary alloc] init];
123120
}
124-
[progressTable setValue:config forKey:taskId];
121+
if (config) [progressTable setValue:config forKey:taskId];
125122
}
126123
}
127124

@@ -132,7 +129,7 @@ + (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)
132129
{
133130
uploadProgressTable = [[NSMutableDictionary alloc] init];
134131
}
135-
[uploadProgressTable setValue:config forKey:taskId];
132+
if (config) [uploadProgressTable setValue:config forKey:taskId];
136133
}
137134
}
138135

@@ -193,9 +190,8 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
193190
responseFormat = AUTO;
194191

195192
NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
196-
NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
197193
NSString * key = [self.options valueForKey:CONFIG_KEY];
198-
__block NSURLSession * session;
194+
NSURLSession * session;
199195

200196
bodyLength = contentLength;
201197

@@ -246,16 +242,15 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
246242
respFile = NO;
247243
}
248244

249-
__block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
245+
NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
250246
@synchronized ([RNFetchBlobNetwork class]){
251247
[taskTable setObject:task forKey:taskId];
252-
[task resume];
253248
}
249+
[task resume];
254250

255251
// network status indicator
256252
if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
257253
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
258-
__block UIApplication * app = [UIApplication sharedApplication];
259254

260255
}
261256

@@ -269,7 +264,7 @@ + (void) emitExpiredTasks
269264
while((key = [emu nextObject]))
270265
{
271266
RCTBridge * bridge = [RNFetchBlob getRCTBridge];
272-
NSData * args = @{ @"taskId": key };
267+
id args = @{ @"taskId": key };
273268
[bridge.eventDispatcher sendDeviceEventWithName:EVENT_EXPIRE body:args];
274269

275270
}
@@ -352,23 +347,14 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
352347
{
353348
respType = @"blob";
354349
// for XMLHttpRequest, switch response data handling strategy automatically
355-
if([options valueForKey:@"auto"] == YES) {
350+
if([options valueForKey:@"auto"]) {
356351
respFile = YES;
357352
destPath = [RNFetchBlobFS getTempPath:taskId withExtension:@""];
358353
}
359354
}
360-
}
361-
else
355+
} else {
362356
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+
}
372358

373359
#pragma mark - handling cookies
374360
// # 153 get cookies
@@ -383,11 +369,16 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
383369

384370
[self.bridge.eventDispatcher
385371
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+
}
387381
];
388-
headers = nil;
389-
respInfo = nil;
390-
391382
}
392383
else
393384
NSLog(@"oops");
@@ -475,8 +466,8 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
475466
sendDeviceEventWithName:EVENT_PROGRESS
476467
body:@{
477468
@"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],
480471
@"chunk": chunkString
481472
}
482473
];
@@ -494,17 +485,12 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
494485
{
495486

496487
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;
500491

501492
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
502493

503-
if(respInfo == nil)
504-
{
505-
respInfo = [NSNull null];
506-
}
507-
508494
if(error != nil)
509495
{
510496
errMsg = [error localizedDescription];
@@ -550,7 +536,11 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
550536
}
551537

552538

553-
callback(@[ errMsg, rnfbRespType, respStr]);
539+
callback(@[
540+
errMsg ?: [NSNull null],
541+
rnfbRespType ?: @"",
542+
respStr ?: [NSNull null]
543+
]);
554544

555545
@synchronized ([RNFetchBlobNetwork class])
556546
{
@@ -608,7 +598,7 @@ + (void) cancelRequest:(NSString *)taskId
608598

609599
- (void) URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable credantial))completionHandler
610600
{
611-
BOOL trusty = [options valueForKey:CONFIG_TRUSTY];
601+
BOOL trusty = [[options valueForKey:CONFIG_TRUSTY] boolValue];
612602
if(!trusty)
613603
{
614604
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);

ios/RNFetchBlobReqBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
body:(NSString *)body
3030
onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete;
3131

32-
+(NSString *) getHeaderIgnoreCases:(NSString *)field fromHeaders:(NSMutableArray *) headers;
32+
+(NSString *) getHeaderIgnoreCases:(NSString *)field fromHeaders:(NSDictionary *) headers;
3333

3434

3535
@end

ios/RNFetchBlobReqBuilder.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ void __block (^getFieldData)(id field) = ^(id field)
277277
}
278278
}
279279

280-
+(NSString *) getHeaderIgnoreCases:(NSString *)field fromHeaders:(NSMutableDictionary *) headers {
280+
+(NSString *) getHeaderIgnoreCases:(NSString *)field fromHeaders:(NSDictionary *) headers {
281281

282282
NSString * normalCase = [headers valueForKey:field];
283283
NSString * ignoredCase = [headers valueForKey:[field lowercaseString]];

0 commit comments

Comments
 (0)