make sure response with error status code won't change downloaded file on iOS

This commit is contained in:
James Zhan 2017-11-20 15:08:15 +11:00
parent 2dfcac5dd3
commit 14a2c8db06
1 changed files with 7 additions and 3 deletions

View File

@ -29,7 +29,9 @@
NSURL* url = [NSURL URLWithString:_params.fromUrl]; NSURL* url = [NSURL URLWithString:_params.fromUrl];
[[NSFileManager defaultManager] createFileAtPath:_params.toFile contents:nil attributes:nil]; if (![[NSFileManager defaultManager] fileExistsAtPath:_params.toFile]) {
[[NSFileManager defaultManager] createFileAtPath:_params.toFile contents:nil attributes:nil];
}
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:_params.toFile]; _fileHandle = [NSFileHandle fileHandleForWritingAtPath:_params.toFile];
if (!_fileHandle) { if (!_fileHandle) {
@ -97,8 +99,10 @@
NSURL *destURL = [NSURL fileURLWithPath:_params.toFile]; NSURL *destURL = [NSURL fileURLWithPath:_params.toFile];
NSFileManager *fm = [NSFileManager defaultManager]; NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil; NSError *error = nil;
[fm removeItemAtURL:destURL error:nil]; // Remove file at destination path, if it exists if([_statusCode integerValue] >= 200 && [_statusCode integerValue] < 300) {
[fm moveItemAtURL:location toURL:destURL error:&error]; [fm removeItemAtURL:destURL error:nil]; // Remove file at destination path, if it exists
[fm moveItemAtURL:location toURL:destURL error:&error];
}
if (error) { if (error) {
NSLog(@"RNFS download: unable to move tempfile to destination. %@, %@", error, error.userInfo); NSLog(@"RNFS download: unable to move tempfile to destination. %@, %@", error, error.userInfo);
} }