10
10
#import " RCTBridge.h"
11
11
#import " RCTEventDispatcher.h"
12
12
#import " RNFetchBlobFS.h"
13
- #import " RNFetchBlobResp .h"
13
+ #import " RNFetchBlobNetwork .h"
14
14
#import " RNFetchBlobConst.h"
15
15
16
16
@@ -61,13 +61,13 @@ - (NSDictionary *)constantsToExport
61
61
form:(NSArray *)form
62
62
callback:(RCTResponseSenderBlock)callback)
63
63
{
64
-
64
+ NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
65
65
// send request
66
66
NSMutableURLRequest *request = [[NSMutableURLRequest alloc ]
67
67
initWithURL: [NSURL
68
- URLWithString: url ]];
68
+ URLWithString: encodedUrl ]];
69
69
70
- NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [ FetchBlobUtils normalizeHeaders: headers]];
70
+ NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [ RNFetchBlobNetwork normalizeHeaders: headers]];
71
71
72
72
73
73
NSTimeInterval timeStamp = [[NSDate date ] timeIntervalSince1970 ];
@@ -76,9 +76,9 @@ - (NSDictionary *)constantsToExport
76
76
// generate boundary
77
77
NSString * boundary = [NSString stringWithFormat: @" RNFetchBlob%d " , timeStampObj];
78
78
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
79
+ NSMutableData * postData = [[NSMutableData alloc ] init ];
79
80
// if method is POST or PUT, convert data string format
80
81
if ([[method lowercaseString ] isEqualToString: @" post" ] || [[method lowercaseString ] isEqualToString: @" put" ]) {
81
- NSMutableData * postData = [[NSMutableData alloc ] init ];
82
82
83
83
// combine multipart/form-data body
84
84
for (id field in form) {
@@ -128,7 +128,7 @@ - (NSDictionary *)constantsToExport
128
128
129
129
130
130
// send HTTP request
131
- FetchBlobUtils * utils = [[FetchBlobUtils alloc ] init ];
131
+ RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc ] init ];
132
132
[utils sendRequest: options bridge: self .bridge taskId: taskId withRequest: request callback: callback];
133
133
});
134
134
}
@@ -141,29 +141,33 @@ - (NSDictionary *)constantsToExport
141
141
headers:(NSDictionary *)headers
142
142
body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
143
143
{
144
+ NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
144
145
// send request
145
146
NSMutableURLRequest *request = [[NSMutableURLRequest alloc ]
146
147
initWithURL: [NSURL
147
- URLWithString: url ]];
148
+ URLWithString: encodedUrl ]];
148
149
149
- NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [FetchBlobUtils normalizeHeaders: headers]];
150
+ NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [RNFetchBlobNetwork normalizeHeaders: headers]];
150
151
// move heavy task to another thread
151
152
dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
153
+ NSMutableData * blobData;
152
154
// if method is POST or PUT, convert data string format
153
155
if ([[method lowercaseString ] isEqualToString: @" post" ] || [[method lowercaseString ] isEqualToString: @" put" ]) {
154
156
// generate octet-stream body
155
157
if (body != nil ) {
156
- NSMutableData * blobData;
157
158
158
159
// when body is a string contains file path prefix, try load file from the path
159
160
if ([body hasPrefix: self .filePathPrefix]) {
160
161
NSString * orgPath = [body substringFromIndex: [self .filePathPrefix length ]];
161
- blobData = [[NSData alloc ] initWithContentsOfFile: orgPath];
162
+ [request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath: orgPath ]];
163
+ // blobData = [[NSData alloc] initWithContentsOfFile:orgPath];
162
164
}
163
165
// otherwise convert it as BASE64 data string
164
- else
166
+ else {
165
167
blobData = [[NSData alloc ] initWithBase64EncodedString: body options: 0 ];
166
- [request setHTTPBody: blobData];
168
+ [request setHTTPBody: blobData];
169
+ }
170
+
167
171
[mheaders setValue: @" application/octet-stream" forKey: @" content-type" ];
168
172
169
173
}
@@ -173,7 +177,7 @@ - (NSDictionary *)constantsToExport
173
177
[request setAllHTTPHeaderFields: mheaders];
174
178
175
179
// send HTTP request
176
- FetchBlobUtils * utils = [[FetchBlobUtils alloc ] init ];
180
+ RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc ] init ];
177
181
[utils sendRequest: options bridge: self .bridge taskId: taskId withRequest: request callback: callback];
178
182
});
179
183
}
0 commit comments