12
12
#import " RNFetchBlobFS.h"
13
13
#import " RNFetchBlobNetwork.h"
14
14
#import " RNFetchBlobConst.h"
15
+ #import " RNFetchBlobReqBuilder.h"
15
16
16
17
17
18
// //////////////////////////////////////
@@ -61,78 +62,14 @@ - (NSDictionary *)constantsToExport
61
62
form:(NSArray *)form
62
63
callback:(RCTResponseSenderBlock)callback)
63
64
{
64
- NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
65
- // send request
66
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc ]
67
- initWithURL: [NSURL
68
- URLWithString: encodedUrl]];
69
65
70
- NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [ RNFetchBlobNetwork normalizeHeaders: headers]];
71
-
72
-
73
- NSTimeInterval timeStamp = [[NSDate date ] timeIntervalSince1970 ];
74
- NSNumber * timeStampObj = [NSNumber numberWithDouble: timeStamp];
75
-
76
- // generate boundary
77
- NSString * boundary = [NSString stringWithFormat: @" RNFetchBlob%d " , timeStampObj];
78
- dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
79
- NSMutableData * postData = [[NSMutableData alloc ] init ];
80
- // if method is POST or PUT, convert data string format
81
- if ([[method lowercaseString ] isEqualToString: @" post" ] || [[method lowercaseString ] isEqualToString: @" put" ]) {
82
-
83
- // combine multipart/form-data body
84
- for (id field in form) {
85
- NSString * name = [field valueForKey: @" name" ];
86
- NSString * content = [field valueForKey: @" data" ];
87
- // field is a text field
88
- if ([field valueForKey: @" filename" ] == nil || content == [NSNull null ]) {
89
- [postData appendData: [[NSString stringWithFormat: @" --%@ \r\n " , boundary] dataUsingEncoding: NSUTF8StringEncoding]];
90
- [postData appendData: [[NSString stringWithFormat: @" Content-Disposition: form-data; name=\" %@ \"\r\n " , name] dataUsingEncoding: NSUTF8StringEncoding]];
91
- [postData appendData: [[NSString stringWithFormat: @" Content-Type: text/plain\r\n\r\n " ] dataUsingEncoding: NSUTF8StringEncoding]];
92
- [postData appendData: [[NSString stringWithFormat: @" %@ \r\n " , content] dataUsingEncoding: NSUTF8StringEncoding]];
93
- }
94
- // field contains a file
95
- else {
96
- NSMutableData * blobData;
97
- if (content != nil ) {
98
- if ([content hasPrefix: self .filePathPrefix]) {
99
- NSString * orgPath = [content substringFromIndex: [self .filePathPrefix length ]];
100
- orgPath = [RNFetchBlobFS getPathOfAsset: orgPath];
101
- blobData = [[NSData alloc ] initWithContentsOfFile: orgPath];
102
- }
103
- else
104
- blobData = [[NSData alloc ] initWithBase64EncodedString: content options: 0 ];
105
- }
106
- NSString * filename = [field valueForKey: @" filename" ];
107
- [postData appendData: [[NSString stringWithFormat: @" --%@ \r\n " , boundary] dataUsingEncoding: NSUTF8StringEncoding]];
108
- [postData appendData: [[NSString stringWithFormat: @" Content-Disposition: form-data; name=\" %@ \" ; filename=\" %@ \"\r\n " , name, filename] dataUsingEncoding: NSUTF8StringEncoding]];
109
- [postData appendData: [[NSString stringWithFormat: @" Content-Type: application/octet-stream\r\n\r\n " ] dataUsingEncoding: NSUTF8StringEncoding]];
110
- [postData appendData: blobData];
111
- [postData appendData: [[NSString stringWithFormat: @" \r\n " ] dataUsingEncoding: NSUTF8StringEncoding]];
112
- }
113
-
114
- }
115
-
116
- // close form data
117
- [postData appendData: [[NSString stringWithFormat: @" --%@ --\r\n " , boundary] dataUsingEncoding: NSUTF8StringEncoding]];
118
- [request setHTTPBody: postData];
119
- // set content-length
120
- [mheaders setValue: [NSString stringWithFormat: @" %d " ,[postData length ]] forKey: @" Content-Length" ];
121
- [mheaders setValue: [NSString stringWithFormat: @" 100-continue" ,[postData length ]] forKey: @" Expect" ];
122
- // appaned boundary to content-type
123
- [mheaders setValue: [NSString stringWithFormat: @" multipart/form-data; charset=utf-8; boundary=%@ " , boundary] forKey: @" content-type" ];
124
-
125
- }
126
-
127
- [request setHTTPMethod: method];
128
- [request setAllHTTPHeaderFields: mheaders];
129
-
130
-
66
+ [RNFetchBlobReqBuilder buildMultipartRequest: options taskId: taskId method: method url: url headers: headers form: form onComplete: ^(NSURLRequest *req) {
131
67
// send HTTP request
132
68
RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc ] init ];
133
- [utils sendRequest: options bridge: self .bridge taskId: taskId withRequest: request callback: callback];
69
+ [utils sendRequest: options bridge: self .bridge taskId: taskId withRequest: req callback: callback];
134
70
utils = nil ;
135
- });
71
+ }];
72
+
136
73
}
137
74
138
75
// Fetch blob data request
@@ -143,46 +80,13 @@ - (NSDictionary *)constantsToExport
143
80
headers:(NSDictionary *)headers
144
81
body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
145
82
{
146
- NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
147
- // send request
148
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc ]
149
- initWithURL: [NSURL
150
- URLWithString: encodedUrl]];
151
-
152
- NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [RNFetchBlobNetwork normalizeHeaders: headers]];
153
- // move heavy task to another thread
154
- dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
155
- NSMutableData * blobData;
156
- // if method is POST or PUT, convert data string format
157
- if ([[method lowercaseString ] isEqualToString: @" post" ] || [[method lowercaseString ] isEqualToString: @" put" ]) {
158
- // generate octet-stream body
159
- if (body != nil ) {
160
-
161
- // when body is a string contains file path prefix, try load file from the path
162
- if ([body hasPrefix: self .filePathPrefix]) {
163
- NSString * orgPath = [body substringFromIndex: [self .filePathPrefix length ]];
164
- orgPath = [RNFetchBlobFS getPathOfAsset: orgPath];
165
- [request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath: orgPath ]];
166
- }
167
- // otherwise convert it as BASE64 data string
168
- else {
169
- blobData = [[NSData alloc ] initWithBase64EncodedString: body options: 0 ];
170
- [request setHTTPBody: blobData];
171
- }
172
-
173
- [mheaders setValue: @" application/octet-stream" forKey: @" content-type" ];
174
-
175
- }
176
- }
177
-
178
- [request setHTTPMethod: method];
179
- [request setAllHTTPHeaderFields: mheaders];
180
-
83
+ [RNFetchBlobReqBuilder buildOctetRequest: options taskId: taskId method: method url: url headers: headers body: body onComplete: ^(NSURLRequest *req) {
181
84
// send HTTP request
182
85
RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc ] init ];
183
- [utils sendRequest: options bridge: self .bridge taskId: taskId withRequest: request callback: callback];
86
+ [utils sendRequest: options bridge: self .bridge taskId: taskId withRequest: req callback: callback];
184
87
utils = nil ;
185
- });
88
+ }];
89
+
186
90
}
187
91
188
92
RCT_EXPORT_METHOD (createFile:(NSString *)path data:(NSString *)data encoding:(NSString *)encoding callback:(RCTResponseSenderBlock)callback) {
@@ -419,7 +323,7 @@ - (NSDictionary *)constantsToExport
419
323
420
324
RCT_EXPORT_METHOD (readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
421
325
422
- [RNFetchBlobFS readFile: path encoding: encoding resolver: resolve rejecter: reject];
326
+ [RNFetchBlobFS readFile: path encoding: encoding resolver: resolve rejecter: reject onComplete: nil ];
423
327
})
424
328
425
329
RCT_EXPORT_METHOD (readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int )bufferSize) {
@@ -432,14 +336,7 @@ - (NSDictionary *)constantsToExport
432
336
bufferSize = 4096 ;
433
337
}
434
338
// read asset stream
435
- if ([path hasPrefix: @" assets-library://" ])
436
- {
437
-
438
- }
439
- else
440
- {
441
- [fileStream readWithPath: path useEncoding: encoding bufferSize: bufferSize];
442
- }
339
+ [fileStream readWithPath: path useEncoding: encoding bufferSize: bufferSize];
443
340
}
444
341
445
342
RCT_EXPORT_METHOD (getEnvironmentDirs:(RCTResponseSenderBlock) callback) {
0 commit comments