|
@@ -19,7 +19,6 @@
|
|
|
//正在下载的上传地址数组
|
|
|
@property(nonatomic,strong) NSMutableArray *uploadingOperationArr;
|
|
|
|
|
|
-@property(nonatomic,strong)NSLock *arrayLock;//数组遍历是修改会挂掉
|
|
|
@end
|
|
|
|
|
|
@implementation nasMixUploadManager
|
|
@@ -37,7 +36,6 @@
|
|
|
if (self = [super init]) {
|
|
|
_maxUploadLoadCount = 2;
|
|
|
|
|
|
- _arrayLock = [NSLock new];
|
|
|
//[self registeNotification];
|
|
|
}
|
|
|
return self;
|
|
@@ -53,7 +51,7 @@
|
|
|
|
|
|
//1. 排查上传中
|
|
|
for (customUploadOperation *operationDoing in self.uploadingOperationArr) {
|
|
|
- if([operationDoing.fileModel.filename isEqualToString:model.filename]){
|
|
|
+ if([operationDoing.fileModel.localIdentifier isEqualToString:model.localIdentifier]){
|
|
|
needAddType = NO;
|
|
|
break;
|
|
|
}
|
|
@@ -61,7 +59,7 @@
|
|
|
|
|
|
//1. 排查等待下载
|
|
|
for (uploadFileDataModel *waitModel in self.uploadWaitingUrlArr) {
|
|
|
- if([waitModel.filename isEqualToString:model.filename]){
|
|
|
+ if([waitModel.localIdentifier isEqualToString:model.localIdentifier]){
|
|
|
needAddType = NO;
|
|
|
break;
|
|
|
}
|
|
@@ -106,12 +104,20 @@
|
|
|
|
|
|
if(operation){
|
|
|
HLog(@"删除完成的上传任务")
|
|
|
- [_arrayLock lock];
|
|
|
- if(operation.fileHandle){
|
|
|
- [operation closeFileHandleFun];
|
|
|
+
|
|
|
+// if([operation.fileModel.filename isEqualToString:@"IMG_8876.PNG"]){
|
|
|
+// HLog(@"uploadWaitingUrlArr -- count:%ld",self.uploadWaitingUrlArr.count)
|
|
|
+// for(uploadFileDataModel*model in self.uploadWaitingUrlArr){
|
|
|
+// HLog(@"fileName--%@--%ld",model.filename,model.totalBytes)
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ @synchronized (self) {
|
|
|
+ if(operation.fileHandle){
|
|
|
+ [operation closeFileHandleFun];
|
|
|
+ }
|
|
|
+ [self.uploadingOperationArr removeObject:operation];
|
|
|
}
|
|
|
- [self.uploadingOperationArr removeObject:operation];
|
|
|
- [_arrayLock unlock];
|
|
|
}
|
|
|
|
|
|
if(self.uploadingOperationArr.count == _maxUploadLoadCount){
|
|
@@ -129,46 +135,48 @@
|
|
|
//新添加的任务
|
|
|
NSMutableArray *newTaskArr = [NSMutableArray new];
|
|
|
|
|
|
- [_arrayLock lock];
|
|
|
- for (int i=0; i<canAddTaskNumber; i++) {
|
|
|
-
|
|
|
- if(self.uploadWaitingUrlArr.count >= 1){
|
|
|
- //创建上传任务
|
|
|
- uploadFileDataModel *WaitingModel = self.uploadWaitingUrlArr.firstObject;
|
|
|
-
|
|
|
- customUploadOperation * operation = [customUploadOperation new];
|
|
|
- operation.fileModel = WaitingModel;
|
|
|
-
|
|
|
- //等待下载中的任务
|
|
|
- [self.uploadWaitingUrlArr removeObjectAtIndex:0];
|
|
|
-
|
|
|
- HLog(@"添加一个新的上传任务 %@",self.uploadingOperationArr)
|
|
|
- //添加到下载中数组
|
|
|
- [self.uploadingOperationArr addObject:operation];
|
|
|
+ @synchronized (self) {
|
|
|
+ for (int i=0; i<canAddTaskNumber; i++) {
|
|
|
|
|
|
- //[self checkFileUploadStateWithOperation:operation];
|
|
|
-
|
|
|
- [newTaskArr addObject:operation];
|
|
|
+ if(self.uploadWaitingUrlArr.count >= 1){
|
|
|
+ //创建上传任务
|
|
|
+ uploadFileDataModel *WaitingModel = self.uploadWaitingUrlArr.firstObject;
|
|
|
+
|
|
|
+ customUploadOperation * operation = [customUploadOperation new];
|
|
|
+ operation.fileModel = WaitingModel;
|
|
|
+
|
|
|
+ //等待下载中的任务
|
|
|
+ [self.uploadWaitingUrlArr removeObjectAtIndex:0];
|
|
|
+
|
|
|
+ HLog(@"添加一个新的上传任务 %@",self.uploadingOperationArr)
|
|
|
+ //添加到下载中数组
|
|
|
+ [self.uploadingOperationArr addObject:operation];
|
|
|
+
|
|
|
+ //[self checkFileUploadStateWithOperation:operation];
|
|
|
+
|
|
|
+ [newTaskArr addObject:operation];
|
|
|
+
|
|
|
+ //[weakSelf handleTaskDidUploadWith:operation withState:state];
|
|
|
+ }
|
|
|
|
|
|
- //[weakSelf handleTaskDidUploadWith:operation withState:state];
|
|
|
}
|
|
|
|
|
|
+ //开始检测新的上传任务
|
|
|
+ for (customUploadOperation * operation in newTaskArr) {
|
|
|
+ [self checkFileUploadStateWithOperation:operation];
|
|
|
+ }
|
|
|
+
|
|
|
+ [newTaskArr removeAllObjects];
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- //开始检测新的上传任务
|
|
|
- for (customUploadOperation * operation in newTaskArr) {
|
|
|
- [self checkFileUploadStateWithOperation:operation];
|
|
|
- }
|
|
|
-
|
|
|
- [newTaskArr removeAllObjects];
|
|
|
-
|
|
|
- [_arrayLock unlock];
|
|
|
-
|
|
|
}
|
|
|
|
|
|
#pragma mark 检测文件是否上传过了
|
|
|
- (void)checkFileUploadStateWithOperation:(customUploadOperation*)operation
|
|
|
{
|
|
|
+ HLog(@"%@检测文件是否过--%ld",operation.fileModel.filename,operation.fileModel.totalBytes)
|
|
|
+
|
|
|
NSMutableDictionary*paraDict = [NSMutableDictionary new];
|
|
|
|
|
|
//operation.fileModel.filename = @"Img_4107.png";// test same name file
|
|
@@ -224,6 +232,7 @@
|
|
|
operation.fileModel.taskId = model.data.fileTaskId;
|
|
|
|
|
|
if(!model.data.exist){//未上传过
|
|
|
+ HLog(@"%@检测到文件未上传过--%ld",operation.fileModel.filename,operation.fileModel.totalBytes)
|
|
|
[self prepareToUploadFileWithOperation:operation];
|
|
|
}
|
|
|
else if(model.data.isComplete){//上传过了 并且文件上传完了
|
|
@@ -236,16 +245,22 @@
|
|
|
// [self prepareToUploadFileWithOperation:operation];
|
|
|
// }
|
|
|
|
|
|
+ HLog(@"%@检测到文件上传完成--%ld",operation.fileModel.filename,operation.fileModel.totalBytes)
|
|
|
+
|
|
|
//修复同名文件丢失问题
|
|
|
if(model.data.size == operation.fileModel.totalBytes){//上传完了
|
|
|
+ HLog(@"1111111")
|
|
|
[self handleFileDidUploadFunWithOperation:operation];
|
|
|
}
|
|
|
else{//同名文件 处理
|
|
|
+ HLog(@"2222222")
|
|
|
[self prepareToUploadFileWithOperation:operation];
|
|
|
}
|
|
|
|
|
|
}
|
|
|
else{//上传过了 未上传完成
|
|
|
+ HLog(@"%@检测到文件上传过了 未上传完成--%ld",operation.fileModel.filename,operation.fileModel.totalBytes)
|
|
|
+
|
|
|
operation.fileModel.didUploadBytes = model.data.size;
|
|
|
[self prepareToUploadFileWithOperation:operation];
|
|
|
}
|
|
@@ -364,6 +379,7 @@
|
|
|
operation.fileModel.imageData = imageData;
|
|
|
}
|
|
|
|
|
|
+ HLog(@"%@开始上传--%ld",operation.fileModel.filename,operation.fileModel.totalBytes)
|
|
|
|
|
|
NSData *curData = operation.fileModel.imageData;
|
|
|
operation.onceDataLengt = [curData length];
|
|
@@ -375,7 +391,7 @@
|
|
|
[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 && model.position != 0){
|
|
|
- HLog(@"%@上传完成 000",operation.fileModel.filename)
|
|
|
+ HLog(@"%@上传完成 000---%ld",operation.fileModel.filename,operation.fileModel.totalBytes)
|
|
|
[weakSelf handleUploadDoneOneFileBy:operation];
|
|
|
}
|
|
|
else{
|
|
@@ -838,39 +854,39 @@ didReceiveResponse:(NSURLResponse *)response
|
|
|
// 32768 = 32KB
|
|
|
HLog(@"didSendBodyData: %lld--%lld-%lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
|
|
|
|
|
|
- [_arrayLock lock];
|
|
|
- for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
- HLog(@"正在遍历数组 _uploadingOperationArr:%@",_uploadingOperationArr);
|
|
|
-
|
|
|
- if(operation.dataTask == task){
|
|
|
- operation.fileModel.didUploadBytes += bytesSent;
|
|
|
+ @synchronized (self) {
|
|
|
+ for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
+ HLog(@"正在遍历数组 _uploadingOperationArr:%@",_uploadingOperationArr);
|
|
|
|
|
|
- //调整下大小 因为上传的数据 除了data 长度 还包含了 参数大小
|
|
|
- if(totalBytesSent == totalBytesExpectedToSend){
|
|
|
- operation.fileModel.didUploadBytes -= (totalBytesExpectedToSend - operation.onceDataLengt);
|
|
|
- }
|
|
|
-
|
|
|
- NSTimeInterval curTime = [[NSDate date] timeIntervalSince1970];
|
|
|
- NSTimeInterval timeDiff = curTime - operation.preNotTimeInterval;
|
|
|
-
|
|
|
- //HLog(@"控制刷新时间为1秒:%f",timeDiff);
|
|
|
-
|
|
|
- CGFloat RefreshTimer = 0.8;
|
|
|
- if(operation.fileModel.totalBytes <= 5*1024*1024){//小于5M 事实
|
|
|
- RefreshTimer = 0.2;
|
|
|
- }
|
|
|
-
|
|
|
- if (operation.preNotTimeInterval <= 0
|
|
|
- || timeDiff > RefreshTimer ) {
|
|
|
+ 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;
|
|
|
|
|
|
- operation.preNotTimeInterval = curTime;
|
|
|
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileUploadingNotification object:operation.fileModel];
|
|
|
+ //HLog(@"控制刷新时间为1秒:%f",timeDiff);
|
|
|
+
|
|
|
+ CGFloat RefreshTimer = 0.8;
|
|
|
+ if(operation.fileModel.totalBytes <= 5*1024*1024){//小于5M 事实
|
|
|
+ RefreshTimer = 0.2;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (operation.preNotTimeInterval <= 0
|
|
|
+ || timeDiff > RefreshTimer ) {
|
|
|
+
|
|
|
+ operation.preNotTimeInterval = curTime;
|
|
|
+ [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileUploadingNotification object:operation.fileModel];
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
}
|
|
|
-
|
|
|
- break;
|
|
|
}
|
|
|
}
|
|
|
- [_arrayLock unlock];
|
|
|
}
|
|
|
|
|
|
#pragma mark 取消单个任务
|
|
@@ -878,38 +894,32 @@ didReceiveResponse:(NSURLResponse *)response
|
|
|
{
|
|
|
HLog(@"取消任务-- %@--%@",fileModel.filename,fileModel.taskId)
|
|
|
|
|
|
- BOOL didUnLockType = NO;
|
|
|
- [_arrayLock lock];
|
|
|
-
|
|
|
- for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
- HLog(@"正在遍历数组 _uploadingOperationArr:%@",_uploadingOperationArr);
|
|
|
- if([operation.fileModel.filename isEqualToString:fileModel.filename]){
|
|
|
- operation.isCancelType = YES;
|
|
|
- [operation.dataTask cancel];
|
|
|
-
|
|
|
- if(operation.fileHandle){
|
|
|
- [operation closeFileHandleFun];
|
|
|
+ @synchronized (self) {
|
|
|
+
|
|
|
+ for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
+ HLog(@"正在遍历数组 _uploadingOperationArr:%@",_uploadingOperationArr);
|
|
|
+ if([operation.fileModel.localIdentifier isEqualToString:fileModel.localIdentifier]){
|
|
|
+ operation.isCancelType = YES;
|
|
|
+ [operation.dataTask cancel];
|
|
|
+
|
|
|
+ if(operation.fileHandle){
|
|
|
+ [operation closeFileHandleFun];
|
|
|
+ }
|
|
|
+
|
|
|
+ [self beginUploadAfterDeleteOperationBy:operation];
|
|
|
+ return;
|
|
|
}
|
|
|
-
|
|
|
- didUnLockType = YES;
|
|
|
- [_arrayLock unlock];
|
|
|
- [self beginUploadAfterDeleteOperationBy:operation];
|
|
|
- return;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- for (uploadFileDataModel*waitModel in _uploadWaitingUrlArr) {
|
|
|
- if([waitModel.filename isEqualToString:fileModel.filename]){
|
|
|
- [_uploadWaitingUrlArr removeObject:waitModel];
|
|
|
- didUnLockType = YES;
|
|
|
- [_arrayLock unlock];
|
|
|
- [self beginUploadAfterDeleteOperationBy:nil];
|
|
|
- return;
|
|
|
+
|
|
|
+ for (uploadFileDataModel*waitModel in _uploadWaitingUrlArr) {
|
|
|
+ if(//[waitModel.filename isEqualToString:fileModel.filename]
|
|
|
+ [waitModel.localIdentifier isEqualToString:fileModel.localIdentifier]){
|
|
|
+ [_uploadWaitingUrlArr removeObject:waitModel];
|
|
|
+ [self beginUploadAfterDeleteOperationBy:nil];
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- if (!didUnLockType) {
|
|
|
- [_arrayLock unlock];
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -917,20 +927,20 @@ didReceiveResponse:(NSURLResponse *)response
|
|
|
- (void)cancelUploadAllTaskFun
|
|
|
{
|
|
|
HLog(@"取消所有任务")
|
|
|
- [_arrayLock lock];
|
|
|
- [_uploadWaitingUrlArr removeAllObjects];
|
|
|
-
|
|
|
- for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
- HLog(@"正在遍历数组 _uploadingOperationArr:%@",_uploadingOperationArr);
|
|
|
- operation.isCancelType = YES;
|
|
|
- [operation.dataTask cancel];
|
|
|
- if(operation.fileHandle){
|
|
|
- [operation closeFileHandleFun];
|
|
|
+ @synchronized (self) {
|
|
|
+ [_uploadWaitingUrlArr removeAllObjects];
|
|
|
+
|
|
|
+ for (customUploadOperation*operation in _uploadingOperationArr) {
|
|
|
+ HLog(@"正在遍历数组 _uploadingOperationArr:%@",_uploadingOperationArr);
|
|
|
+ operation.isCancelType = YES;
|
|
|
+ [operation.dataTask cancel];
|
|
|
+ if(operation.fileHandle){
|
|
|
+ [operation closeFileHandleFun];
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ [_uploadingOperationArr removeAllObjects];
|
|
|
}
|
|
|
-
|
|
|
- [_uploadingOperationArr removeAllObjects];
|
|
|
- [_arrayLock unlock];
|
|
|
}
|
|
|
|
|
|
|