|
@@ -62,14 +62,14 @@ RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
|
62
|
62
|
form:(NSArray *)form
|
63
|
63
|
callback:(RCTResponseSenderBlock)callback)
|
64
|
64
|
{
|
65
|
|
-
|
|
65
|
+
|
66
|
66
|
[RNFetchBlobReqBuilder buildMultipartRequest:options taskId:taskId method:method url:url headers:headers form:form onComplete:^(NSURLRequest *req, long bodyLength) {
|
67
|
67
|
// send HTTP request
|
68
|
68
|
RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
|
69
|
69
|
[utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
|
70
|
70
|
utils = nil;
|
71
|
71
|
}];
|
72
|
|
-
|
|
72
|
+
|
73
|
73
|
}
|
74
|
74
|
|
75
|
75
|
// Fetch blob data request
|
|
@@ -80,20 +80,29 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
|
80
|
80
|
headers:(NSDictionary *)headers
|
81
|
81
|
body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
|
82
|
82
|
{
|
83
|
|
- [RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
|
84
|
|
- // send HTTP request
|
85
|
|
- RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
|
86
|
|
- [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
|
87
|
|
- utils = nil;
|
88
|
|
- }];
|
89
|
|
-
|
|
83
|
+ NSString *cType = [headers valueForKey:"content-type"]
|
|
84
|
+ if (cType != nil && cType == @"application/x-www-form-urlencoded") {
|
|
85
|
+ [RNFetchBlobReqBuilder buildEncodedRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
|
|
86
|
+ // send HTTP request
|
|
87
|
+ RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
|
|
88
|
+ [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
|
|
89
|
+ utils = nil;
|
|
90
|
+ }];
|
|
91
|
+ } else {
|
|
92
|
+ [RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
|
|
93
|
+ // send HTTP request
|
|
94
|
+ RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
|
|
95
|
+ [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
|
|
96
|
+ utils = nil;
|
|
97
|
+ }];
|
|
98
|
+ }
|
90
|
99
|
}
|
91
|
100
|
|
92
|
101
|
RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NSString *)encoding callback:(RCTResponseSenderBlock)callback) {
|
93
|
|
-
|
|
102
|
+
|
94
|
103
|
NSFileManager * fm = [NSFileManager defaultManager];
|
95
|
104
|
NSData * fileContent = nil;
|
96
|
|
-
|
|
105
|
+
|
97
|
106
|
if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
|
98
|
107
|
fileContent = [[NSData alloc] initWithData:[data dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
|
99
|
108
|
}
|
|
@@ -103,18 +112,18 @@ RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NS
|
103
|
112
|
else {
|
104
|
113
|
fileContent = [[NSData alloc] initWithData:[data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
|
105
|
114
|
}
|
106
|
|
-
|
|
115
|
+
|
107
|
116
|
BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
|
108
|
117
|
if(success == YES)
|
109
|
118
|
callback(@[[NSNull null]]);
|
110
|
119
|
else
|
111
|
120
|
callback(@[[NSString stringWithFormat:@"failed to create new file at path %@ please ensure the folder exists"]]);
|
112
|
|
-
|
|
121
|
+
|
113
|
122
|
}
|
114
|
123
|
|
115
|
124
|
// method for create file with ASCII content
|
116
|
125
|
RCT_EXPORT_METHOD(createFileASCII:(NSString *)path data:(NSArray *)dataArray callback:(RCTResponseSenderBlock)callback) {
|
117
|
|
-
|
|
126
|
+
|
118
|
127
|
NSFileManager * fm = [NSFileManager defaultManager];
|
119
|
128
|
NSMutableData * fileContent = [NSMutableData alloc];
|
120
|
129
|
// prevent stack overflow, alloc on heap
|
|
@@ -130,7 +139,7 @@ RCT_EXPORT_METHOD(createFileASCII:(NSString *)path data:(NSArray *)dataArray cal
|
130
|
139
|
callback(@[[NSNull null]]);
|
131
|
140
|
else
|
132
|
141
|
callback(@[[NSString stringWithFormat:@"failed to create new file at path %@ please ensure the folder exists"]]);
|
133
|
|
-
|
|
142
|
+
|
134
|
143
|
}
|
135
|
144
|
|
136
|
145
|
|
|
@@ -139,7 +148,7 @@ RCT_EXPORT_METHOD(exists:(NSString *)path callback:(RCTResponseSenderBlock)callb
|
139
|
148
|
BOOL exists = NO;
|
140
|
149
|
exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory: &isDir];
|
141
|
150
|
callback(@[@(exists), @(isDir)]);
|
142
|
|
-
|
|
151
|
+
|
143
|
152
|
}
|
144
|
153
|
|
145
|
154
|
RCT_EXPORT_METHOD(writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
|
@@ -202,7 +211,7 @@ RCT_EXPORT_METHOD(unlink:(NSString *)path callback:(RCTResponseSenderBlock) call
|
202
|
211
|
RCT_EXPORT_METHOD(removeSession:(NSArray *)paths callback:(RCTResponseSenderBlock) callback) {
|
203
|
212
|
NSError * error = nil;
|
204
|
213
|
NSString * tmpPath = nil;
|
205
|
|
-
|
|
214
|
+
|
206
|
215
|
for(NSString * path in paths) {
|
207
|
216
|
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
|
208
|
217
|
if(error != nil) {
|
|
@@ -211,7 +220,7 @@ RCT_EXPORT_METHOD(removeSession:(NSArray *)paths callback:(RCTResponseSenderBloc
|
211
|
220
|
}
|
212
|
221
|
}
|
213
|
222
|
callback(@[[NSNull null]]);
|
214
|
|
-
|
|
223
|
+
|
215
|
224
|
}
|
216
|
225
|
|
217
|
226
|
RCT_EXPORT_METHOD(ls:(NSString *)path callback:(RCTResponseSenderBlock) callback) {
|
|
@@ -225,12 +234,12 @@ RCT_EXPORT_METHOD(ls:(NSString *)path callback:(RCTResponseSenderBlock) callback
|
225
|
234
|
}
|
226
|
235
|
NSError * error = nil;
|
227
|
236
|
NSArray * result = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
|
228
|
|
-
|
|
237
|
+
|
229
|
238
|
if(error == nil)
|
230
|
239
|
callback(@[[NSNull null], result == nil ? [NSNull null] :result ]);
|
231
|
240
|
else
|
232
|
241
|
callback(@[[error localizedDescription], [NSNull null]]);
|
233
|
|
-
|
|
242
|
+
|
234
|
243
|
}
|
235
|
244
|
|
236
|
245
|
RCT_EXPORT_METHOD(stat:(NSString *)path callback:(RCTResponseSenderBlock) callback) {
|
|
@@ -238,30 +247,30 @@ RCT_EXPORT_METHOD(stat:(NSString *)path callback:(RCTResponseSenderBlock) callba
|
238
|
247
|
BOOL exist = nil;
|
239
|
248
|
BOOL isDir = nil;
|
240
|
249
|
NSError * error = nil;
|
241
|
|
-
|
|
250
|
+
|
242
|
251
|
path = [RNFetchBlobFS getPathOfAsset:path];
|
243
|
|
-
|
|
252
|
+
|
244
|
253
|
exist = [fm fileExistsAtPath:path isDirectory:&isDir];
|
245
|
254
|
if(exist == NO) {
|
246
|
255
|
callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not exist", path]]);
|
247
|
256
|
return ;
|
248
|
257
|
}
|
249
|
258
|
NSData * res = [RNFetchBlobFS stat:path error:&error];
|
250
|
|
-
|
|
259
|
+
|
251
|
260
|
if(error == nil)
|
252
|
261
|
callback(@[[NSNull null], res]);
|
253
|
262
|
else
|
254
|
263
|
callback(@[[error localizedDescription], [NSNull null]]);
|
255
|
|
-
|
|
264
|
+
|
256
|
265
|
}
|
257
|
266
|
|
258
|
267
|
RCT_EXPORT_METHOD(lstat:(NSString *)path callback:(RCTResponseSenderBlock) callback) {
|
259
|
268
|
NSFileManager* fm = [NSFileManager defaultManager];
|
260
|
269
|
BOOL exist = nil;
|
261
|
270
|
BOOL isDir = nil;
|
262
|
|
-
|
|
271
|
+
|
263
|
272
|
path = [RNFetchBlobFS getPathOfAsset:path];
|
264
|
|
-
|
|
273
|
+
|
265
|
274
|
exist = [fm fileExistsAtPath:path isDirectory:&isDir];
|
266
|
275
|
if(exist == NO) {
|
267
|
276
|
callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not exist", path]]);
|
|
@@ -269,7 +278,7 @@ RCT_EXPORT_METHOD(lstat:(NSString *)path callback:(RCTResponseSenderBlock) callb
|
269
|
278
|
}
|
270
|
279
|
NSError * error = nil;
|
271
|
280
|
NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
|
272
|
|
-
|
|
281
|
+
|
273
|
282
|
NSMutableArray * res = [[NSMutableArray alloc] init];
|
274
|
283
|
if(isDir == YES) {
|
275
|
284
|
for(NSString * p in files) {
|
|
@@ -280,35 +289,35 @@ RCT_EXPORT_METHOD(lstat:(NSString *)path callback:(RCTResponseSenderBlock) callb
|
280
|
289
|
else {
|
281
|
290
|
[res addObject:[RNFetchBlobFS stat:path error:&error]];
|
282
|
291
|
}
|
283
|
|
-
|
|
292
|
+
|
284
|
293
|
if(error == nil)
|
285
|
294
|
callback(@[[NSNull null], res == nil ? [NSNull null] :res ]);
|
286
|
295
|
else
|
287
|
296
|
callback(@[[error localizedDescription], [NSNull null]]);
|
288
|
|
-
|
|
297
|
+
|
289
|
298
|
}
|
290
|
299
|
|
291
|
300
|
RCT_EXPORT_METHOD(cp:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {
|
292
|
301
|
NSError * error = nil;
|
293
|
302
|
path = [RNFetchBlobFS getPathOfAsset:path];
|
294
|
303
|
BOOL result = [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
|
295
|
|
-
|
|
304
|
+
|
296
|
305
|
if(error == nil)
|
297
|
306
|
callback(@[[NSNull null], @YES]);
|
298
|
307
|
else
|
299
|
308
|
callback(@[[error localizedDescription], @NO]);
|
300
|
|
-
|
|
309
|
+
|
301
|
310
|
}
|
302
|
311
|
|
303
|
312
|
RCT_EXPORT_METHOD(mv:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback) {
|
304
|
313
|
NSError * error = nil;
|
305
|
314
|
BOOL result = [[NSFileManager defaultManager] moveItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
|
306
|
|
-
|
|
315
|
+
|
307
|
316
|
if(error == nil)
|
308
|
317
|
callback(@[[NSNull null], @YES]);
|
309
|
318
|
else
|
310
|
319
|
callback(@[[error localizedDescription], @NO]);
|
311
|
|
-
|
|
320
|
+
|
312
|
321
|
}
|
313
|
322
|
|
314
|
323
|
RCT_EXPORT_METHOD(mkdir:(NSString *)path callback:(RCTResponseSenderBlock) callback) {
|
|
@@ -322,12 +331,12 @@ RCT_EXPORT_METHOD(mkdir:(NSString *)path callback:(RCTResponseSenderBlock) callb
|
322
|
331
|
}
|
323
|
332
|
|
324
|
333
|
RCT_EXPORT_METHOD(readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
|
325
|
|
-
|
|
334
|
+
|
326
|
335
|
[RNFetchBlobFS readFile:path encoding:encoding resolver:resolve rejecter:reject onComplete:nil];
|
327
|
336
|
})
|
328
|
337
|
|
329
|
338
|
RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize) {
|
330
|
|
-
|
|
339
|
+
|
331
|
340
|
RNFetchBlobFS *fileStream = [[RNFetchBlobFS alloc] initWithBridgeRef:self.bridge];
|
332
|
341
|
if(bufferSize == nil) {
|
333
|
342
|
if([[encoding lowercaseString] isEqualToString:@"base64"])
|
|
@@ -340,7 +349,7 @@ RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding
|
340
|
349
|
}
|
341
|
350
|
|
342
|
351
|
RCT_EXPORT_METHOD(getEnvironmentDirs:(RCTResponseSenderBlock) callback) {
|
343
|
|
-
|
|
352
|
+
|
344
|
353
|
callback(@[
|
345
|
354
|
[RNFetchBlobFS getDocumentDir],
|
346
|
355
|
[RNFetchBlobFS getCacheDir],
|
|
@@ -350,7 +359,7 @@ RCT_EXPORT_METHOD(getEnvironmentDirs:(RCTResponseSenderBlock) callback) {
|
350
|
359
|
RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBlock)callback) {
|
351
|
360
|
[RNFetchBlobNetwork cancelRequest:taskId];
|
352
|
361
|
callback(@[[NSNull null], taskId]);
|
353
|
|
-
|
|
362
|
+
|
354
|
363
|
}
|
355
|
364
|
|
356
|
365
|
#pragma mark RNFetchBlob private methods
|