huangxiaodong преди 1 година
родител
ревизия
5021ede569

+ 54 - 6
创维盒子/双子星云手机/Class/Set/uploadFile/uploadFileManager/downloadManager.m

@@ -6,6 +6,7 @@
 //
 
 #import "downloadManager.h"
+#import "cachesFileManager.h"
 
 @implementation downloadManager
 static downloadManager * cur_downloadManager = nil;
@@ -83,27 +84,69 @@ static downloadManager * cur_downloadManager = nil;
 
 }
 
-- (void)handleCouldPhoneFileModelToDownloadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
+-(void)handleCouldPhoneFileModelToDownloadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
 {
     if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
         complete(nil);
         return;
     }
     
- 
     for (couldPhoneFileModel*model in indexPathsForSelectedItems) {
         model.bg_tableName = download_tableName;
     }
     
     KWeakSelf
-    [couldPhoneFileModel bg_saveOrUpdateArrayAsync:indexPathsForSelectedItems complete:^(BOOL isSuccess) {
+    //查
+    [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
+        //排重
+        NSMutableArray *newDownArr = [weakSelf  checkDownloadTaskRepeatFun:indexPathsForSelectedItems];
         
-        [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
+        if(newDownArr.count>0){
+            //存
+            [couldPhoneFileModel bg_saveOrUpdateArrayAsync:newDownArr complete:^(BOOL isSuccess) {
+                //再查
+                [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
+                    complete(Arr);
+                    [weakSelf planToDownloadFileFun];
+                }];
+            }];
+        }
+        else{
             complete(Arr);
             [weakSelf planToDownloadFileFun];
-        }];
+        }
+        
     }];
+}
+
+#pragma mark 检测是否有下载任务重复
+- (NSMutableArray*)checkDownloadTaskRepeatFun:(NSMutableArray*)indexPathsForSelectedItems
+{
+    if(!_databaseArr || _databaseArr.count != 3){
+        return indexPathsForSelectedItems;
+    }
     
+    NSMutableArray *downloadArr = _databaseArr[0];
+    NSMutableArray *newTaskArr = [NSMutableArray new];
+    
+    for (couldPhoneFileModel*model in indexPathsForSelectedItems) {
+       NSString *fileName = [model getFileNameFun];
+        
+        BOOL isNeedAddType = YES;
+        for (couldPhoneFileModel* hadModel in downloadArr) {
+            NSString *hadFileName = [hadModel getFileNameFun];
+            if([fileName isEqualToString:hadFileName]){
+                isNeedAddType = NO;
+                break;
+            }
+        }
+        
+        if(isNeedAddType){
+            [newTaskArr addObject:model];
+        }
+    }
+    
+    return newTaskArr;
 }
 
 - (void)planToDownloadFileFun
@@ -134,9 +177,14 @@ static downloadManager * cur_downloadManager = nil;
 - (void)DownloadFileDoneOneFileFun
 {
     _curDownloadFileModel.curDownloadStateType = downloadStateDone;
+//    NSString *fileName = [_curDownloadFileModel getFileNameFun];
+//    [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
     
-    [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
+    [_curDownloadFileModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
+            
+    }];
     
+    [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
     [self beginDownloadFileFun];
 }
 @end

+ 46 - 10
创维盒子/双子星云手机/CloudPlayer/PlayerViewController+downloadFile.m

@@ -28,10 +28,22 @@
     
     self.downloadFileTaskUid = [iTools getTaskUidStr];
  
-    NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:self.curDownloadFileModel.didDownloadBytes];
-
-    [self.commandChannelManager rc_sendData:commandStr];
+    NSString *fileName = [self.curDownloadFileModel getFileNameFun];
+    NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
+    long didDownloadBytes = 0;
+    
+    NSFileManager *manager0 = [NSFileManager defaultManager];
+    if([manager0 fileExistsAtPath:pathStr]) {
+        NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:pathStr]; // 创建文件句柄
+        if (fileHandle) {
+            didDownloadBytes = [fileHandle seekToEndOfFile];
+            // 关闭文件句柄
+            [fileHandle closeFile];
+        }
+    }
     
