Browse Source

Fix #89 IOS slice precision

Ben Hsieh 8 years ago
parent
commit
ce596a09fb
2 changed files with 19 additions and 16 deletions
  1. 1
    3
      src/ios/RNFetchBlob/RNFetchBlob.m
  2. 18
    13
      src/ios/RNFetchBlobFS.m

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

364
 
364
 
365
 RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(nonnull NSNumber *)start end:(nonnull NSNumber *)end resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
365
 RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(nonnull NSNumber *)start end:(nonnull NSNumber *)end resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
366
 {
366
 {
367
-     dispatch_sync(dispatch_get_main_queue(),^(void){
368
-         [RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
369
-     });
367
+    [RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
370
 })
368
 })
371
 
369
 
372
 #pragma mark RNFetchBlob private methods
370
 #pragma mark RNFetchBlob private methods

+ 18
- 13
src/ios/RNFetchBlobFS.m View File

542
     // abort for the source file not exists
542
     // abort for the source file not exists
543
     if([fm fileExistsAtPath:path] == NO)
543
     if([fm fileExistsAtPath:path] == NO)
544
     {
544
     {
545
-        reject(@"RNFetchBlob slice failed", @"the file does not exists", path);
545
+        reject(@"RNFetchBlob slice failed : the file does not exists", path, nil);
546
         return;
546
         return;
547
     }
547
     }
548
     long size = [fm attributesOfItemAtPath:path error:nil].fileSize;
548
     long size = [fm attributesOfItemAtPath:path error:nil].fileSize;
549
-    // abort for the file size is less than start
550
-    if(size < [start longValue])
551
-    {
552
-        reject(@"RNFetchBlob slice failed", @"start is greater than file size", @"start is greater than file size");
553
-        return;
554
-    }
549
+    long max = MIN(size, [end longValue]);
550
+    
555
     if(![fm fileExistsAtPath:dest]) {
551
     if(![fm fileExistsAtPath:dest]) {
556
         [fm createFileAtPath:dest contents:@"" attributes:nil];
552
         [fm createFileAtPath:dest contents:@"" attributes:nil];
557
     }
553
     }
558
     [handle seekToFileOffset:[start longValue]];
554
     [handle seekToFileOffset:[start longValue]];
559
     while(read < expected)
555
     while(read < expected)
560
     {
556
     {
561
-
562
-        NSData * chunk = [handle readDataOfLength:10240];
563
-        long remain = expected - read;
564
-        if(remain < 10240)
557
+        
558
+        NSData * chunk;
559
+        long chunkSize = 0;
560
+        if([start longValue] + read + 10240 > max)
565
         {
561
         {
566
-            [os write:[chunk bytes] maxLength:remain];
562
+            NSLog(@"read chunk %lu", max - read - [start longValue]);
563
+            chunkSize = max - read - [start longValue];
564
+            chunk = [handle readDataOfLength:chunkSize];
567
         }
565
         }
568
         else
566
         else
569
         {
567
         {
570
-            [os write:[chunk bytes] maxLength:10240];
568
+            NSLog(@"read chunk %lu", 10240);
569
+            chunkSize = 10240;
570
+            chunk = [handle readDataOfLength:10240];
571
         }
571
         }
572
+        if([chunk length] <= 0)
573
+            break;
574
+        long remain = expected - read;
575
+    
576
+        [os write:[chunk bytes] maxLength:chunkSize];
572
         read += [chunk length];
577
         read += [chunk length];
573
     }
578
     }
574
     [handle closeFile];
579
     [handle closeFile];