From e24d494eff2e4bcfc7f31b5be1c7d9ac18fde722 Mon Sep 17 00:00:00 2001 From: Will Han Date: Sat, 28 Mar 2020 01:12:14 +0800 Subject: [PATCH] Post a notification when finishing moving the downloaded file successfully. (#4393) * Post a notification when finishing moving the downloaded file successfully. * Add test cases for move notifications. --- AFNetworking/AFURLSessionManager.h | 5 ++++ AFNetworking/AFURLSessionManager.m | 5 ++++ Tests/Tests/AFURLSessionManagerTests.m | 37 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/AFNetworking/AFURLSessionManager.h b/AFNetworking/AFURLSessionManager.h index 355ee2f97d..bb22b31a88 100644 --- a/AFNetworking/AFURLSessionManager.h +++ b/AFNetworking/AFURLSessionManager.h @@ -509,6 +509,11 @@ FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidSuspendNotification; */ FOUNDATION_EXPORT NSString * const AFURLSessionDidInvalidateNotification; +/** + Posted when a session download task finished moving the temporary download file to a specified destination successfully. + */ +FOUNDATION_EXPORT NSString * const AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification; + /** Posted when a session download task encountered an error when moving the temporary download file to a specified destination. */ diff --git a/AFNetworking/AFURLSessionManager.m b/AFNetworking/AFURLSessionManager.m index af43f06561..68924075ab 100644 --- a/AFNetworking/AFURLSessionManager.m +++ b/AFNetworking/AFURLSessionManager.m @@ -46,6 +46,7 @@ static dispatch_group_t url_session_manager_completion_group() { NSString * const AFNetworkingTaskDidCompleteNotification = @"com.alamofire.networking.task.complete"; NSString * const AFNetworkingTaskDidSuspendNotification = @"com.alamofire.networking.task.suspend"; NSString * const AFURLSessionDidInvalidateNotification = @"com.alamofire.networking.session.invalidate"; +NSString * const AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification = @"com.alamofire.networking.session.download.file-manager-succeed"; NSString * const AFURLSessionDownloadTaskDidFailToMoveFileNotification = @"com.alamofire.networking.session.download.file-manager-error"; NSString * const AFNetworkingTaskDidCompleteSerializedResponseKey = @"com.alamofire.networking.task.complete.serializedresponse"; @@ -324,6 +325,8 @@ - (void)URLSession:(NSURLSession *)session if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:self.downloadFileURL error:&fileManagerError]) { [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:fileManagerError.userInfo]; + } else { + [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification object:downloadTask userInfo:nil]; } } } @@ -1210,6 +1213,8 @@ - (void)URLSession:(NSURLSession *)session if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error]) { [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:error.userInfo]; + } else { + [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification object:downloadTask userInfo:nil]; } return; diff --git a/Tests/Tests/AFURLSessionManagerTests.m b/Tests/Tests/AFURLSessionManagerTests.m index 95d543c85f..fc4d3bfc7c 100644 --- a/Tests/Tests/AFURLSessionManagerTests.m +++ b/Tests/Tests/AFURLSessionManagerTests.m @@ -442,6 +442,43 @@ - (void)testBackgroundManagerReturnsExpectedClassForDownloadTask { } } +#pragma mark - Notifications + +- (void)testTaskMoveSuccessfullyAfterDownloading { + NSURL *dirURL = [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject]; + NSURL *destinationURL = [dirURL URLByAppendingPathComponent:NSUUID.UUID.UUIDString]; + + NSURLSessionDownloadTask *task = [self.localManager downloadTaskWithRequest:[self bigImageURLRequest] + progress:nil + destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { + return destinationURL; + } + completionHandler:nil]; + + [self expectationForNotification:AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification + object:nil + handler:nil]; + [task resume]; + [self waitForExpectationsWithCommonTimeout]; + [[NSFileManager defaultManager] removeItemAtURL:destinationURL error:nil]; +} + +- (void)testTaskMoveFailedAfterDownloading { + NSURLSessionDownloadTask *downloadTask = [self.localManager downloadTaskWithRequest:[self bigImageURLRequest] + progress:nil + destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { + // Try to move the destination file to a nonexist path on purpose for simulating a move failure. + return [NSURL fileURLWithPath:@"/a/b/c"]; + } + completionHandler:nil]; + + [self expectationForNotification:AFURLSessionDownloadTaskDidFailToMoveFileNotification + object:nil + handler:nil]; + [downloadTask resume]; + [self waitForExpectationsWithCommonTimeout]; +} + #pragma mark - private - (void)_testResumeNotificationForTask:(NSURLSessionTask *)task {