|
|
@@ -277,10 +277,17 @@ didReceiveResponse:(NSURLResponse *)response
|
|
|
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
|
|
|
|
|
|
// 将响应交给列队处理
|
|
|
- [self handleOperateby:dataTask WithResponse:response];
|
|
|
+ BOOL canDownload = [self handleOperateby:dataTask WithResponse:response];
|
|
|
+
|
|
|
+ if(canDownload){
|
|
|
+ // 允许下载
|
|
|
+ completionHandler(NSURLSessionResponseAllow);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ // 不允许下载
|
|
|
+ completionHandler(NSURLSessionResponseCancel);
|
|
|
+ }
|
|
|
|
|
|
- // 允许下载
|
|
|
- completionHandler(NSURLSessionResponseAllow);
|
|
|
}
|
|
|
|
|
|
// 接受到数据碎片 的时候调用,调用多次
|
|
|
@@ -315,7 +322,7 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
#pragma mark 处理接收到的数据
|
|
|
|
|
|
// 接收到相应时
|
|
|
-- (void)handleOperateby:(NSURLSessionTask*)dataTask WithResponse:(NSURLResponse *)response {
|
|
|
+- (BOOL)handleOperateby:(NSURLSessionTask*)dataTask WithResponse:(NSURLResponse *)response {
|
|
|
|
|
|
customDownloadOperation *operation = nil;
|
|
|
for (customDownloadOperation *operationDoing in self.downloadingOperationArr) {
|
|
|
@@ -327,14 +334,29 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
|
|
|
if (!operation) {
|
|
|
HLog(@"没找到当前下载任务");
|
|
|
+ operation.downloadState = customDownloadStateFailed;
|
|
|
[dataTask cancel];
|
|
|
- return;
|
|
|
+ return NO;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查response是否是NSHTTPURLResponse的实例
|
|
|
+ if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
|
|
+ NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
|
|
|
+ NSInteger statusCode = httpResponse.statusCode;
|
|
|
+ HLog(@"HTTP Status Code: %ld", (long)statusCode);
|
|
|
+
|
|
|
+ if(statusCode == 404){
|
|
|
+ operation.downloadState = customDownloadStateFailed;
|
|
|
+ return NO;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
// 总的size
|
|
|
if (operation.currentSize + response.expectedContentLength == 0) {
|
|
|
HLog(@"下载数据回调异常");
|
|
|
- return;
|
|
|
+ operation.downloadState = customDownloadStateFailed;
|
|
|
+ return NO;
|
|
|
}
|
|
|
operation.totalSize = operation.currentSize + response.expectedContentLength;
|
|
|
|
|
|
@@ -353,8 +375,7 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
// 开始下载记录文件下载信息
|
|
|
operation.downloadState = customDownloadStateDoing;
|
|
|
|
|
|
- // 回调给外界
|
|
|
-
|
|
|
+ return YES;
|
|
|
}
|
|
|
|
|
|
- (void)dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
|
|
|
@@ -413,10 +434,12 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
operation.dataTask = nil;
|
|
|
operation.session = nil;
|
|
|
|
|
|
- // 关闭文件句柄
|
|
|
- [operation.handle closeFile];
|
|
|
- // 释放文件句柄
|
|
|
- operation.handle = nil;
|
|
|
+ if(operation.handle){
|
|
|
+ // 关闭文件句柄
|
|
|
+ [operation.handle closeFile];
|
|
|
+ // 释放文件句柄
|
|
|
+ operation.handle = nil;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if(operation.isManualCancel){
|
|
|
@@ -425,7 +448,7 @@ totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
}
|
|
|
|
|
|
// 完成下载 通知 block
|
|
|
- if (error) {
|
|
|
+ if (error ||operation.downloadState == customDownloadStateFailed) {
|
|
|
[self operationFailedWithOperation:operation];
|
|
|
} else {
|
|
|
operation.isFinished = YES;
|