|
@@ -19,7 +19,7 @@
|
|
|
//正在下载的上传地址数组
|
|
|
@property(nonatomic,strong) NSMutableArray *uploadingOperationArr;
|
|
|
|
|
|
-@property(nonatomic,assign) NSInteger speedShowCount;//内网的情况 多少次才刷新一次
|
|
|
+@property(nonatomic,strong)NSLock *arrayLock;//数组遍历是修改会挂掉
|
|
|
@end
|
|
|
|
|
|
@implementation nasMixUploadManager
|
|
@@ -37,13 +37,7 @@
|
|
|
if (self = [super init]) {
|
|
|
_maxUploadLoadCount = 2;
|
|
|
|
|
|
- if ([pingManager shareManager].isPingOk)
|
|
|
- {
|
|
|
- _speedShowCount = 3;
|
|
|
- }
|
|
|
- else{
|
|
|
- _speedShowCount = 0;
|
|
|
- }
|
|
|
+ _arrayLock = [NSLock new];
|
|
|
//[self registeNotification];
|
|
|
}
|
|
|
return self;
|
|
@@ -79,12 +73,13 @@
|
|
|
}
|
|
|
|
|
|
//启动上传
|
|
|
- [self beginUpload];
|
|
|
+ [self beginUploadAfterDeleteOperationBy:nil];
|
|
|
}
|
|
|
|
|
|
//在添加XX后 启动下载
|
|
|
-- (void)beginUpload
|
|
|
+- (void)beginUploadAfterDeleteOperationBy:(customUploadOperation*)operation
|
|
|
{
|
|
|
+ HLog(@"beginUploadAfterDeleteOperationBy")
|
|
|
BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
|
|
|
if(!isCanUseCellular){//不允许流量上传
|
|
|
//
|
|
@@ -108,41 +103,50 @@
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- @synchronized (self) {
|
|
|
- if(self.uploadingOperationArr.count == _maxUploadLoadCount){
|
|
|
- HLog(@"正在上传的数量达到了最大值 %ld",_maxUploadLoadCount)
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if(self.uploadWaitingUrlArr.count == 0){
|
|
|
- HLog(@"没有等待中的上传任务")
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- NSInteger canAddTaskNumber = _maxUploadLoadCount - self.uploadingOperationArr.count;
|
|
|
+
|
|
|
+ if(operation){
|
|
|
+ HLog(@"删除完成的上传任务")
|
|
|
+ [_arrayLock lock];
|
|
|
+ [self.uploadingOperationArr removeObject:operation];
|
|
|
+ [_arrayLock unlock];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(self.uploadingOperationArr.count == _maxUploadLoadCount){
|
|
|
+ HLog(@"正在上传的数量达到了最大值 %ld",_maxUploadLoadCount)
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(self.uploadWaitingUrlArr.count == 0){
|
|
|
+ HLog(@"没有等待中的上传任务")
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ NSInteger canAddTaskNumber = _maxUploadLoadCount - self.uploadingOperationArr.count;
|
|
|
+
|
|
|
+ [_arrayLock lock];
|
|
|
+ for (int i=0; i<canAddTaskNumber; i++) {
|
|
|
|
|
|
- for (int i=0; i<canAddTaskNumber; i++) {
|
|
|
+ if(self.uploadWaitingUrlArr.count >= 1){
|
|
|
+ //创建上传任务
|
|
|
+ uploadFileDataModel *WaitingModel = self.uploadWaitingUrlArr.firstObject;
|
|
|
|
|
|
- if(self.uploadWaitingUrlArr.count >= 1){
|
|
|
- //创建上传任务
|
|
|
- uploadFileDataModel *WaitingModel = self.uploadWaitingUrlArr.firstObject;
|
|
|
-
|
|
|
- __block customUploadOperation * operation = [customUploadOperation new];
|
|
|
- operation.fileModel = WaitingModel;
|
|
|
-
|
|
|
- //等待下载中的任务
|
|
|
- [self.uploadWaitingUrlArr removeObjectAtIndex:0];
|
|
|
- //添加到下载中数组
|
|
|
- [self.uploadingOperationArr addObject:operation];
|
|
|
-
|
|
|
- [self checkFileUploadStateWithOperation:operation];
|
|
|
-
|
|
|
- //[weakSelf handleTaskDidUploadWith:operation withState:state];
|
|
|
- }
|
|
|
+ __block customUploadOperation * operation = [customUploadOperation new];
|
|
|
+ operation.fileModel = WaitingModel;
|
|
|
+
|
|
|
+ //等待下载中的任务
|
|
|
+ [self.uploadWaitingUrlArr removeObjectAtIndex:0];
|
|
|
+
|
|
|
+ HLog(@"添加一个新的上传任务")
|
|
|
+ //添加到下载中数组
|
|
|
+ [self.uploadingOperationArr addObject:operation];
|
|
|
|
|
|
+ [self checkFileUploadStateWithOperation:operation];
|
|
|
+
|
|
|
+ //[weakSelf handleTaskDidUploadWith:operation withState:state];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
+ [_arrayLock unlock];
|
|
|
|
|
|
}
|
|
|
|
|
@@ -167,11 +171,12 @@
|
|
|
[weakSelf checkFileUploadStateFunAfterNetWith:model WithOperation:operation];
|
|
|
}
|
|
|
else{
|
|
|
- //FileExistRet(-1);
|
|
|
+ HLog(@"isFileExist接口异常:%@",responseObject)
|
|
|
+ [weakSelf handleUploadFailOneFileBy:operation];
|
|
|
}
|
|
|
} failure:^(NSError * _Nonnull error) {
|
|
|
HLog(@"%@",error)
|
|
|
- //FileExistRet(-1);
|
|
|
+ [weakSelf handleUploadFailOneFileBy:operation];
|
|
|
}];
|
|
|
|
|
|
}
|
|
@@ -180,6 +185,7 @@
|
|
|
- (void)checkFileUploadStateFunAfterNetWith:(frpFileExistModel*)model WithOperation:(customUploadOperation*)operation
|
|
|
{
|
|
|
if(!model){
|
|
|
+ [self handleUploadFailOneFileBy:operation];
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -193,18 +199,7 @@
|
|
|
else if(model.data.isComplete){//上传过了 并且文件上传完了
|
|
|
//判断下文件创建长度是否一致 一致则是上传完了 不一致 重新上传一个 可能是同名的文件而已
|
|
|
if(model.data.size >= operation.fileModel.totalBytes){//上传完了
|
|
|
- mainBlock(^{
|
|
|
- [[iToast makeText:NSLocalizedString(@"File_upload_file_already_exists",nil)] show];
|
|
|
- });
|
|
|
-
|
|
|
- NSMutableArray *delArr = [NSMutableArray new];
|
|
|
- [delArr addObject:operation.fileModel];
|
|
|
-
|
|
|
- [[nasUploadFileManager shareInstance] deleteUploadFileRecordBy:delArr withDelCache:YES complete:^(BOOL isSuccess) {
|
|
|
- if (isSuccess) {
|
|
|
-
|
|
|
- }
|
|
|
- }];
|
|
|
+ [self handleFileDidUploadFunWithOperation:operation];
|
|
|
}
|
|
|
else{//未上传完 isComplete 这个字段有问题
|
|
|
operation.fileModel.didUploadBytes = model.data.size;
|
|
@@ -219,7 +214,22 @@
|
|
|
}
|
|
|
|
|
|
|
|
|
-#pragma mark 处理任务失败
|
|
|
+#pragma mark 任务上传过了了
|
|
|
+- (void)handleFileDidUploadFunWithOperation:(customUploadOperation*)operation
|
|
|
+{
|
|
|
+ mainBlock(^{
|
|
|
+ [[iToast makeText:NSLocalizedString(@"File_upload_file_already_exists",nil)] show];
|
|
|
+ });
|
|
|
+
|
|
|
+ NSMutableArray *delArr = [NSMutableArray new];
|
|
|
+ [delArr addObject:operation.fileModel];
|
|
|
+
|
|
|
+ [[nasUploadFileManager shareInstance] deleteUploadFileRecordBy:delArr withDelCache:YES complete:^(BOOL isSuccess) {
|
|
|
+ if (isSuccess) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }];
|
|
|
+}
|
|
|
|
|
|
|
|
|
#pragma mark 准备上传文件
|
|
@@ -241,6 +251,7 @@
|
|
|
}
|
|
|
else{
|
|
|
HLog(@"获取保存路径失败")
|
|
|
+ [self handleUploadFailOneFileBy:operation];
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -261,6 +272,16 @@
|
|
|
NSString*pathStr = [cachesFileManager getFilePathWithName:operation.fileModel.filename type:operation.fileModel.curUploadFileType];
|
|
|
NSData *imageData = [NSData dataWithContentsOfFile:pathStr];
|
|
|
|
|
|
+ //修复出现过 图片数据小于totalBytes
|
|
|
+ if(imageData){
|
|
|
+ long curDataLengt = [imageData length];
|
|
|
+ if(curDataLengt == operation.fileModel.didUploadBytes && curDataLengt < operation.fileModel.totalBytes){
|
|
|
+ HLog(@"totalBytes 获取出错?")
|
|
|
+ [self handleFileDidUploadFunWithOperation:operation];
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
if (!imageData) {
|
|
|
|
|
|
if(!operation.fileModel.asset){
|
|
@@ -274,9 +295,13 @@
|
|
|
// 直接得到最终的 NSData 数据
|
|
|
if (imageData) {
|
|
|
operation.fileModel.imageData = imageData;
|
|
|
+ if(operation.fileModel.totalBytes == 0){
|
|
|
+ operation.fileModel.totalBytes = [imageData length];
|
|
|
+ }
|
|
|
[weakSelf afterGetImageDataFunWithOperation:operation];
|
|
|
}
|
|
|
else{
|
|
|
+ HLog(@"\n\n\n\nrequestImageDataForAsset fail imageData nil\n\n\n")
|
|
|
[weakSelf handleUploadFailOneFileBy:operation];
|
|
|
}
|
|
|
}];
|
|
@@ -290,6 +315,10 @@
|
|
|
NSData *curData = operation.fileModel.imageData;
|
|
|
operation.onceDataLengt = [curData length];
|
|
|
|
|
|
+ if(operation.fileModel.totalBytes == 0){//异常处理
|
|
|
+ operation.fileModel.totalBytes = operation.onceDataLengt;
|
|
|
+ }
|
|
|
+
|
|
|
[self startUpload:paraDict operation:operation data:curData success:^(id _Nonnull responseObject) {
|
|
|
frpUploadModel *model = [[frpUploadModel alloc] initWithDictionary:responseObject error:nil];
|
|
|
if(model && model.position == operation.fileModel.totalBytes){
|
|
@@ -297,7 +326,8 @@
|
|
|
[weakSelf handleUploadDoneOneFileBy:operation];
|
|
|
}
|
|
|
else{
|
|
|
- HLog(@"%@上传完成一片 %ld",operation.fileModel.filename,model.position)
|
|
|
+ HLog(@"%@上传完成异常 %ld---%ld",operation.fileModel.filename,model.position,operation.fileModel.totalBytes)
|
|
|
+ [weakSelf handleUploadDoneOneFileBy:operation];
|
|
|
}
|
|
|
} faild:^(NSError * _Nonnull error) {
|
|
|
HLog(@"%@上传失败",operation.fileModel.filename)
|
|
@@ -386,7 +416,7 @@
|
|
|
[self startUpload:paraDict operation:operation data:videoData success:^(id _Nonnull responseObject) {
|
|
|
frpUploadModel *model = [[frpUploadModel alloc] initWithDictionary:responseObject error:nil];
|
|
|
if(model && model.position >= operation.fileModel.totalBytes){
|
|
|
- HLog(@"%@上传完成 000",operation.fileModel.filename)
|
|
|
+ HLog(@"%@上传完成 001",operation.fileModel.filename)
|
|
|
[weakSelf handleUploadDoneOneFileBy:operation];
|
|
|
}
|
|
|
else{
|
|
@@ -411,6 +441,8 @@
|
|
|
|
|
|
#pragma mark 分段读视频文件
|
|
|
-(NSData*)cutVideoFileFunAtIndex:(NSUInteger)dataIndex withMaxLenght:(NSInteger)maxLengt withModel:(uploadFileDataModel*)dataModel{
|
|
|
+ HLog(@"视频切片开始 线程:%@",[NSThread currentThread]);
|
|
|
+
|
|
|
NSString *filePath = [cachesFileManager getFilePathWithName:dataModel.filename type:uploadFileTypeVideo]; // 文件路径
|
|
|
|
|
|
NSFileManager *manager0 = [NSFileManager defaultManager];
|
|
@@ -475,9 +507,9 @@
|
|
|
#pragma mark 处理上传完成
|
|
|
- (void)handleUploadDoneOneFileBy:(customUploadOperation*)operation
|
|
|
{
|
|
|
+ HLog(@"handleUploadDoneOneFileBy")
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:nasUploadTaskExeEnd object:operation.fileModel];
|
|
|
- [_uploadingOperationArr removeObject:operation];
|
|
|
- [self beginUpload];
|
|
|
+ [self beginUploadAfterDeleteOperationBy:operation];
|
|
|
}
|
|
|
|
|
|
#pragma mark 处理删除失败
|
|
@@ -485,8 +517,7 @@
|
|
|
{
|
|
|
operation.fileModel.curUploadStateType = uploadStateFail;
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:nasUploadTaskExeError object:operation.fileModel];
|
|
|
- [_uploadingOperationArr removeObject:operation];
|
|
|
- [self beginUpload];
|
|
|
+ [self beginUploadAfterDeleteOperationBy:operation];
|
|
|
}
|
|
|
|
|
|
#pragma mark 根据 asset 获取到图片数据
|
|
@@ -687,7 +718,7 @@
|
|
|
}
|
|
|
}];
|
|
|
|
|
|
- [self beginUpload];
|
|
|
+ [self beginUploadAfterDeleteOperationBy:nil];
|
|
|
}
|
|
|
|
|
|
|
|
@@ -743,67 +774,84 @@ didReceiveResponse:(NSURLResponse *)response
|
|
|
// 32768 = 32KB
|
|
|
HLog(@"didSendBodyData: %lld--%lld-%lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
|
|
|
|
|
|
-
|
|
|
- for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
- if(operation.dataTask == task){
|
|
|
- operation.fileModel.didUploadBytes += bytesSent;
|
|
|
-
|
|
|
- //调整下大小 因为上传的数据 除了data 长度 还包含了 参数大小
|
|
|
- if(totalBytesSent == totalBytesExpectedToSend){
|
|
|
- operation.fileModel.didUploadBytes -= (totalBytesExpectedToSend - operation.onceDataLengt);
|
|
|
- }
|
|
|
-
|
|
|
- operation.i ++;
|
|
|
- if(operation.i >= _speedShowCount){
|
|
|
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:operation.fileModel];
|
|
|
- operation.i = 0;
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ //最后一片数据不刷新
|
|
|
+// if(totalBytesSent == totalBytesExpectedToSend){
|
|
|
+// return;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+// [_arrayLock lock];
|
|
|
+// for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
+// if(operation.dataTask == task){
|
|
|
+// operation.fileModel.didUploadBytes += bytesSent;
|
|
|
+//
|
|
|
+// //调整下大小 因为上传的数据 除了data 长度 还包含了 参数大小
|
|
|
+// if(totalBytesSent == totalBytesExpectedToSend){
|
|
|
+// operation.fileModel.didUploadBytes -= (totalBytesExpectedToSend - operation.onceDataLengt);
|
|
|
+// }
|
|
|
+//
|
|
|
+// NSTimeInterval curTime = [[NSDate date] timeIntervalSince1970];
|
|
|
+// NSTimeInterval timeDiff = curTime - operation.preNotTimeInterval;
|
|
|
+//
|
|
|
+// //HLog(@"控制刷新时间为1秒:%f",timeDiff);
|
|
|
+//
|
|
|
+// if (operation.preNotTimeInterval <= 0
|
|
|
+// || timeDiff > 0.8 ) {
|
|
|
+//
|
|
|
+// operation.preNotTimeInterval = curTime;
|
|
|
+// [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:operation.fileModel];
|
|
|
+// }
|
|
|
+//
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// [_arrayLock unlock];
|
|
|
}
|
|
|
|
|
|
#pragma mark 取消单个任务
|
|
|
- (void)cancelUploadTaskFunWith:(uploadFileDataModel*)fileModel
|
|
|
{
|
|
|
HLog(@"取消任务-- %@--%@",fileModel.filename,fileModel.taskId)
|
|
|
- @synchronized (self) {
|
|
|
- for (uploadFileDataModel*waitModel in _uploadWaitingUrlArr) {
|
|
|
- if([waitModel.filename isEqualToString:fileModel.filename]){
|
|
|
- [_uploadWaitingUrlArr removeObject:waitModel];
|
|
|
- return;
|
|
|
- }
|
|
|
+
|
|
|
+ [_arrayLock lock];
|
|
|
+
|
|
|
+ for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
+ if([operation.fileModel.filename isEqualToString:fileModel.filename]){
|
|
|
+ operation.isCancelType = YES;
|
|
|
+ [operation.dataTask cancel];
|
|
|
+ [_arrayLock unlock];
|
|
|
+ [self beginUploadAfterDeleteOperationBy:operation];
|
|
|
+ return;
|
|
|
}
|
|
|
-
|
|
|
- for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
- if([operation.fileModel.filename isEqualToString:fileModel.filename]){
|
|
|
- operation.isCancelType = YES;
|
|
|
- [operation.dataTask cancel];
|
|
|
- [_uploadingOperationArr removeObject:operation];
|
|
|
- [self beginUpload];
|
|
|
- break;
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (uploadFileDataModel*waitModel in _uploadWaitingUrlArr) {
|
|
|
+ if([waitModel.filename isEqualToString:fileModel.filename]){
|
|
|
+ [_uploadWaitingUrlArr removeObject:waitModel];
|
|
|
+ [_arrayLock lock];
|
|
|
+ [self beginUploadAfterDeleteOperationBy:nil];
|
|
|
+ return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ [_arrayLock unlock];
|
|
|
+
|
|
|
}
|
|
|
|
|
|
#pragma mark 取消所有任务
|
|
|
- (void)cancelUploadAllTaskFun
|
|
|
{
|
|
|
HLog(@"取消所有任务")
|
|
|
- @synchronized (self) {
|
|
|
- [_uploadWaitingUrlArr removeAllObjects];
|
|
|
-
|
|
|
- for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
- operation.isCancelType = YES;
|
|
|
- [operation.dataTask cancel];
|
|
|
- }
|
|
|
-
|
|
|
- [_uploadingOperationArr removeAllObjects];
|
|
|
+ [_arrayLock lock];
|
|
|
+ [_uploadWaitingUrlArr removeAllObjects];
|
|
|
+
|
|
|
+ for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
+ operation.isCancelType = YES;
|
|
|
+ [operation.dataTask cancel];
|
|
|
}
|
|
|
+
|
|
|
+ [_uploadingOperationArr removeAllObjects];
|
|
|
+ [_arrayLock unlock];
|
|
|
}
|
|
|
|
|
|
|