|
@@ -11,13 +11,15 @@
|
11
|
11
|
#import "RCTBridge.h"
|
12
|
12
|
#import "RCTEventDispatcher.h"
|
13
|
13
|
|
14
|
|
-// lib event
|
|
14
|
+// fetch configs
|
|
15
|
+NSString *const CONFIG_USE_TEMP = @"fileCache";
|
|
16
|
+NSString *const CONFIG_FILE_PATH = @"path";
|
|
17
|
+NSString *const CONFIG_FILE_EXT = @"appendExt";
|
|
18
|
+
|
15
|
19
|
NSString *const MSG_EVENT = @"RNFetchBlobMessage";
|
16
|
20
|
NSString *const MSG_EVENT_LOG = @"log";
|
17
|
21
|
NSString *const MSG_EVENT_WARN = @"warn";
|
18
|
22
|
NSString *const MSG_EVENT_ERROR = @"error";
|
19
|
|
-NSString *const CONFIG_USE_TEMP = @"fileCache";
|
20
|
|
-NSString *const CONFIG_FILE_PATH = @"path";
|
21
|
23
|
NSString *const FS_EVENT_DATA = @"data";
|
22
|
24
|
NSString *const FS_EVENT_END = @"end";
|
23
|
25
|
NSString *const FS_EVENT_WARN = @"warn";
|
|
@@ -62,10 +64,13 @@ NSString *const FS_EVENT_ERROR = @"error";
|
62
|
64
|
return [NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES) firstObject];
|
63
|
65
|
}
|
64
|
66
|
|
65
|
|
-+ (NSString *) getTempPath:(NSString*)taskId {
|
66
|
67
|
|
|
68
|
++ (NSString *) getTempPath:(NSString*)taskId withExtension:(NSString *)ext {
|
|
69
|
+
|
67
|
70
|
NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
|
68
|
71
|
NSString * filename = [NSString stringWithFormat:@"/RNFetchBlobTmp_%@", taskId];
|
|
72
|
+ if(ext != nil)
|
|
73
|
+ filename = [filename stringByAppendingString: [NSString stringWithFormat:@".%@", ext]];
|
69
|
74
|
NSString * tempPath = [documentDir stringByAppendingString: filename];
|
70
|
75
|
return tempPath;
|
71
|
76
|
}
|
|
@@ -88,14 +93,6 @@ NSString *const FS_EVENT_ERROR = @"error";
|
88
|
93
|
}
|
89
|
94
|
|
90
|
95
|
|
91
|
|
-- (void)openWithId:(NSString *)taskId {
|
92
|
|
-
|
93
|
|
- NSString * tmpPath = [[self class ]getTempPath: taskId];
|
94
|
|
- // create a file stream
|
95
|
|
- [self openWithPath:tmpPath];
|
96
|
|
-
|
97
|
|
-}
|
98
|
|
-
|
99
|
96
|
// Write file chunk into an opened stream
|
100
|
97
|
- (void)write:(NSData *) chunk toPath:(NSString *) path{
|
101
|
98
|
NSUInteger left = [chunk length];
|
|
@@ -117,14 +114,6 @@ NSString *const FS_EVENT_ERROR = @"error";
|
117
|
114
|
|
118
|
115
|
}
|
119
|
116
|
|
120
|
|
-- (void)readWithTaskId:(NSString *)taskId withPath:(NSString *)path useEncoding:(NSString *)encoding {
|
121
|
|
- self.taskId = taskId;
|
122
|
|
- self.path = path;
|
123
|
|
- if(path == nil)
|
124
|
|
- [self readWithPath:[[self class]getTempPath:taskId] useEncoding:encoding];
|
125
|
|
- else
|
126
|
|
- [self readWithPath:path useEncoding:encoding];
|
127
|
|
-}
|
128
|
117
|
|
129
|
118
|
// close file write stream
|
130
|
119
|
- (void)closeOutStream {
|
|
@@ -279,6 +268,7 @@ NSString *const FS_EVENT_ERROR = @"error";
|
279
|
268
|
self.options = options;
|
280
|
269
|
|
281
|
270
|
NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
|
|
271
|
+ NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
|
282
|
272
|
|
283
|
273
|
// open file stream for write
|
284
|
274
|
if( path != nil) {
|
|
@@ -287,7 +277,7 @@ NSString *const FS_EVENT_ERROR = @"error";
|
287
|
277
|
}
|
288
|
278
|
else if ( [self.options valueForKey:CONFIG_USE_TEMP]!= nil ) {
|
289
|
279
|
self.fileStream = [[FetchBlobFS alloc]initWithCallback:self.callback];
|
290
|
|
- [self.fileStream openWithId:taskId];
|
|
280
|
+ [self.fileStream openWithPath:[FetchBlobFS getTempPath:taskId withExtension:ext]];
|
291
|
281
|
}
|
292
|
282
|
|
293
|
283
|
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO];
|
|
@@ -319,7 +309,8 @@ NSString *const FS_EVENT_ERROR = @"error";
|
319
|
309
|
}
|
320
|
310
|
// write to tmp file
|
321
|
311
|
else if( fileCache != nil) {
|
322
|
|
- [self.fileStream write:data toPath:[FetchBlobFS getTempPath:self.taskId ]];
|
|
312
|
+ NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
|
|
313
|
+ [self.fileStream write:data toPath:[FetchBlobFS getTempPath:self.taskId withExtension:ext]];
|
323
|
314
|
}
|
324
|
315
|
// cache data in memory
|
325
|
316
|
else {
|
|
@@ -381,6 +372,7 @@ NSString *const FS_EVENT_ERROR = @"error";
|
381
|
372
|
data = [[NSData alloc] init];
|
382
|
373
|
|
383
|
374
|
NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
|
|
375
|
+ NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
|
384
|
376
|
Boolean useCache = [self.options valueForKey:CONFIG_USE_TEMP];
|
385
|
377
|
|
386
|
378
|
[self.fileStream closeInStream];
|
|
@@ -391,7 +383,7 @@ NSString *const FS_EVENT_ERROR = @"error";
|
391
|
383
|
}
|
392
|
384
|
// when fileCache option is set but no path specified, save to tmp path
|
393
|
385
|
else if( [self.options valueForKey:CONFIG_USE_TEMP] != nil) {
|
394
|
|
- NSString * tmpPath = [FetchBlobFS getTempPath:taskId];
|
|
386
|
+ NSString * tmpPath = [FetchBlobFS getTempPath:taskId withExtension:ext];
|
395
|
387
|
callback(@[[NSNull null], tmpPath]);
|
396
|
388
|
}
|
397
|
389
|
// otherwise return base64 string
|
|
@@ -526,18 +518,14 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
|
526
|
518
|
|
527
|
519
|
}
|
528
|
520
|
|
529
|
|
-RCT_EXPORT_METHOD(readStream:(NSString *)taskId withPath:(NSString *)path withEncoding:(NSString *)encoding) {
|
|
521
|
+RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding) {
|
530
|
522
|
FetchBlobFS *fileStream = [[FetchBlobFS alloc] initWithBridgeRef:self.bridge];
|
531
|
|
- [fileStream readWithTaskId:taskId withPath:path useEncoding:encoding];
|
|
523
|
+ [fileStream readWithPath:path useEncoding:encoding];
|
532
|
524
|
}
|
533
|
525
|
|
534
|
|
-RCT_EXPORT_METHOD(flush:(NSString *)taskId withPath:(NSString *)path) {
|
|
526
|
+RCT_EXPORT_METHOD(flush:(NSString *)path) {
|
535
|
527
|
NSError * error = nil;
|
536
|
528
|
NSString * tmpPath = nil;
|
537
|
|
- if(path != nil)
|
538
|
|
- tmpPath = path;
|
539
|
|
- else
|
540
|
|
- tmpPath = [FetchBlobFS getTempPath:taskId];
|
541
|
529
|
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
|
542
|
530
|
}
|
543
|
531
|
|