浏览代码

1.备份文件--视频等

huangxiaodong 1 年之前
父节点
当前提交
4ab8fe5e41

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

@@ -235,7 +235,8 @@
     
     _curPhotosBackupsTaskModel = curPhotosBackupsTaskModel;
     
-    _curPhotosBackupsTaskModel.curBackupsState = backupsStateUploading;
+    //test code
+    //_curPhotosBackupsTaskModel.curBackupsState = backupsStateUploading;
     
     _fileImageView.image = [UIImage imageNamed:@"uploadFile_image"];
     

+ 47 - 4
创维盒子/双子星云手机/Class/Set/uploadFile/uploadFileManager/backupsFileManager.m

@@ -6,6 +6,7 @@
 //
 
 #import "backupsFileManager.h"
+#import "cachesFileManager.h"
 
 @implementation backupsFileManager
 static backupsFileManager * cur_backupsFileShareInstance = nil;
@@ -152,15 +153,15 @@ static backupsFileManager * cur_backupsFileShareInstance = nil;
     }
         
     PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
-    PHAsset *asset = fetchResult.firstObject;
+    PHAsset *curAsset = fetchResult.firstObject;
     
-    _curPhotosBackupsTaskMod.filename = [asset valueForKey:@"filename"];
+    _curPhotosBackupsTaskMod.filename = [curAsset valueForKey:@"filename"];
     
     
     KWeakSelf
