diff --git a/YTKNetwork.podspec b/YTKNetwork.podspec index 4655d4f..b233b76 100644 --- a/YTKNetwork.podspec +++ b/YTKNetwork.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "YTKNetwork" - s.version = "2.0.4" + s.version = "2.0.5" s.summary = "YTKNetwork is a high level request util based on AFNetworking." s.homepage = "https://github.com/yuantiku/YTKNetwork" s.license = "MIT" diff --git a/YTKNetwork/YTKNetworkAgent.m b/YTKNetwork/YTKNetworkAgent.m index 5703013..72ffff9 100644 --- a/YTKNetwork/YTKNetworkAgent.m +++ b/YTKNetwork/YTKNetworkAgent.m @@ -241,7 +241,7 @@ - (void)addRequest:(YTKBaseRequest *)request { - (void)cancelRequest:(YTKBaseRequest *)request { NSParameterAssert(request != nil); - if (request.resumableDownloadPath) { + if (request.resumableDownloadPath && [self incompleteDownloadTempPathForDownloadPath:request.resumableDownloadPath] != nil) { NSURLSessionDownloadTask *requestTask = (NSURLSessionDownloadTask *)request.requestTask; [requestTask cancelByProducingResumeData:^(NSData *resumeData) { NSURL *localUrl = [self incompleteDownloadTempPathForDownloadPath:request.resumableDownloadPath]; @@ -382,8 +382,9 @@ - (void)requestDidFailWithRequest:(YTKBaseRequest *)request error:(NSError *)err // Save incomplete download data. NSData *incompleteDownloadData = error.userInfo[NSURLSessionDownloadTaskResumeData]; - if (incompleteDownloadData) { - [incompleteDownloadData writeToURL:[self incompleteDownloadTempPathForDownloadPath:request.resumableDownloadPath] atomically:YES]; + NSURL *localUrl = [self incompleteDownloadTempPathForDownloadPath:request.resumableDownloadPath]; + if (incompleteDownloadData && localUrl != nil) { + [incompleteDownloadData writeToURL:localUrl atomically:YES]; } // Load response from file and clean up if download task failed. @@ -492,27 +493,30 @@ - (NSURLSessionDownloadTask *)downloadTaskWithDownloadPath:(NSString *)downloadP [[NSFileManager defaultManager] removeItemAtPath:downloadTargetPath error:nil]; } - BOOL resumeDataFileExists = [[NSFileManager defaultManager] fileExistsAtPath:[self incompleteDownloadTempPathForDownloadPath:downloadPath].path]; - NSData *data = [NSData dataWithContentsOfURL:[self incompleteDownloadTempPathForDownloadPath:downloadPath]]; - BOOL resumeDataIsValid = [YTKNetworkUtils validateResumeData:data]; - - BOOL canBeResumed = resumeDataFileExists && resumeDataIsValid; BOOL resumeSucceeded = NO; __block NSURLSessionDownloadTask *downloadTask = nil; - // Try to resume with resumeData. - // Even though we try to validate the resumeData, this may still fail and raise excecption. - if (canBeResumed) { - @try { - downloadTask = [_manager downloadTaskWithResumeData:data progress:downloadProgressBlock destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { - return [NSURL fileURLWithPath:downloadTargetPath isDirectory:NO]; - } completionHandler: - ^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { - [self handleRequestResult:downloadTask responseObject:filePath error:error]; - }]; - resumeSucceeded = YES; - } @catch (NSException *exception) { - YTKLog(@"Resume download failed, reason = %@", exception.reason); - resumeSucceeded = NO; + NSURL *localUrl = [self incompleteDownloadTempPathForDownloadPath:downloadPath]; + if (localUrl != nil) { + BOOL resumeDataFileExists = [[NSFileManager defaultManager] fileExistsAtPath:localUrl.path]; + NSData *data = [NSData dataWithContentsOfURL:localUrl]; + BOOL resumeDataIsValid = [YTKNetworkUtils validateResumeData:data]; + + BOOL canBeResumed = resumeDataFileExists && resumeDataIsValid; + // Try to resume with resumeData. + // Even though we try to validate the resumeData, this may still fail and raise excecption. + if (canBeResumed) { + @try { + downloadTask = [_manager downloadTaskWithResumeData:data progress:downloadProgressBlock destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { + return [NSURL fileURLWithPath:downloadTargetPath isDirectory:NO]; + } completionHandler: + ^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { + [self handleRequestResult:downloadTask responseObject:filePath error:error]; + }]; + resumeSucceeded = YES; + } @catch (NSException *exception) { + YTKLog(@"Resume download failed, reason = %@", exception.reason); + resumeSucceeded = NO; + } } } if (!resumeSucceeded) { @@ -549,7 +553,7 @@ - (NSURL *)incompleteDownloadTempPathForDownloadPath:(NSString *)downloadPath { NSString *tempPath = nil; NSString *md5URLString = [YTKNetworkUtils md5StringFromString:downloadPath]; tempPath = [[self incompleteDownloadTempCacheFolder] stringByAppendingPathComponent:md5URLString]; - return [NSURL fileURLWithPath:tempPath]; + return tempPath == nil ? nil : [NSURL fileURLWithPath:tempPath]; } #pragma mark - Testing