Browse Source

Fix IOS uri writeFile function #56

Ben Hsieh 8 years ago
parent
commit
55e2b22a4a
3 changed files with 64 additions and 27 deletions
  1. 2
    1
      src/ios/RNFetchBlob/RNFetchBlob.m
  2. 1
    0
      src/ios/RNFetchBlobFS.h
  3. 61
    26
      src/ios/RNFetchBlobFS.m

+ 2
- 1
src/ios/RNFetchBlob/RNFetchBlob.m View File

@@ -152,7 +152,8 @@ RCT_EXPORT_METHOD(exists:(NSString *)path callback:(RCTResponseSenderBlock)callb
152 152
 }
153 153
 
154 154
 RCT_EXPORT_METHOD(writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
155
-    [RNFetchBlobFS writeFile:path encoding:encoding data:data append:append resolver:resolve rejecter:reject];
155
+    
156
+    [RNFetchBlobFS writeFile:path encoding:[NSString stringWithString:encoding] data:data append:append resolver:resolve rejecter:reject];
156 157
 })
157 158
 
158 159
 RCT_EXPORT_METHOD(writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {

+ 1
- 0
src/ios/RNFetchBlobFS.h View File

@@ -55,6 +55,7 @@
55 55
 + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
56 56
 + (void) readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject onComplete:(void (^)(NSData * content))onComplete;
57 57
 + (void) readAssetFile:(NSData *)assetUrl completionBlock:(void(^)(NSData * content))completionBlock failBlock:(void(^)(NSError * err))failBlock;
58
+//+ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append;
58 59
 
59 60
 // constructor
60 61
 - (id) init;

+ 61
- 26
src/ios/RNFetchBlobFS.m View File

@@ -39,7 +39,7 @@ NSMutableDictionary *fileStreams = nil;
39 39
 
40 40
 // static member getter
41 41
 + (NSArray *) getFileStreams {
42
-
42
+    
43 43
     if(fileStreams == nil)
44 44
         fileStreams = [[NSMutableDictionary alloc] init];
45 45
     return fileStreams;
@@ -58,7 +58,7 @@ NSMutableDictionary *fileStreams = nil;
58 58
     {
59 59
         assetURI = [assetURI stringByReplacingOccurrencesOfString:ASSET_PREFIX withString:@""];
60 60
         assetURI = [[NSBundle mainBundle] pathForResource: [assetURI stringByDeletingPathExtension]
61
-                                               ofType: [assetURI pathExtension]];
61
+                                                   ofType: [assetURI pathExtension]];
62 62
     }
63 63
     return assetURI;
64 64
 }
@@ -84,12 +84,12 @@ NSMutableDictionary *fileStreams = nil;
84 84
 }
85 85
 
86 86
 + (NSString *) getTempPath {
87
-
87
+    
88 88
     return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:@"/RNFetchBlob_tmp"];
89 89
 }
90 90
 
91 91
 + (NSString *) getTempPath:(NSString*)taskId withExtension:(NSString *)ext {
92
-
92
+    
93 93
     NSString * documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
94 94
     NSString * filename = [NSString stringWithFormat:@"/RNFetchBlob_tmp/RNFetchBlobTmp_%@", taskId];
95 95
     if(ext != nil)
@@ -185,31 +185,66 @@ NSMutableDictionary *fileStreams = nil;
185 185
     }
186 186
 }
187 187
 
