Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit ce596a0

Browse files
committed
Fix #89 IOS slice precision
1 parent 3c15f55 commit ce596a0

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,7 @@ - (NSDictionary *)constantsToExport
364364

365365
RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(nonnull NSNumber *)start end:(nonnull NSNumber *)end resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject
366366
{
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];
370368
})
371369

372370
#pragma mark RNFetchBlob private methods

src/ios/RNFetchBlobFS.m

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -542,33 +542,38 @@ + (void)slice:(NSString *)path
542542
// abort for the source file not exists
543543
if([fm fileExistsAtPath:path] == NO)
544544
{
545-
reject(@"RNFetchBlob slice failed", @"the file does not exists", path);
545+
reject(@"RNFetchBlob slice failed : the file does not exists", path, nil);
546546
return;
547547
}
548548
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+
555551
if(![fm fileExistsAtPath:dest]) {
556552
[fm createFileAtPath:dest contents:@"" attributes:nil];
557553
}
558554
[handle seekToFileOffset:[start longValue]];
559555
while(read < expected)
560556
{
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)
565561
{
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];
567565
}
568566
else
569567
{
570-
[os write:[chunk bytes] maxLength:10240];
568+
NSLog(@"read chunk %lu", 10240);
569+
chunkSize = 10240;
570+
chunk = [handle readDataOfLength:10240];
571571
}
572+
if([chunk length] <= 0)
573+
break;
574+
long remain = expected - read;
575+
576+
[os write:[chunk bytes] maxLength:chunkSize];
572577
read += [chunk length];
573578
}
574579
[handle closeFile];

0 commit comments

Comments
 (0)