|
|
@@ -11,7 +11,7 @@
|
|
|
#import "AFHTTPSessionManager.h"
|
|
|
#import "frpUploadModel.h"
|
|
|
|
|
|
-#define Kboundary @"----WebKitFormBoundaryjv0UfA04ED44AhWx"
|
|
|
+#define Kboundary @"Boundaryhxd"
|
|
|
#define KNewLine [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]
|
|
|
|
|
|
@interface nasUploadManager ()<NSURLSessionDataDelegate>
|
|
|
@@ -68,17 +68,17 @@
|
|
|
}
|
|
|
|
|
|
// 2 接收数据
|
|
|
-- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
|
|
|
-
|
|
|
- NSDictionary *infoDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
|
|
|
- HLog(@"%@", infoDict);
|
|
|
-
|
|
|
-}
|
|
|
+//- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
|
|
|
+//
|
|
|
+// NSDictionary *infoDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
|
|
|
+// HLog(@"%@", infoDict);
|
|
|
+//
|
|
|
+//}
|
|
|
|
|
|
// 3 完成
|
|
|
-- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
|
|
|
- HLog(@"task:%@---error:%@", task,error);
|
|
|
-}
|
|
|
+//- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
|
|
|
+// HLog(@"上传完成 task:%@---error:%@", task,error);
|
|
|
+//}
|
|
|
|
|
|
- (NSMutableData *)getBodyDataWithRequest:(NSMutableURLRequest *)request withModel:(uploadFileDataModel*)dataModel withData:(NSData*)data withPara:(NSMutableDictionary*)params{
|
|
|
|
|
|
@@ -102,15 +102,18 @@
|
|
|
[paraString appendFormat:@"--%@\r\n",Kboundary];//\n:换行 \n:切换到行首
|
|
|
[paraString appendFormat:@"Content-Disposition: form-data; name=\"%@\"",key];
|
|
|
[paraString appendFormat:@"\r\n"];
|
|
|
+ [paraString appendFormat:@"\r\n"];
|
|
|
[paraString appendFormat:@"%@\r\n",value];
|
|
|
}
|
|
|
|
|
|
- [boydData appendData:[paraString dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
+ //[boydData appendData:[paraString dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
|
|
|
|
|
// body每一个段内容以换行符作为结束标示
|
|
|
NSString *fileBeginBoundary = [NSString stringWithFormat:@"--%@\r\n", Kboundary];
|
|
|
- [boydData appendData:[fileBeginBoundary dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
+ //[boydData appendData:[fileBeginBoundary dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
+
|
|
|
+ [paraString appendString:fileBeginBoundary];
|
|
|
|
|
|
// 2.2 属性配置 名字;key;类型
|
|
|
|
|
|
@@ -133,7 +136,7 @@
|
|
|
*/
|
|
|
//2.3 拼接数据(创建一个字符串来拼装)
|
|
|
NSMutableString *string = [NSMutableString new];
|
|
|
- [string appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\" ", serverFileKey, serverFileName];
|
|
|
+ [string appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\" ", @"file", serverFileName];
|
|
|
//[string appendFormat:@"%@", KNewLine];
|
|
|
[string appendFormat:@"\r\n"];
|
|
|
[string appendFormat:@"Content-Type:%@", serverContentTypes];
|
|
|
@@ -141,13 +144,16 @@
|
|
|
// [string appendFormat:@"%@", KNewLine];
|
|
|
[string appendFormat:@"\r\n"];
|
|
|
[string appendFormat:@"\r\n"];
|
|
|
- [boydData appendData:[string dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
+ // [boydData appendData:[string dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
+
|
|
|
+ [paraString appendString:string];
|
|
|
+ [boydData appendData:[paraString dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
|
|
// 2.3 拼接数据(拼接文件数据)
|
|
|
[boydData appendData:data];
|
|
|
|
|
|
// 2.4 边界符号 (结束边界)
|
|
|
- NSString *fileEndBoundary = [NSString stringWithFormat:@"\r\n--%@", Kboundary];
|
|
|
+ NSString *fileEndBoundary = [NSString stringWithFormat:@"\r\n--%@--", Kboundary];
|
|
|
[boydData appendData:[fileEndBoundary dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
|
|
return boydData;
|
|
|
@@ -174,7 +180,7 @@
|
|
|
//设置 POST请求文件上传
|
|
|
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",Kboundary] forHTTPHeaderField:@"Content-Type"];
|
|
|
|
|
|
-
|
|
|
+ KWeakSelf
|
|
|
//回话对象
|
|
|
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
|
|
|
|
|
|
@@ -186,7 +192,8 @@
|
|
|
*/
|
|
|
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:nil completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
//解析
|
|
|
- HLog(@"data string:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
|
|
|
+ //HLog(@"data string:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
|
|
|
+ [weakSelf handleCustomUploadResultBy:data withResponse:response withError:error success:success faild:faildStr];
|
|
|
}];
|
|
|
|
|
|
// NSString *filePath = [cachesFileManager getFilePathWithName:dataModel.filename type:uploadFileTypeVideo]; // 文件路径
|
|
|
@@ -194,16 +201,52 @@
|
|
|
//
|
|
|
// NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:filePathUrl completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
// HLog(@"data string:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
|
|
|
+ //[weakSelf handleCustomUploadResultBy:data withResponse:response withError:error];
|
|
|
// }];
|
|
|
+
|
|
|
+// NSURLSessionDataTask *uploadTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
+//
|
|
|
+// HLog(@"data string:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
|
|
|
+ //[weakSelf handleCustomUploadResultBy:data withResponse:response withError:error];
|
|
|
+//
|
|
|
+//// NSJSONSerialization *object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
|
|
|
+//// NSDictionary *dict = (NSDictionary *)object;
|
|
|
+//// NSLog(@"=====%@",[dict objectForKey:@"success"]);
|
|
|
+// }];
|
|
|
+
|
|
|
+
|
|
|
//执行Task
|
|
|
[uploadTask resume];
|
|
|
+ self.uploadTask = uploadTask;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark 用系统方法写的上传处理
|
|
|
+- (void)handleCustomUploadResultBy:(NSData*)data withResponse:(NSURLResponse*)response withError:(NSError*)error success:(netWork_Success)success faild:(netWork_Faild)faildStr{
|
|
|
+ if(error){
|
|
|
+ HLog(@"上传错误:%@",error)
|
|
|
+ faildStr(error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ NSJSONSerialization *object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
|
|
|
+ NSDictionary *dict = (NSDictionary *)object;
|
|
|
+
|
|
|
+ frpUploadModel *model = [[frpUploadModel alloc] initWithDictionary:dict error:nil];
|
|
|
+ if(model && [model.msg isEqualToString:@"success"]){
|
|
|
+ success(dict);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ NSError *err = [NSError new];
|
|
|
+ faildStr(err);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
- (void)nasUploadFileToFileServiceWithParams:(NSMutableDictionary *)params model:(uploadFileDataModel*)dataModel data:(NSData *)data success:(netWork_Success)success faild:(netWork_Faild)faildStr {
|
|
|
|
|
|
//test code
|
|
|
- [self startUpload:params model:dataModel data:data success:success faild:faildStr];
|
|
|
- return;
|
|
|
+// [self startUpload:params model:dataModel data:data success:success faild:faildStr];
|
|
|
+// return;
|
|
|
|
|
|
NSString *urlString = ksharedAppDelegate.NASFileByBoxService;
|
|
|
urlString = [[NSString alloc] initWithFormat:@"%@uploadFile",urlString];
|
|
|
@@ -214,7 +257,8 @@
|
|
|
position = positionNumber.longValue;
|
|
|
}
|
|
|
|
|
|
- HLog(@"上传地址:%@---%@---position:%ld--",urlString,params,position);
|
|
|
+ __block long dataLength = [data length];
|
|
|
+ HLog(@"上传地址:%@---%@---position:%ld--data 长度:%ld",urlString,params,position,[data length]);
|
|
|
|
|
|
self.uploadTask = [_uploadManager POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
|
|
|
|
|
|
@@ -232,7 +276,14 @@
|
|
|
|
|
|
} progress:^(NSProgress * _Nonnull uploadProgress) {
|
|
|
HLog(@"上传 Progress:--%@---%lld",uploadProgress,uploadProgress.completedUnitCount)
|
|
|
- dataModel.didUploadBytes = position + (uploadProgress.completedUnitCount);
|
|
|
+ //dataModel.didUploadBytes = position + (uploadProgress.completedUnitCount);
|
|
|
+
|
|
|
+ long didSendData = (long)(uploadProgress.fractionCompleted *dataLength);
|
|
|
+ if(uploadProgress.fractionCompleted==1){
|
|
|
+ didSendData = dataLength;
|
|
|
+ }
|
|
|
+ dataModel.didUploadBytes = position + didSendData;
|
|
|
+
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:dataModel];
|
|
|
|
|
|
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
|
|
|
@@ -276,7 +327,7 @@
|
|
|
NSData *curData = dataModel.imageData;
|
|
|
|
|
|
[self nasUploadFileToFileServiceWithParams:paraDict model:dataModel data:curData success:^(id _Nonnull responseObject) {
|
|
|
- HLog(@"%@上传完成",dataModel.filename)
|
|
|
+ HLog(@"%@上传完成 000",dataModel.filename)
|
|
|
success(responseObject);
|
|
|
} faild:^(NSError * _Nonnull error) {
|
|
|
HLog(@"%@上传失败",dataModel.filename)
|
|
|
@@ -287,36 +338,33 @@
|
|
|
[paraDict setObject:@1 forKey:@"videoType"];
|
|
|
|
|
|
//断点续传处理 先传1字节数据看服务器是否有数据
|
|
|
- //NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:1 withModel:dataModel];
|
|
|
- //[paraDict setObject:@0 forKey:@"isLast"];
|
|
|
-
|
|
|
- __block NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
-
|
|
|
- // NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
+ __block NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:1 withModel:dataModel];
|
|
|
+ [paraDict setObject:@0 forKey:@"isLast"];
|
|
|
|
|
|
+// __block NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
|
|
|
[self nasUploadFileToFileServiceWithParams:paraDict model:dataModel data:videoData success:^(id _Nonnull responseObject) {
|
|
|
- HLog(@"%@上传完成",dataModel.filename)
|
|
|
+ HLog(@"%@上传完成 111 %@",dataModel.filename,responseObject)
|
|
|
videoData = nil;
|
|
|
success(responseObject);
|
|
|
|
|
|
-// 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);
|
|
|
-// }
|
|
|
+ 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) {
|
|
|
videoData = nil;
|
|
|
- HLog(@"%@上传失败",dataModel.filename)
|
|
|
+ HLog(@"%@上传失败 -- %@",dataModel.filename,error)
|
|
|
faildStr(error);
|
|
|
}];
|
|
|
}
|
|
|
@@ -336,10 +384,10 @@
|
|
|
|
|
|
//视频数据切片
|
|
|
__block NSData *videoData = [self cutVideoFileFunAtIndex:position withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
-
|
|
|
+
|
|
|
KWeakSelf
|
|
|
[self nasUploadFileToFileServiceWithParams:paraDict model:dataModel data:videoData success:^(id _Nonnull responseObject) {
|
|
|
- HLog(@"%@上传完成",dataModel.filename)
|
|
|
+ HLog(@"%@上传完成 222 %@",dataModel.filename,responseObject)
|
|
|
videoData = nil;
|
|
|
|
|
|
frpUploadModel *model = [[frpUploadModel alloc] initWithDictionary:responseObject error:nil];
|
|
|
@@ -348,8 +396,8 @@
|
|
|
success(responseObject);
|
|
|
}
|
|
|
else{
|
|
|
- [weakSelf beginUploadVideoDataFunBy:dataModel with:(position +MaxNasUploadPieceSzie) withPara:paraDict success:^(id _Nonnull responseObject) {
|
|
|
-
|
|
|
+ [weakSelf beginUploadVideoDataFunBy:dataModel with:model.position withPara:paraDict success:^(id _Nonnull responseObject) {
|
|
|
+ success(responseObject);
|
|
|
} faild:^(NSError * _Nonnull error) {
|
|
|
NSError *err = [NSError new];
|
|
|
faildStr(err);
|
|
|
@@ -385,6 +433,17 @@
|
|
|
const NSUInteger chunkSize = maxLengt;//cutVideoPieceSzie;//5 * 1024 *1024;
|
|
|
NSMutableData *data = [NSMutableData data];
|
|
|
|
|
|
+ //把视频切成两份文件给凌云
|
|
|
+// NSData* chunk = [fileHandle readDataOfLength:MaxNasUploadPieceSzie];
|
|
|
+//
|
|
|
+// [cachesFileManager getFileNameWithContent:chunk fileName:[[NSString alloc] initWithFormat:@"1_%@",dataModel.filename] type:uploadFileTypeVideo];
|
|
|
+//
|
|
|
+// [fileHandle seekToFileOffset:MaxNasUploadPieceSzie];
|
|
|
+// NSData* chunk2 = [fileHandle readDataToEndOfFile];
|
|
|
+// [cachesFileManager getFileNameWithContent:chunk2 fileName:[[NSString alloc] initWithFormat:@"2_%@",dataModel.filename] type:uploadFileTypeVideo];
|
|
|
+//
|
|
|
+// return chunk2;
|
|
|
+
|
|
|
if (fileHandle) {
|
|
|
|
|
|
long long endOfFile = [fileHandle seekToEndOfFile];
|
|
|
@@ -428,6 +487,7 @@
|
|
|
[fileHandle closeFile];
|
|
|
}
|
|
|
|
|
|
+ HLog(@"视频切片完成 dataIndex:%ld --长度:%ld",dataIndex,[data length])
|
|
|
return data;
|
|
|
}
|
|
|
|