188
-+ (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject {
188
++ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append
189
+{
190
+    NSInputStream * is = [[NSInputStream alloc] initWithFileAtPath:src];
191
+    NSOutputStream * os = [[NSOutputStream alloc] initToFileAtPath:dest append:append];
192
+    [is open];
193
+    [os open];
194
+    uint8_t buffer[10240];
195
+    int read = [is read:buffer maxLength:10240];
196
+    while(read > 0) {
197
+        [os write:buffer maxLength:read];
198
+        read = [is read:buffer maxLength:10240];
199
+    }
200
+    [os close];
201
+    [is close];
202
+}
203
+
204
++ (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
205
+{
206
+    NSLog(encoding);
189 207
     @try {
190 208
         NSFileManager * fm = [NSFileManager defaultManager];
191 209
         NSError * err = nil;
192 210
         // check if the folder exists, if not exists, create folders recursively
193 211
         // after the folders created, write data into the file
194 212
         NSString * folder = [path stringByDeletingLastPathComponent];
213
+        encoding = [encoding lowercaseString];
195 214
         if(![fm fileExistsAtPath:folder]) {
196 215
             [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:&err];
197 216
         }
198
-        // if file exists, write file by encoding and strategy
217
+        // if file exists, write file using encoding
199 218
         if(![fm fileExistsAtPath:path]) {
200
-            if([[encoding lowercaseString] isEqualToString:@"base64"]){
219
+            if([encoding isEqualToString:@"base64"]){
201 220
                 NSData * byteData = [[NSData alloc] initWithBase64EncodedString:data options:0];
202 221
                 [fm createFileAtPath:path contents:byteData attributes:NULL];
203 222
             }
223
+            // write file from file
224
+            else if([encoding isEqualToString:@"uri"]) {
225
+                [[self class] writeFileFromFile:data toFile:path append:append];
226
+                resolve([NSNull null]);
227
+                return;
228
+            }
229
+            //TODO: from buffer
230
+            // else if ([encoding isEqualToString:@"buffer"]){
231
+            // }
232
+            // write data as UTF8 string
204 233
             else
205 234
                 [fm createFileAtPath:path contents:[data dataUsingEncoding:NSUTF8StringEncoding] attributes:NULL];
206 235
         }
236
+        // file does not exists, create one
207 237
         else {
208 238
             NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
209 239
             NSData * content = nil;
210
-            if([[encoding lowercaseString] isEqualToString:@"base64"]) {
240
+            if([encoding isEqualToString:@"base64"]) {
211 241
                 content = [[NSData alloc] initWithBase64EncodedString:data options:0];
212 242
             }
243
+            else if([encoding isEqualToString:@"uri"]) {
244
+                [[self class] writeFileFromFile:data toFile:path append:append];
245
+                resolve([NSNull null]);
246
+                return;
247
+            }
213 248
             else {
214 249
                 content = [data dataUsingEncoding:NSUTF8StringEncoding];
215 250
             }
@@ -443,11 +478,11 @@ NSMutableDictionary *fileStreams = nil;
443 478
         [self.outStream close];
444 479
         self.outStream = nil;
445 480
     }
446
-
481
+    
447 482
 }
448 483
 
449 484
 - (void)readWithPath:(NSString *)path useEncoding:(NSString *)encoding bufferSize:(int) bufferSize {
450
-
485
+    
451 486
     self.inStream = [[NSInputStream alloc] initWithFileAtPath:path];
452 487
     self.inStream.delegate = self;
453 488
     self.encoding = encoding;
@@ -483,25 +518,25 @@ NSMutableDictionary *fileStreams = nil;
483 518
         [[RNFetchBlobFS getFileStreams] setValue:nil forKey:self.streamId];
484 519
         self.streamId = nil;
485 520
     }
486
-
521
+    
487 522
 }
488 523
 
489 524
 #pragma mark RNFetchBlobFS read stream delegate
490 525
 
491 526
 - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
492
-
527
+    
493 528
     NSString * streamEventCode = [NSString stringWithFormat:@"RNFetchBlobStream+%@", self.path];
494
-
529
+    
495 530
     switch(eventCode) {
496
-
497
-        // write stream event
531
+            
532
+            // write stream event
498 533
         case NSStreamEventHasSpaceAvailable:
499 534
         {
500
-
501
-
535
+            
536
+            
502 537
         }
503
-
504
-        // read stream incoming chunk
538
+            
539
+            // read stream incoming chunk
505 540
         case NSStreamEventHasBytesAvailable:
506 541
         {
507 542
             NSMutableData * chunkData = [[NSMutableData alloc] init];
@@ -535,13 +570,13 @@ NSMutableDictionary *fileStreams = nil;
535 570
                             [asciiArray addObject:[NSNumber numberWithChar:bytePtr[i]]];
536 571
                         }
537 572
                     }
538
-
573
+                    
539 574
                     [self.bridge.eventDispatcher
540 575
                      sendDeviceEventWithName:streamEventCode
541 576
                      body: @{
542 577
                              @"event": FS_EVENT_DATA,
543 578
                              @"detail": asciiArray
544
-                            }
579
+                             }
545 580
                      ];
546 581
                     return;
547 582
                 }
@@ -560,7 +595,7 @@ NSMutableDictionary *fileStreams = nil;
560 595
                      ];
561 596
                     return;
562 597
                 }
563
-
598
+                
564 599
                 [self.bridge.eventDispatcher
565 600
                  sendDeviceEventWithName:streamEventCode
566 601
                  body:@{
@@ -581,8 +616,8 @@ NSMutableDictionary *fileStreams = nil;
581 616
             }
582 617
             break;
583 618
         }
584
-
585
-        // stream error
619
+            
620
+            // stream error
586 621
         case NSStreamEventErrorOccurred:
587 622
         {
588 623
             [self.bridge.eventDispatcher
@@ -594,9 +629,9 @@ NSMutableDictionary *fileStreams = nil;
594 629
              ];
595 630
             break;
596 631
         }
597
-
632
+            
598 633
     }
599
-
634
+    
600 635
 }
601 636
 
602 637
 + (void) getPathFromUri:(NSString *)uri completionHandler:(void(^)(NSString * path, ALAssetRepresentation *asset)) onComplete