Browse Source

1.nas文件删除的下载失败逻辑处理

huangxiaodong 1 year ago
parent
commit
f9487d9a3a

+ 36 - 13
创维盒子/双子星云手机/Class/Set/uploadFile/customDownloadManager/customDownloadManager.m

@@ -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;

+ 2 - 1
创维盒子/双子星云手机/Class/Set/uploadFile/uploadFileManager/boxDownloadFileManager.m

@@ -30,7 +30,8 @@ static boxDownloadFileManager * cur_boxDownloadFileShareInstance = nil;
         //某一个YCDownloadItem下载成功通知
         //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskFinishedNoti:) name:mixDownloadTaskExeEnd object:nil];
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskFinishedNoti:) name:customDownloadTaskExeEnd object:nil];
-     
+        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadTaskFinishedNoti:) name:customDownloadTaskExeError object:nil];
+        
         [self initDownloadManagerFun];
     }