From f72c7583f20c20278bb849509094269b7bfba6ac Mon Sep 17 00:00:00 2001 From: daily1092892 Date: Tue, 3 May 2022 04:05:43 +0800 Subject: [PATCH 1/2] fix: on iOS, check http status when downloaded finished --- ios/Classes/FlutterDownloaderPlugin.m | 83 +++++++++++++++------------ 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/ios/Classes/FlutterDownloaderPlugin.m b/ios/Classes/FlutterDownloaderPlugin.m index 5c9df825..455a2f7e 100644 --- a/ios/Classes/FlutterDownloaderPlugin.m +++ b/ios/Classes/FlutterDownloaderPlugin.m @@ -876,54 +876,65 @@ - (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]); + + NSLog(@"%s HTTP status code: %ld", __FUNCTION__, httpStatusCode); + + 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) { From efec4de72b29c96de53c3a27e8cd28b2cc16fcf5 Mon Sep 17 00:00:00 2001 From: daily1092892 Date: Wed, 4 May 2022 18:58:56 +0800 Subject: [PATCH 2/2] delete redundant NSLog --- ios/Classes/FlutterDownloaderPlugin.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/ios/Classes/FlutterDownloaderPlugin.m b/ios/Classes/FlutterDownloaderPlugin.m index 455a2f7e..9f0d71b1 100644 --- a/ios/Classes/FlutterDownloaderPlugin.m +++ b/ios/Classes/FlutterDownloaderPlugin.m @@ -884,8 +884,6 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas NSLog(@"%s HTTP status code: %ld", __FUNCTION__, httpStatusCode); } - NSLog(@"%s HTTP status code: %ld", __FUNCTION__, httpStatusCode); - bool isSuccess = (httpStatusCode >= 200 && httpStatusCode < 300); if (isSuccess) {