Skip to content

Commit

Permalink
Fix HTTP request errors returned as connectivity errors (#19)
Browse files Browse the repository at this point in the history
* Fix http request errors returned as connectivity errors

* Protect from error after download
  • Loading branch information
jrouault authored Nov 24, 2016
1 parent eed47b4 commit 59681a9
Showing 1 changed file with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand *)command {
NSString *uri = [command.arguments objectAtIndex:1];
NSString *filePath = [self buildAssetFilePathFromUri:uri];

NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath] == YES) {
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] == YES) {
// download complete pass back confirmation to JS
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:filePath];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
Expand All @@ -73,29 +72,12 @@ - (void)downloadFile:(CDVInvokedUrlCommand *)command {
CDVPluginResult *result = nil;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = httpResponse.statusCode;
if (error) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:CONNECTIVITY_ERROR message:[error description]]];
} else if (statusCode < 200 || statusCode >= 300) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:HTTP_REQUEST_ERROR status:statusCode]];
if (!error && statusCode >= 200 && statusCode < 300) {
result = [self moveFileAtURL:location toFilePath:filePath];
} else if (error) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:HTTP_REQUEST_ERROR status:statusCode message:[error description]]];
} else {
NSError *ioError = nil;
NSString *fullDir = [filePath stringByDeletingLastPathComponent];
BOOL isDirectoryCreated = [fileManager createDirectoryAtPath:fullDir withIntermediateDirectories:YES attributes:nil error:&ioError];
if (ioError) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:DIRECTORY_CREATION_ERROR message:[ioError description]]];
} else if (!isDirectoryCreated) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:DIRECTORY_CREATION_ERROR]];
} else {
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
BOOL isFileMoved = [fileManager moveItemAtURL:location toURL:filePathURL error:&ioError];
if (ioError) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:FILE_CREATION_ERROR message:[ioError description]]];
} else if (!isFileMoved) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:FILE_CREATION_ERROR]];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:filePath];
}
}
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:HTTP_REQUEST_ERROR status:statusCode]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
Expand All @@ -105,6 +87,30 @@ - (void)downloadFile:(CDVInvokedUrlCommand *)command {
[downloadTask resume];
}

- (CDVPluginResult *)moveFileAtURL:(NSURL *)location toFilePath:(NSString *)filePath {
CDVPluginResult *result = nil;
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fullDir = [filePath stringByDeletingLastPathComponent];
BOOL isDirectoryCreated = [fileManager createDirectoryAtPath:fullDir withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:DIRECTORY_CREATION_ERROR message:[error description]]];
} else if (!isDirectoryCreated) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:DIRECTORY_CREATION_ERROR]];
} else {
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
BOOL isFileMoved = [fileManager moveItemAtURL:location toURL:filePathURL error:&error];
if (error) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:FILE_CREATION_ERROR message:[error description]]];
} else if (!isFileMoved) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:[self createDownloadFileError:FILE_CREATION_ERROR]];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:filePath];
}
}
return result;
}

- (NSDictionary *)createDownloadFileError:(int)code {
return [self createDownloadFileError:code status:-1 message:nil];
}
Expand Down

0 comments on commit 59681a9

Please sign in to comment.