Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: on iOS, check http status when downloaded finished #635

Merged
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
81 changes: 45 additions & 36 deletions ios/Classes/FlutterDownloaderPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -876,54 +876,63 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) downloadTask.response;
long httpStatusCode = (long)[httpResponse statusCode];

if (debug) {
NSLog(@"URLSession:downloadTask:didFinishDownloadingToURL:");
NSLog(@"%s HTTP status code: %ld", __FUNCTION__, httpStatusCode);
}

NSString *taskId = [self identifierForTask:downloadTask ofSession:session];
NSDictionary *task = [self loadTaskWithId:taskId];
NSURL *destinationURL = [self fileUrlOf:taskId taskInfo:task downloadTask:downloadTask];

[_runningTaskById removeObjectForKey:taskId];

NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:[destinationURL path]]) {
[fileManager removeItemAtURL:destinationURL error:nil];
}

BOOL success = [fileManager copyItemAtURL:location
toURL:destinationURL
error:&error];

__typeof__(self) __weak weakSelf = self;
if (success) {
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_COMPLETE) andProgress:@100];
dispatch_sync(databaseQueue, ^{
[weakSelf updateTask:taskId status:STATUS_COMPLETE progress:100];
});
} else {
if (debug) {
NSLog(@"Unable to copy temp file. Error: %@", [error localizedDescription]);

bool isSuccess = (httpStatusCode >= 200 && httpStatusCode < 300);

if (isSuccess) {
NSString *taskId = [self identifierForTask:downloadTask ofSession:session];
NSDictionary *task = [self loadTaskWithId:taskId];
NSURL *destinationURL = [self fileUrlOf:taskId taskInfo:task downloadTask:downloadTask];

[_runningTaskById removeObjectForKey:taskId];

NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:[destinationURL path]]) {
[fileManager removeItemAtURL:destinationURL error:nil];
}

BOOL success = [fileManager copyItemAtURL:location
toURL:destinationURL
error:&error];

__typeof__(self) __weak weakSelf = self;
if (success) {
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_COMPLETE) andProgress:@100];
dispatch_sync(databaseQueue, ^{
[weakSelf updateTask:taskId status:STATUS_COMPLETE progress:100];
});
} else {
if (debug) {
NSLog(@"Unable to copy temp file. Error: %@", [error localizedDescription]);
}
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_FAILED) andProgress:@(-1)];
dispatch_sync(databaseQueue, ^{
[weakSelf updateTask:taskId status:STATUS_FAILED progress:-1];
});
}
[self sendUpdateProgressForTaskId:taskId inStatus:@(STATUS_FAILED) andProgress:@(-1)];
dispatch_sync(databaseQueue, ^{
[weakSelf updateTask:taskId status:STATUS_FAILED progress:-1];
});
}

}

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (debug) {
NSLog(@"URLSession:task:didCompleteWithError:");
}

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) task.response;
long httpStatusCode = (long)[httpResponse statusCode];

if (debug) {
NSLog(@"HTTP status code: %ld", httpStatusCode);
NSLog(@"%s HTTP status code: %ld", __FUNCTION__, httpStatusCode);
}

bool isSuccess = (httpStatusCode >= 200 && httpStatusCode < 300);
if (error != nil || !isSuccess) {
if (debug) {
Expand Down