-    if(asset.mediaType == PHAssetMediaTypeImage){
+    if(curAsset.mediaType == PHAssetMediaTypeImage){
         _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeImage;
-        [[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
+        [[PHImageManager defaultManager] requestImageDataForAsset:curAsset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
                 // 直接得到最终的 NSData 数据
                 if (imageData) {
                     [weakSelf  afterGetImageDataFun:imageData];
@@ -172,12 +173,40 @@ static backupsFileManager * cur_backupsFileShareInstance = nil;
     }
     else{
         _curPhotosBackupsTaskMod.curUploadFileType = uploadFileTypeVideo;
+        
+        //判断文件是否在app内
+       BOOL didSaveFile = [cachesFileManager  checkFileIsSaveState:_curPhotosBackupsTaskMod.filename withType:uploadFileTypeVideo];
+        
+        if(didSaveFile){
+            [self afterGetVideoDataFun];
+            return;
+        }
+        
+        //真正的视频数据
+        PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
+        options.version = PHVideoRequestOptionsVersionOriginal;
+        [[PHImageManager defaultManager] requestAVAssetForVideo:curAsset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
+            if ([asset isKindOfClass:[AVURLAsset class]]) {
+                
+                AVURLAsset* urlAsset = (AVURLAsset*)asset;
+                
+                BOOL isSuc = [cachesFileManager copyVideoItemAtPath:[urlAsset.URL path] fileName:self->_curPhotosBackupsTaskMod.filename error:nil];
+        
+                if (isSuc) {
+                    [weakSelf  afterGetVideoDataFun];
+                }
+                else{
+                    [weakSelf getDataWrongToChangeFailFun];
+                }
+         }
+        }];
     }
     
 }
 
 - (void)getDataWrongToChangeFailFun
 {
+    [self deleteVideoFun];
     _curPhotosBackupsTaskMod.failCount += 1;
     
     [self RefreshDatabaseFun];
@@ -200,9 +229,23 @@ static backupsFileManager * cur_backupsFileShareInstance = nil;
     [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
 }
 
+- (void)deleteVideoFun
+{
+    if(_curPhotosBackupsTaskMod.curUploadFileType == uploadFileTypeVideo){
+        [cachesFileManager removeItemAtPath:_curPhotosBackupsTaskMod.filename type:uploadFileTypeVideo error:nil];
+    }
+}
+
+- (void)afterGetVideoDataFun
+{
+    [[NSNotificationCenter defaultCenter] postNotificationName:backupsFileBeginNotification object:_curPhotosBackupsTaskMod];
+}
+
 - (void)backupsFileDoneFun
 {
+    [self deleteVideoFun];
     _curPhotosBackupsTaskMod.didBackupsCount +=1;
+    [self RefreshDatabaseFun];
     [self beginBackupsFileFun];
     
 //    long totalSizeByte = _curUploadFileDataModel.totalBytes;

+ 99 - 2
创维盒子/双子星云手机/CloudPlayer/PlayerViewController+backupsFile.m

@@ -93,7 +93,7 @@
                                 [self beginGotoBackupsImageFun:fileLengthNum];
                             }
                             else{
-                                //[self beginGotoUploadVidelFun:fileLengthNum];
+                                [self beginGotoUploadVidelFun:fileLengthNum];
                             }
                         }
                     }
@@ -133,6 +133,91 @@
     [self backupsFileFun];
 }
 
+#pragma mark 视频上传
+//每次从文件重新切一片 传一片
+-(void)beginGotoUploadVidelFun:(NSNumber*)fileLengthNum{
+    
+    NSData *data = [self backupsCutVideoFileFunAtIndex:fileLengthNum.longLongValue];
+    HLog(@"data leng:%ld",[data length]);
+
+    self.backupsAllFileData  = data;
+    self.backupsCutFileDataArr  = [self backupsFileCutPartsBy:self.backupsAllFileData withDidUpLoadSize:0];
+    self.backupsIndexOfUploadFlie  = 0;
+    HLog(@"data leng:%ld",[data length]);
+    
+    //return;
+
+//    NSString*videoPathStr = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo];
+//    self.AllFileData = [NSData dataWithContentsOfFile:videoPathStr];
+
+   // self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
+    
+    if(!self.backupsCutFileDataArr){
+        [self clearBackupsFileDataFun];
+        [[backupsFileManager shareInstance] getDataWrongToChangeFailFun];
+        
+        HLog(@"hxd fail 备份任务异常终止")
+        return;
+    }
+    
+    //答复的taskUid 跟申请的一致 开发发送内容
+    [self backupsFileFun];
+}
+
+#pragma mark 分段读视频文件
+-(NSData*)backupsCutVideoFileFunAtIndex:(NSUInteger)dataIndex{
+    NSString *filePath = [cachesFileManager getFilePathWithName:self.curPhotosBackupsTaskMod.filename type:uploadFileTypeVideo]; // 文件路径
+    
+    NSFileManager *manager0 = [NSFileManager defaultManager];
+    if(![manager0 fileExistsAtPath:filePath]) {
+     
+        return [NSData new];
+    }
+    
+    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; // 创建文件句柄
+      
+    // 设置分段读取的大小,这里以每次读取1KB为例
+    //const NSUInteger chunkSize = 50 * 1024 *1024;
+    const NSUInteger chunkSize = 5 * 1024 *1024;
+    NSMutableData *data = [NSMutableData data];
+    
+    if (fileHandle) {
+       
+        long long endOfFile = [fileHandle seekToEndOfFile];
+        
+        if(dataIndex == 0 || self.curPhotosBackupsTaskMod.totalBytes == 0){
+            self.curPhotosBackupsTaskMod.totalBytes = endOfFile;
+        }
+        
+        if (endOfFile >=  chunkSize) {
+            
+            // 读取文件的分段数据到某个位置
+            [fileHandle seekToFileOffset:dataIndex];
+            
+            // 读取文件的分段数据
+            NSData* chunk = [fileHandle readDataOfLength:chunkSize];
+            if (chunk) {
+                [data appendData:chunk];
+            }
+        }
+        else{
+            // 读取文件的分段数据到某个位置
+            [fileHandle seekToFileOffset:dataIndex];
+            
+            [data appendData:[fileHandle readDataToEndOfFile]];
+        }
+        
+        // 在这里可以对文件内容进行处理
+        // ...
+        
+        // 关闭文件句柄
+        [fileHandle closeFile];
+    }
+    
+    return data;
+}
+
+
 - (void)backupsFileFunServiceResponseFun:(NSDictionary *)dataDict
 {
     //"type":"backUpFileRandomRet","data":{"progress":"0\/1","filePath":"/sdcard/DCIM/IMG_0889.JPG","fileLenth":258066,"status":"0","msg":"success"}}
@@ -202,7 +287,7 @@
     else if(self.curPhotosBackupsTaskMod.curUploadFileType == uploadFileTypeVideo && fileLengthNum && fileLengthNum.longLongValue < self.curPhotosBackupsTaskMod.totalBytes){
         //视频做了二次切割 不一定上传完了
         HLog(@"视频上传传完一片 再来一片");
-        //[self beginGotoUploadVidelFun:fileLengthNum];
+        [self beginGotoUploadVidelFun:fileLengthNum];
     }
     else{
         HLog(@"备份完成");
@@ -211,6 +296,18 @@
     }
 }
 
+-(void)clearBackupsFileDataFun{
+    //上传数据清空
+    self.backupsTaskUid = nil;
+    self.backupsFileName = nil;
+    self.backupsAllFileData = nil;
+    self.backupsIndexOfUploadFlie = 0;
+    self.backupsCutFileDataArr = nil;
+    self.curPhotosBackupsTaskMod = nil;
+    self.isSuspendBackupsType = NO;
+    
+}
+
 - (Byte)getBCC:(NSData *)data {
     Byte bcc = 0;
     unsigned char *bytes = (unsigned char *)[data bytes];