Преглед на файлове

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

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

+ 1
- 0
src/ios/RNFetchBlob/RNFetchBlob.h Целия файл

18
 // config
18
 // config
19
 extern NSString *const CONFIG_USE_TEMP;
19
 extern NSString *const CONFIG_USE_TEMP;
20
 extern NSString *const CONFIG_FILE_PATH;
20
 extern NSString *const CONFIG_FILE_PATH;
21
+extern NSString *const CONFIG_FILE_EXT;
21
 
22
 
22
 // fs events
23
 // fs events
23
 extern NSString *const FS_EVENT_DATA;
24
 extern NSString *const FS_EVENT_DATA;

+ 18
- 30
src/ios/RNFetchBlob/RNFetchBlob.m Целия файл

11
 #import "RCTBridge.h"
11
 #import "RCTBridge.h"
12
 #import "RCTEventDispatcher.h"
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
 NSString *const MSG_EVENT = @"RNFetchBlobMessage";
19
 NSString *const MSG_EVENT = @"RNFetchBlobMessage";
16
 NSString *const MSG_EVENT_LOG = @"log";
20
 NSString *const MSG_EVENT_LOG = @"log";
17
 NSString *const MSG_EVENT_WARN = @"warn";
21
 NSString *const MSG_EVENT_WARN = @"warn";
18
 NSString *const MSG_EVENT_ERROR = @"error";
22
 NSString *const MSG_EVENT_ERROR = @"error";
19
-NSString *const CONFIG_USE_TEMP = @"fileCache";
20
-NSString *const CONFIG_FILE_PATH = @"path";
21
 NSString *const FS_EVENT_DATA = @"data";
23
 NSString *const FS_EVENT_DATA = @"data";
22
 NSString *const FS_EVENT_END = @"end";
24
 NSString *const FS_EVENT_END = @"end";
23
 NSString *const FS_EVENT_WARN = @"warn";
25
 NSString *const FS_EVENT_WARN = @"warn";
62
     return [NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES) firstObject];
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
     NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
70
     NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
68
     NSString * filename = [NSString stringWithFormat:@"/RNFetchBlobTmp_%@", taskId];
71
     NSString * filename = [NSString stringWithFormat:@"/RNFetchBlobTmp_%@", taskId];
72
+    if(ext != nil)
73
+        filename = [filename stringByAppendingString: [NSString stringWithFormat:@".%@", ext]];
69
     NSString * tempPath = [documentDir stringByAppendingString: filename];
74
     NSString * tempPath = [documentDir stringByAppendingString: filename];
70
     return tempPath;
75
     return tempPath;
71
 }
76
 }
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
 // Write file chunk into an opened stream
96
 // Write file chunk into an opened stream
100
 - (void)write:(NSData *) chunk toPath:(NSString *) path{
97
 - (void)write:(NSData *) chunk toPath:(NSString *) path{
101
     NSUInteger left = [chunk length];
98
     NSUInteger left = [chunk length];
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
 // close file write stream
118
 // close file write stream
130
 - (void)closeOutStream {
119
 - (void)closeOutStream {
279
     self.options = options;
268
     self.options = options;
280
     
269
     
281
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
270
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
271
+    NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
282
     
272
     
283
     // open file stream for write
273
     // open file stream for write
284
     if( path != nil) {
274
     if( path != nil) {
287
     }
277
     }
288
     else if ( [self.options valueForKey:CONFIG_USE_TEMP]!= nil ) {
278
     else if ( [self.options valueForKey:CONFIG_USE_TEMP]!= nil ) {
289
         self.fileStream = [[FetchBlobFS alloc]initWithCallback:self.callback];
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
     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO];
283
     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO];
319
     }
309
     }
320
     // write to tmp file
310
     // write to tmp file
321
     else if( fileCache != nil) {
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
     // cache data in memory
315
     // cache data in memory
325
     else {
316
     else {
381
         data = [[NSData alloc] init];
372
         data = [[NSData alloc] init];
382
     
373
     
383
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
374
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
375
+    NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
384
     Boolean useCache = [self.options valueForKey:CONFIG_USE_TEMP];
376
     Boolean useCache = [self.options valueForKey:CONFIG_USE_TEMP];
385
     
377
     
386
     [self.fileStream closeInStream];
378
     [self.fileStream closeInStream];
391
     }
383
     }
392
     // when fileCache option is set but no path specified, save to tmp path
384
     // when fileCache option is set but no path specified, save to tmp path
393
     else if( [self.options valueForKey:CONFIG_USE_TEMP] != nil) {
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
         callback(@[[NSNull null], tmpPath]);
387
         callback(@[[NSNull null], tmpPath]);
396
     }
388
     }
397
     // otherwise return base64 string
389
     // otherwise return base64 string
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
     FetchBlobFS *fileStream = [[FetchBlobFS alloc] initWithBridgeRef:self.bridge];
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
     NSError * error = nil;
527
     NSError * error = nil;
536
     NSString * tmpPath = nil;
528
     NSString * tmpPath = nil;
537
-    if(path != nil)
538
-        tmpPath = path;
539
-    else
540
-        tmpPath = [FetchBlobFS getTempPath:taskId];
541
     [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
529
     [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
542
 }
530
 }
543
 
531