|
@@ -8,6 +8,7 @@
|
|
|
#import "nasUploadManager.h"
|
|
|
#import "AFNetworkReachabilityManager.h"
|
|
|
#import "AFHTTPSessionManager.h"
|
|
|
+#import "frpUploadModel.h"
|
|
|
|
|
|
@interface nasUploadManager ()
|
|
|
@property(nonatomic,strong)AFHTTPSessionManager *uploadManager;
|
|
@@ -54,8 +55,12 @@
|
|
|
|
|
|
NSString *mimeType =@"application/octet-stream";
|
|
|
if ([[params allKeys] containsObject:@"imageType"]) {
|
|
|
- mimeType = @"jpg";
|
|
|
+ mimeType = @"image/jpeg";
|
|
|
}
|
|
|
+ else if ([[params allKeys] containsObject:@"videoType"]) {
|
|
|
+ mimeType = @"video/mp4";
|
|
|
+ }
|
|
|
+
|
|
|
[formData appendPartWithFileData:data name:@"file" fileName:params[@"filename"] mimeType:mimeType];
|
|
|
|
|
|
} progress:^(NSProgress * _Nonnull uploadProgress) {
|
|
@@ -70,7 +75,7 @@
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
-- (void)beginUploadDataBy:(uploadFileDataModel*)dataModel
|
|
|
+- (void)beginUploadDataBy:(uploadFileDataModel*)dataModel success:(netWork_Success)success faild:(netWork_Faild)faildStr
|
|
|
{
|
|
|
NSMutableDictionary *paraDict = [NSMutableDictionary new];
|
|
|
NSString* taskUid = [iTools getTaskUidStr];
|
|
@@ -93,18 +98,157 @@
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- NSData *curData = dataModel.curUploadFileType == uploadFileTypeImage ? dataModel.imageData : dataModel.videoData;
|
|
|
-
|
|
|
+ KWeakSelf
|
|
|
if(dataModel.curUploadFileType == uploadFileTypeImage){
|
|
|
[paraDict setObject:@1 forKey:@"imageType"];
|
|
|
+ NSData *curData = dataModel.imageData;
|
|
|
+
|
|
|
+ [self nasUploadFileToFileServiceWithParams:paraDict data:curData success:^(id _Nonnull responseObject) {
|
|
|
+ HLog(@"%@上传完成",dataModel.filename)
|
|
|
+ success(responseObject);
|
|
|
+ } faild:^(NSError * _Nonnull error) {
|
|
|
+ HLog(@"%@上传失败",dataModel.filename)
|
|
|
+ faildStr(error);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ [paraDict setObject:@1 forKey:@"videoType"];
|
|
|
+
|
|
|
+ //断点续传处理 先传1字节数据看服务器是否有数据
|
|
|
+ NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:1 withModel:dataModel];
|
|
|
+ [paraDict setObject:@0 forKey:@"isLast"];
|
|
|
+
|
|
|
+ // NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
+
|
|
|
+
|
|
|
+ [self nasUploadFileToFileServiceWithParams:paraDict data:videoData success:^(id _Nonnull responseObject) {
|
|
|
+ HLog(@"%@上传完成",dataModel.filename)
|
|
|
+
|
|
|
+ frpUploadModel *model = [[frpUploadModel alloc] initWithDictionary:responseObject error:nil];
|
|
|
+ if(model && model.msg){
|
|
|
+ NSInteger curPosition = model.position > 0 ? model.position : 1;
|
|
|
+ [weakSelf beginUploadVideoDataFunBy:dataModel with:curPosition withPara:paraDict success:^(id _Nonnull responseObject) {
|
|
|
+ success(responseObject);
|
|
|
+ } faild:^(NSError * _Nonnull error) {
|
|
|
+ faildStr(error);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ NSError *err = [NSError new];
|
|
|
+ faildStr(err);
|
|
|
+ }
|
|
|
+
|
|
|
+ } faild:^(NSError * _Nonnull error) {
|
|
|
+ HLog(@"%@上传失败",dataModel.filename)
|
|
|
+ faildStr(error);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)beginUploadVideoDataFunBy:(uploadFileDataModel*)dataModel with:(NSInteger)position withPara:(NSMutableDictionary*)paraDict success:(netWork_Success)success faild:(netWork_Faild)faildStr
|
|
|
+{
|
|
|
+
|
|
|
+ BOOL isLastPicece = NO;
|
|
|
+ if((dataModel.totalBytes - position) <= MaxNasUploadPieceSzie){
|
|
|
+ [paraDict setObject:@1 forKey:@"isLast"];
|
|
|
+ isLastPicece = YES;
|
|
|
}
|
|
|
|
|
|
+ [paraDict setObject:[NSNumber numberWithLong:position] forKey:@"position"];
|
|
|
+
|
|
|
+ //视频数据切片
|
|
|
+ NSData *videoData = [self cutVideoFileFunAtIndex:position withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
|
|
|
- [self nasUploadFileToFileServiceWithParams:paraDict data:curData success:^(id _Nonnull responseObject) {
|
|
|
- HLog(@"上传完成")
|
|
|
+ KWeakSelf
|
|
|
+ [self nasUploadFileToFileServiceWithParams:paraDict data:videoData success:^(id _Nonnull responseObject) {
|
|
|
+ HLog(@"%@上传完成",dataModel.filename)
|
|
|
+
|
|
|
+ frpUploadModel *model = [[frpUploadModel alloc] initWithDictionary:responseObject error:nil];
|
|
|
+ if(model && model.msg){
|
|
|
+ if (isLastPicece) {
|
|
|
+ success(responseObject);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ [weakSelf beginUploadVideoDataFunBy:dataModel with:(position +MaxNasUploadPieceSzie) withPara:paraDict success:^(id _Nonnull responseObject) {
|
|
|
+
|
|
|
+ } faild:^(NSError * _Nonnull error) {
|
|
|
+ NSError *err = [NSError new];
|
|
|
+ faildStr(err);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ NSError *err = [NSError new];
|
|
|
+ faildStr(err);
|
|
|
+ }
|
|
|
+
|
|
|
} faild:^(NSError * _Nonnull error) {
|
|
|
- HLog(@"上传失败")
|
|
|
+ HLog(@"%@上传失败",dataModel.filename)
|
|
|
+ faildStr(error);
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
+#pragma mark 分段读视频文件
|
|
|
+-(NSData*)cutVideoFileFunAtIndex:(NSUInteger)dataIndex withMaxLenght:(NSInteger)maxLengt withModel:(uploadFileDataModel*)dataModel{
|
|
|
+ NSString *filePath = [cachesFileManager getFilePathWithName:dataModel.filename type:uploadFileTypeVideo]; // 文件路径
|
|
|
+
|
|
|
+ NSFileManager *manager0 = [NSFileManager defaultManager];
|
|
|
+ if(![manager0 fileExistsAtPath:filePath]) {
|
|
|
+
|
|
|
+ return [NSData new];
|
|
|
+ }
|
|
|
+
|
|
|
+ NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; // 创建文件句柄
|
|
|
+
|
|
|
+ // 设置分段读取的大小,这里以每次读取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];
|
|
|
+ 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];
|
|
|
+ }
|
|
|
+
|
|
|
+ return data;
|
|
|
+}
|
|
|
+
|
|
|
@end
|