1
1
//
2
2
// RNFetchBlob.m
3
3
//
4
- // Created by Ben Hsieh on 2016/4/28.
4
+ // Created by suzuri04x2 on 2016/4/28.
5
5
// Copyright © 2016年 Facebook. All rights reserved.
6
6
//
7
7
10
10
#import " RCTLog.h"
11
11
#import < Foundation/Foundation.h>
12
12
13
- // CalendarManager.m
13
+
14
+ // //////////////////////////////////////
15
+ //
16
+ // Util functions
17
+ //
18
+ // //////////////////////////////////////
19
+
20
+ @implementation FetchBlobUtils
21
+
22
+ // callback class method to handle request
23
+ + (void ) onBlobResponse : (NSURLResponse * _Nullable)response withData : (NSData * _Nullable)data withError : (NSError * _Nullable)connectionError withCallback : (RCTResponseSenderBlock)callback {
24
+
25
+ NSHTTPURLResponse * resp = (NSHTTPURLResponse *) response;
26
+ NSString * status = [NSString stringWithFormat: @" %d " , resp.statusCode];
27
+
28
+ if (connectionError)
29
+ {
30
+ callback (@[[connectionError localizedDescription ], [NSNull null ]]);
31
+ }
32
+ else if (![status isEqualToString: @" 200" ]) {
33
+ callback (@[status, [NSNull null ]]);
34
+ }
35
+ else {
36
+ callback (@[[NSNull null ], [data base64EncodedStringWithOptions: 0 ]]);
37
+ }
38
+
39
+ }
40
+
41
+ // removing case of headers
42
+ + (NSMutableDictionary *) normalizeHeaders : (NSDictionary *)headers {
43
+
44
+ NSMutableDictionary * mheaders = [[NSMutableDictionary alloc ]init];
45
+ for (NSString * key in headers) {
46
+ [mheaders setValue: [headers valueForKey: key] forKey: [key lowercaseString ]];
47
+ }
48
+
49
+ return mheaders;
50
+ }
51
+
52
+ @end
53
+
54
+
55
+ // //////////////////////////////////////
56
+ //
57
+ // Exported native methods
58
+ //
59
+ // //////////////////////////////////////
60
+
14
61
@implementation RNFetchBlob
15
62
16
63
RCT_EXPORT_MODULE ();
17
64
18
65
// Fetch blob data request
19
66
RCT_EXPORT_METHOD (fetchBlobForm:(NSString *)method url:(NSString *)url headers:(NSDictionary *)headers form:(NSArray *)form callback:(RCTResponseSenderBlock)callback)
20
67
{
68
+
21
69
// send request
22
70
NSMutableURLRequest *request = [[NSMutableURLRequest alloc ]
23
71
initWithURL: [NSURL
24
72
URLWithString: url]];
73
+ NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [ FetchBlobUtils normalizeHeaders: headers]];
25
74
26
- NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] init ];
27
-
28
- // make headers case insensitive
29
- for (NSString * key in headers) {
30
- [mheaders setValue: [headers valueForKey: key] forKey: [key lowercaseString ]];
31
- }
32
75
33
76
NSTimeInterval timeStamp = [[NSDate date ] timeIntervalSince1970 ];
34
77
NSNumber * timeStampObj = [NSNumber numberWithDouble: timeStamp];
@@ -40,7 +83,7 @@ @implementation RNFetchBlob
40
83
if ([[method lowercaseString ] isEqualToString: @" post" ] || [[method lowercaseString ] isEqualToString: @" put" ]) {
41
84
NSMutableData * postData = [[NSMutableData alloc ] init ];
42
85
43
- // combine body
86
+ // combine multipart/form-data body
44
87
for (id field in form) {
45
88
NSString * name = [field valueForKey: @" name" ];
46
89
NSString * content = [field valueForKey: @" data" ];
@@ -81,19 +124,7 @@ @implementation RNFetchBlob
81
124
NSOperationQueue *queue = [[NSOperationQueue alloc ] init ];
82
125
[NSURLConnection sendAsynchronousRequest: request queue: queue completionHandler: ^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
83
126
84
- NSHTTPURLResponse * resp = (NSHTTPURLResponse *) response;
85
- NSString * status = [NSString stringWithFormat: @" %d " , resp.statusCode];
86
-
87
- if (connectionError)
88
- {
89
- callback (@[[connectionError localizedDescription ], [NSNull null ]]);
90
- }
91
- else if (![status isEqualToString: @" 200" ]) {
92
- callback (@[status, [NSNull null ]]);
93
- }
94
- else {
95
- callback (@[[NSNull null ], [data base64EncodedStringWithOptions: 0 ]]);
96
- }
127
+ [FetchBlobUtils onBlobResponse: response withData: data withError: connectionError withCallback: callback];
97
128
98
129
}];
99
130
@@ -107,16 +138,12 @@ @implementation RNFetchBlob
107
138
initWithURL: [NSURL
108
139
URLWithString: url]];
109
140
110
- NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] init ];
111
-
112
- // make headers case insensitive
113
- for (NSString * key in headers) {
114
- [mheaders setValue: [headers valueForKey: key] forKey: [key lowercaseString ]];
115
- }
141
+ NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [FetchBlobUtils normalizeHeaders: headers]];
116
142
117
143
// if method is POST or PUT, convert data string format
118
144
if ([[method lowercaseString ] isEqualToString: @" post" ] || [[method lowercaseString ] isEqualToString: @" put" ]) {
119
145
146
+ // generate octet-stream body
120
147
NSData * blobData = [[NSData alloc ] initWithBase64EncodedString: body options: 0 ];
121
148
NSMutableData * postBody = [[NSMutableData alloc ] init ];
122
149
[postBody appendData: [NSData dataWithData: blobData]];
@@ -132,23 +159,10 @@ @implementation RNFetchBlob
132
159
NSOperationQueue *queue = [[NSOperationQueue alloc ] init ];
133
160
[NSURLConnection sendAsynchronousRequest: request queue: queue completionHandler: ^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
134
161
135
- NSHTTPURLResponse * resp = (NSHTTPURLResponse *) response;
136
- NSString * status = [NSString stringWithFormat: @" %d " , resp.statusCode];
137
-
138
- if (connectionError)
139
- {
140
- callback (@[[connectionError localizedDescription ], [NSNull null ]]);
141
- }
142
- else if (![status isEqualToString: @" 200" ]) {
143
- callback (@[status, [NSNull null ]]);
144
- }
145
- else {
146
- callback (@[[NSNull null ], [data base64EncodedStringWithOptions: 0 ]]);
147
- }
162
+ [FetchBlobUtils onBlobResponse: response withData: data withError: connectionError withCallback: callback];
148
163
149
164
}];
150
165
151
166
}
167
+ @end
152
168
153
-
154
- @end
0 commit comments