|
@@ -12,5 +12,82 @@
|
|
|
|
|
|
@implementation customUploadOperation
|
|
|
|
|
|
+#pragma mark 分段读视频文件
|
|
|
+-(NSData*)cutVideoFileFunAtIndex:(NSUInteger)dataIndex withMaxLenght:(NSInteger)maxLengt{
|
|
|
+ HLog(@"视频切片开始 线程:%@",[NSThread currentThread]);
|
|
|
+ uploadFileDataModel *dataModel = _fileModel;
|
|
|
+ NSString *filePath = _filePath;
|
|
|
+ if(!filePath || filePath.length == 0){
|
|
|
+ filePath = [cachesFileManager getFilePathWithName:dataModel.filename type:uploadFileTypeVideo]; // 文件路径
|
|
|
+ }
|
|
|
|
|
|
+ NSFileManager *manager0 = [NSFileManager defaultManager];;
|
|
|
+
|
|
|
+ if(![manager0 fileExistsAtPath:filePath]) {
|
|
|
+ return [NSData new];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSFileHandle *fileHandle = _fileHandle;
|
|
|
+ if(!fileHandle){
|
|
|
+ fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; // 创建文件句柄
|
|
|
+ _fileHandle = fileHandle;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 设置分段读取的大小,这里以每次读取1KB为例
|
|
|
+ const NSUInteger chunkSize = maxLengt;//cutVideoPieceSzie;//5 * 1024 *1024;
|
|
|
+ NSMutableData *data = [NSMutableData data];
|
|
|
+
|
|
|
+ if (fileHandle) {
|
|
|
+
|
|
|
+ long long endOfFile = [fileHandle seekToEndOfFile];
|
|
|
+
|
|
|
+ if(dataModel.totalBytes == 0
|
|
|
+ || dataModel.totalBytes < endOfFile){//异常处理
|
|
|
+ dataModel.totalBytes = endOfFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ //异常处理
|
|
|
+ if(endOfFile == dataIndex){
|
|
|
+ dataModel.totalBytes = endOfFile;
|
|
|
+ dataModel.didUploadBytes = endOfFile;
|
|
|
+ dataModel.curUploadStateType = uploadStateDone;
|
|
|
+ [fileHandle closeFile];
|
|
|
+ _fileHandle = nil;
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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];
|
|
|
+ _fileHandle = nil;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ HLog(@"视频切片完成 dataIndex:%ld --长度:%ld",dataIndex,[data length])
|
|
|
+ return data;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)closeFileHandleFun
|
|
|
+{
|
|
|
+ if(_fileHandle){
|
|
|
+ [_fileHandle closeFile];
|
|
|
+ _fileHandle = nil;
|
|
|
+ }
|
|
|
+}
|
|
|
@end
|