Просмотр исходного кода

#2 #3 Add config for customize file extension for tmp files

Ben Hsieh 9 лет назад
Родитель
Сommit
12a96d52dc
3 измененных файлов: 28 добавлений и 39 удалений
  1. 9
    9
      src/index.js
  2. 1
    0
      src/ios/RNFetchBlob/RNFetchBlob.h
  3. 18
    30
      src/ios/RNFetchBlob/RNFetchBlob.m

+ 9
- 9
src/index.js Просмотреть файл

@@ -11,6 +11,13 @@ import {
11 11
   Platform,
12 12
 } from 'react-native'
13 13
 
14
+
15
+type fetchConfig = {
16
+  fileCache : bool,
17
+  path : string,
18
+  appendExt : string
19
+};
20
+
14 21
 type RNFetchBlobNative = {
15 22
   fetchBlob : (
16 23
     options:fetchConfig,
@@ -31,7 +38,6 @@ type RNFetchBlobNative = {
31 38
     callback:(err:any, ...data:any) => void
32 39
   ) => void,
33 40
   readStream : (
34
-    taskId:string,
35 41
     path:string,
36 42
     encode: 'utf8' | 'ascii' | 'base64'
37 43
   ) => void,
@@ -66,16 +72,10 @@ if(!RNFetchBlob || !RNFetchBlob.fetchBlobForm || !RNFetchBlob.fetchBlob) {
66 72
   )
67 73
 }
68 74
 
69
-type fetchConfig = {
70
-  fileCache : bool,
71
-  path : string,
72
-};
73
-
74 75
 function getSystemDirs() {
75 76
   return new Promise((resolve, reject) => {
76 77
     try {
77 78
       RNFetchBlob.getEnvironmentDirs((...dirs) => {
78
-        console.log('##',...dirs)
79 79
         let [PictureDir, MovieDir, DocumentDir, CacheDir] = [...dirs]
80 80
         resolve({PictureDir, MovieDir, DocumentDir, CacheDir})
81 81
       })
@@ -196,7 +196,7 @@ class FetchBlobResponse {
196 196
      * @return {void}
197 197
      */
198 198
     this.flush = () => {
199
-      RNFetchBlob.flush(this.taskId, this.path)
199
+      RNFetchBlob.flush(this.path())
200 200
     }
201 201
 
202 202
     this.path = () => {
@@ -222,7 +222,7 @@ class FetchBlobResponse {
222 222
       })
223 223
 
224 224
       if(this.type === 'path') {
225
-        RNFetchBlob.readStream(this.taskId, this.data, encode)
225
+        RNFetchBlob.readStream(this.data, encode)
226 226
       }
227 227
       else {
228 228
         console.warn('RNFetchblob', 'this response data does not contains any available stream')

+ 1
- 0
src/ios/RNFetchBlob/RNFetchBlob.h Просмотреть файл

@@ -18,6 +18,7 @@ extern NSString *const MSG_EVENT_ERROR;
18 18
 // config
19 19
 extern NSString *const CONFIG_USE_TEMP;
20 20
 extern NSString *const CONFIG_FILE_PATH;
21
+extern NSString *const CONFIG_FILE_EXT;
21 22
 
22 23
 // fs events
23 24
 extern NSString *const FS_EVENT_DATA;

+ 18
- 30
src/ios/RNFetchBlob/RNFetchBlob.m Просмотреть файл

@@ -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