Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Recognise and handle HTTP response status codes on iOS as well #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions ios/RNBackgroundDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ + (void)setCompletionHandlerWithIdentifier: (NSString *)identifier completionHan
}
}

- (NSError *)getServerError: (nonnull NSURLSessionDownloadTask *)downloadTask {
NSError *serverError;
NSInteger httpStatusCode = [((NSHTTPURLResponse *)downloadTask.response) statusCode];
if(httpStatusCode != 200) {
serverError = [NSError errorWithDomain:NSURLErrorDomain
code:httpStatusCode
userInfo:@{NSLocalizedDescriptionKey: [NSHTTPURLResponse localizedStringForStatusCode: httpStatusCode]}];
}
return serverError;
}

- (BOOL)saveDownloadedFile: (nonnull RNBGDTaskConfig *) taskConfig downloadURL:(nonnull NSURL *)location error:(NSError **)saveError {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *destURL = [NSURL fileURLWithPath:taskConfig.destination];
[fileManager createDirectoryAtURL:[destURL URLByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager removeItemAtURL:destURL error:nil];

return [fileManager moveItemAtURL:location toURL:destURL error:saveError];
}

#pragma mark - JS exported methods
RCT_EXPORT_METHOD(download: (NSDictionary *) options) {
Expand Down Expand Up @@ -208,19 +227,17 @@ + (void)setCompletionHandlerWithIdentifier: (NSString *)identifier completionHan
#pragma mark - NSURLSessionDownloadDelegate methods
- (void)URLSession:(nonnull NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location {
@synchronized (sharedLock) {
RNBGDTaskConfig *taskCofig = taskToConfigMap[@(downloadTask.taskIdentifier)];
if (taskCofig != nil) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *destURL = [NSURL fileURLWithPath:taskCofig.destination];
[fileManager createDirectoryAtURL:[destURL URLByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager removeItemAtURL:destURL error:nil];
NSError *moveError;
BOOL moved = [fileManager moveItemAtURL:location toURL:destURL error:&moveError];
RNBGDTaskConfig *taskConfig = taskToConfigMap[@(downloadTask.taskIdentifier)];
if (taskConfig != nil) {
NSError *error = [self getServerError:downloadTask];
if (error == nil) {
[self saveDownloadedFile:taskConfig downloadURL:location error:&error];
}
if (self.bridge) {
if (moved) {
[self sendEventWithName:@"downloadComplete" body:@{@"id": taskCofig.id}];
if (error == nil) {
[self sendEventWithName:@"downloadComplete" body:@{@"id": taskConfig.id}];
} else {
[self sendEventWithName:@"downloadFailed" body:@{@"id": taskCofig.id, @"error": [moveError localizedDescription]}];
[self sendEventWithName:@"downloadFailed" body:@{@"id": taskConfig.id, @"error": [error localizedDescription]}];
}
}
[self removeTaskFromMap:downloadTask];
Expand Down