+    NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:didDownloadBytes];
+    [self.commandChannelManager rc_sendData:commandStr];
 }
 
 - (void)downloadFileFunServiceResponseFun:(NSData *)fileData  with:(BOOL)isDownloadDone
@@ -54,8 +66,6 @@
                 [fileHandle writeData:fileData];
             }
             
-            //[fileHandle writeData:fileData];
-            
             endOfFile = [fileHandle seekToEndOfFile];
             // 关闭文件句柄
             [fileHandle closeFile];
@@ -69,10 +79,22 @@
     
     self.curDownloadFileModel.didDownloadBytes = didDownloadBytes;
    
+    if(isDownloadDone){
+        NSInteger i;
+    }
+    
+    if(endOfFile == self.curDownloadFileModel.length){
+        NSInteger i;
+    }
+    
     if(isDownloadDone || endOfFile == self.curDownloadFileModel.length){
         [self handleDownloadDoneFun];
     }
     else{//获取下一片
+        if(isDownloadDone){
+            NSInteger i;
+        }
+        
         NSString * commandStr = [RCCommandHelp applyForDownloadFileBy:self.downloadFileTaskUid withFilePath:self.curDownloadFileModel.path withPosition:didDownloadBytes];
         [self.commandChannelManager rc_sendData:commandStr];
     }
@@ -81,7 +103,7 @@
 
 - (void)handleDownloadDoneFun
 {
-    if([self.curDownloadFileModel.fileType isEqualToString:@"jpg"]){
+    if([self.curDownloadFileModel.fileType isEqualToString:@".jpg"]){
         NSString *fileName = [self.curDownloadFileModel getFileNameFun];
         NSString*pathStr = [cachesFileManager getFilePathWithName:fileName type:DownLoadFileType];
         UIImage *image = [UIImage imageWithContentsOfFile:pathStr];
@@ -94,7 +116,6 @@
         [self loadVideoFinished];
     }
     
-    [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
 }
 
 
@@ -105,7 +126,17 @@
          PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
             
      } completionHandler:^(BOOL success, NSError * _Nullable error) {
-         NSLog(@"success = %d, error = %@", success, error);
+         //NSLog(@"success = %d, error = %@", success, error);
+         if (success) {
+             HLog(@"已将视频保存至相册");
+             NSString *fileName = [self.curDownloadFileModel getFileNameFun];
+             [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
+             
+         } else {
+             HLog(@"未能将视频保存至相册");
+         }
+         
+         [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
     }];
 }
 
@@ -120,10 +151,15 @@
     fileURLWithPath:pathStr]];
     } completionHandler:^(BOOL success, NSError * _Nullable error) {
         if (success) {
-            NSLog(@"已将视频保存至相册");
+            HLog(@"已将图片保存至相册");
+            //NSString *fileName = [self.curDownloadFileModel getFileNameFun];
+            [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
+            
         } else {
-            NSLog(@"未能保存视频到相册");
+            HLog(@"未能将图片保存至相册");
         }
+        
+        [[downloadManager shareInstance] DownloadFileDoneOneFileFun];
     }];
 }
 @end

+ 3 - 5
创维盒子/双子星云手机/CloudPlayer/PlayerViewController+otherDelegate.mm

@@ -303,7 +303,9 @@
     int totalNum = [self data2Int:totalNumData];
     //HLog(@"totalNum: %d",totalNum);
     
-    HLog(@"taskId:%@ curNum:%d totalNum: %d",taskIdStr,curNum,totalNum);
+    BOOL isDownloadDone = curNum == totalNum ? YES : NO;
+    
+    HLog(@"taskUid:%@ curNum:%d totalNum: %d datalen:%d isDownloadDone:%d",taskIdStr,curNum,totalNum,datalen -8,isDownloadDone);
     
     if(curData.length <= (20 + datalen - 8)){
         HLog(@"下载数据解析出错了");
@@ -312,11 +314,7 @@
     
     NSData *fileData = [curData subdataWithRange:NSMakeRange(20, datalen-8)];
 
-
     if([taskIdStr isEqualToString:self.downloadFileTaskUid]){
-        
-        BOOL isDownloadDone = curNum == totalNum ? YES : NO;
-        
         [self downloadFileFunServiceResponseFun:fileData with:isDownloadDone];
     }