Skip to content

Commit

Permalink
FIX: create directory fail bug
Browse files Browse the repository at this point in the history
Change-Id: If6e0b27a604190e0be907de43581417adac7b75b
  • Loading branch information
baocq committed Feb 9, 2018
1 parent a2ad5de commit 0d14ab8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion YTKNetwork.podspec
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
50 changes: 27 additions & 23 deletions YTKNetwork/YTKNetworkAgent.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0d14ab8

Please sign in to comment.