1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009 |
- //
- // uploadFileManager.m
- // 隐私保护
- //
- // Created by xd h on 2023/11/15.
- //
- #import "uploadFileManager.h"
- #import <AssetsLibrary/AssetsLibrary.h>
- #import "photosBackupsTaskModel.h"
- #import "AFNetworkReachabilityManager.h"
- @interface uploadFileManager ()
- /**检测任务10s后 无反馈重新开始 */
- @property (nonatomic, assign) NSInteger taskRenewTime; //
- @end
- @implementation uploadFileManager
- static uploadFileManager * cur_uploadFileShareInstance = nil;
- +(uploadFileManager *)shareInstance;
- {
- static dispatch_once_t onceToken;
-
- dispatch_once(&onceToken, ^{
- cur_uploadFileShareInstance = [[uploadFileManager alloc] init];
-
- });
-
- return cur_uploadFileShareInstance;
- }
- - (id)init
- {
- self = [super init];
-
- if (self) {
- //[self initManager];
- }
-
- return self;
- }
- #pragma mark 读取数据库数据
- - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
- {
- if(_databaseArr && _databaseArr.count == 3 && !isReGet){
- complete(_databaseArr);
- return;
- }
-
- if(!_databaseArr)
- {
- _databaseArr = [NSMutableArray new];
- }
-
- //KWeakSelf
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
- // NSLock *lock = [NSLock new];
- // [lock lock];
- [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:0 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
- NSMutableArray *failArr = [NSMutableArray new];
- NSMutableArray *doneArr = [NSMutableArray new];
- NSMutableArray *otherArr = [NSMutableArray new];
-
- for (uploadFileDataModel * curModel in array) {
-
- //图片 和视频 还原
- if(curModel.curUploadFileType == uploadFileTypeImage){
- }
- else if(curModel.curUploadFileType == uploadFileTypeVideo){
- if(curModel.curUploadStateType != uploadStateDone){
-
- }
- }
-
- if(curModel.curUploadStateType == uploadStateFail){
- [failArr addObject:curModel];
- }
- else if(curModel.curUploadStateType == uploadStateDone){
- [doneArr addObject:curModel];
- }
- else{
- [otherArr addObject:curModel];
- }
- }
-
- NSLock *lock = [NSLock new];
- [lock lock];
-
- NSMutableArray *newArr = [NSMutableArray new];
- [newArr addObject:otherArr];
- [newArr addObject:doneArr];
- [newArr addObject:failArr];
- self->_databaseArr = newArr;
-
- [lock unlock];
-
- complete(self->_databaseArr);
- }];
- // [lock unlock];
- });
- }
- //把TZAssetModel 转成 我们需要上传的model
- - (void)handlTZAssetModelToUploadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
- {
-
- if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
- complete(nil);
- return;
- }
-
- if(ksharedAppDelegate.DisabledFileTransferType){
- if(ksharedAppDelegate.isImageNewFor130){
- [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
- }
- else{
- [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
- }
-
- complete(_databaseArr);
- return;
- }
-
- if(!_fileModelDataArr){
- _fileModelDataArr = [NSMutableArray new];
- }
-
- _isNewAddTaskType = YES;
-
- self.curUploadModelNumbers = indexPathsForSelectedItems.count;
-
- NSMutableArray *newUploadTaskArr = [NSMutableArray new];
-
- NSLock *lock = [NSLock new];
-
- for (TZAssetModel * model in indexPathsForSelectedItems) {
- uploadFileDataModel * curModel = [uploadFileDataModel new];
- curModel.asset = model.asset;
- curModel.localIdentifier = model.asset.localIdentifier;
-
- BOOL isRepeatingTasksType = NO;
- [lock lock];
- //查询上传任务是否存在
- if(_databaseArr && _databaseArr.count == 3 ){
- NSMutableArray *uploadingArr = [[NSMutableArray alloc] initWithArray:_databaseArr[0]];
- for (uploadFileDataModel *preModel in uploadingArr) {
- if([preModel.localIdentifier isEqualToString:curModel.localIdentifier]){
- isRepeatingTasksType = YES;
- break;
- }
- }
- }
- [lock unlock];
-
- if(isRepeatingTasksType){
- continue;
- }
-
- curModel.imageData = model.imageData;
- curModel.videoData = model.videoData;
- curModel.filename = [model.asset valueForKey:@"filename"];
- //curModel.curUploadStateType = uploadStateWait;
- curModel.curUploadStateType = uploadStateUploading;
-
- if(model.type == TZAssetModelMediaTypeVideo){
- curModel.curUploadFileType = uploadFileTypeVideo;
- [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
-
-
- //curModel.totalBytes = [model.videoData length];
- curModel.totalBytes = model.totalBytes;
- curModel.videoData = [NSData new];//视频文件存储到文件后内存清空
-
- NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
- curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
-
- //第一帧图片
- [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
- // 直接得到最终的 NSData 数据
- if (imageData) {
- curModel.imageData = imageData;
- [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];
- }
- }];
- }
- else{
- curModel.curUploadFileType = uploadFileTypeImage;
- curModel.totalBytes = model.totalBytes;
-
- if(curModel.imageData)
- {
- [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
- }
- else{
-
- // [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
- // // 直接得到最终的 NSData 数据
- // if (imageData) {
- // curModel.imageData = imageData;
- // [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];;
- // }
- // }];
- }
-
- }
-
- //保存到数据库
- curModel.bg_tableName = upLoadFile_image_tableName;
- [newUploadTaskArr addObject:curModel];
-
- }
-
- KWeakSelf
- if(newUploadTaskArr.count == 0){
- [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
- complete(Arr);
- [weakSelf handleUploadFileModelBg_idFun:Arr];
- }];
- }
- else{
- [uploadFileDataModel bg_saveOrUpdateArrayAsync:newUploadTaskArr complete:^(BOOL isSuccess) {
- [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
- complete(Arr);
- [weakSelf handleUploadFileModelBg_idFun:Arr];
- }];
- }];
- }
- }
- #pragma mark 处理当前的model 加上bg_id
- - (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
- {
- if(!totalArr || totalArr.count != 3){
- return;
- }
-
- _fileModelDataArr = [NSMutableArray arrayWithArray:totalArr[0]];
-
- [self beginUploadFileFun];
- }
- - (void)beginUploadFileFun
- {
- // if(_isUploadIngType && _reUploadIngSelectIndex <= 0){
- // return;
- // }
-
- BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
- if(!isCanUseCellular){//不允许流量上传
- //
- if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
- mainBlock(^{
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendAllNotification object:nil];
- [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
- });
- return;
- }
-
- }
-
- if(ksharedAppDelegate.DisabledFileTransferType){
- if(ksharedAppDelegate.isImageNewFor130){
- [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
- }
- else{
- [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
- }
- return;
- }
-
- if(_isSuspendType)
- {
- return;
- }
-
- _isUploadIngType = YES;
-
-
- _curUploadFileDataModel.imageData = nil;
- _curUploadFileDataModel.videoData = nil;
- _curUploadFileDataModel = nil;
-
- if(_fileModelDataArr.count == 0){
- [self didUploadAllTaskDoneFun];
- return;
- }
-
- if(_reUploadIngSelectIndex > 0 && _reUploadIngSelectIndex < _fileModelDataArr.count){
- _curUploadFileDataModel = _fileModelDataArr[_reUploadIngSelectIndex];
- }
- else
- {
- _curUploadFileDataModel = _fileModelDataArr.firstObject;
- //_curUploadFileDataModel = _fileModelDataArr.lastObject;
- }
-
- //做个异常处理
- if(_databaseArr.count == 3 && _isNewAddTaskType){
- NSMutableArray *uploadIngArr = _databaseArr[0];
- if(_fileModelDataArr.count != uploadIngArr.count){
- _databaseArr[0] = [NSMutableArray arrayWithArray:_fileModelDataArr];
- }
- }
-
-
- if(!_curUploadFileDataModel){
- return;
- }
-
-
- if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage){
- NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:_curUploadFileDataModel.curUploadFileType];
- _curUploadFileDataModel.imageData = [NSData dataWithContentsOfFile:pathStr];
-
- if(_curUploadFileDataModel.imageData && _curUploadFileDataModel.imageData.length >0){
- if(_curUploadFileDataModel.totalBytes == 0){
- _curUploadFileDataModel.totalBytes = _curUploadFileDataModel.imageData.length;
- }
-
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
- return;
- }
-
- }
- else{
- //NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
-
- if([cachesFileManager checkFileIsSaveState:_curUploadFileDataModel.filename withType:uploadFileTypeVideo]){
-
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
- return;
- }
- }
-
-
- if(!_curUploadFileDataModel.asset){
- NSString *curLocalIdentifier = _curUploadFileDataModel.localIdentifier;
- PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
- PHAsset *asset = fetchResult.firstObject;
- _curUploadFileDataModel.asset = asset;
- }
-
- if(!_curUploadFileDataModel.asset){
- [self getDataWrongToChangeFailFun];
- return;
- }
-
- KWeakSelf
- if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage)
- {
- if(!_curUploadFileDataModel.imageData || _curUploadFileDataModel.imageData.length == 0){
- [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
- // 直接得到最终的 NSData 数据
- if (imageData) {
- self->_curUploadFileDataModel.imageData = imageData;
- [weakSelf afterGetImageDataFun];
- }
- else{
- [weakSelf getDataWrongToChangeFailFun];
- }
- }];
- }
- }
- else if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
-
- //第一帧图片
- [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
- // 直接得到最终的 NSData 数据
- if (imageData) {
- self->_curUploadFileDataModel.imageData = imageData;
- [weakSelf afterGetImageDataInVideoFun];
- }
- }];
- //真正的视频数据
- PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
- options.version = PHVideoRequestOptionsVersionOriginal;
- [[PHImageManager defaultManager] requestAVAssetForVideo:_curUploadFileDataModel.asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
- if ([asset isKindOfClass:[AVURLAsset class]]) {
-
- AVURLAsset* urlAsset = (AVURLAsset*)asset;
-
- BOOL isSuc = [cachesFileManager copyVideoItemAtPath:[urlAsset.URL path] fileName:self->_curUploadFileDataModel.filename error:nil];
-
- //NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
-
- if (isSuc) {
- //self->_curUploadFileDataModel.videoData = videoData;
- [weakSelf afterGetVideoDataFun];
- }
- else{
- [weakSelf getDataWrongToChangeFailFun];
- }
- }
- else{
- [weakSelf getDataWrongToChangeFailFun];
- }
- }];
- }
- }
- #pragma mark 获取数据失败 报错
- - (void)getDataWrongToChangeFailFun
- {
- [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
- }];
- }
- #pragma mark 根据 asset 获取到图片数据
- - (void)afterGetImageDataFun
- {
- [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.filename type:uploadFileTypeImage];
-
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
- }
- - (void)afterGetImageDataInVideoFun
- {
- [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.videoFirstImageName type:uploadFileTypeImage];
- }
- - (void)afterGetVideoDataFun
- {
- [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.videoData fileName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
-
- _curUploadFileDataModel.videoData = nil;
-
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
- }
- //修改文件上传的状态
- - (void)changeUploadFileState:(uploadStateType)curUploadStateType withDidUploadBytes:(long)didUpLoadBytes withModel:(uploadFileDataModel*)model complete:(custom_complete_B)complete
- {
- if(model.bg_id.integerValue != _curUploadFileDataModel.bg_id.integerValue){
- complete(YES);
- return;
- }
-
- HLog(@"上传任务列表:%@",_fileModelDataArr);
-
- self.taskRenewTime = [iTools getNowTimeStamp];
-
- _curUploadFileDataModel.curUploadStateType = curUploadStateType;
- _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
-
- if(curUploadStateType == uploadStateDone){
-
- NSString *uploadDefaultPath = [HWDataManager getStringWithKey:stringKeyAddSn(Const_photo_upload_default_path)];
-
- if([uploadDefaultPath containsString:@"mnt/media_rw"]){
- uploadDefaultPath = [uploadDefaultPath stringByReplacingOccurrencesOfString:@"mnt/media_rw" withString:NSLocalizedString(@"disk_Extra_default_tip",nil)];
- }
- else if([uploadDefaultPath containsString:@"storage/emulated/0"]){
- uploadDefaultPath = [uploadDefaultPath stringByReplacingOccurrencesOfString:@"storage/emulated/0" withString:NSLocalizedString(@"disk_phone_default_tip",nil)];
- }
- else if([uploadDefaultPath containsString:@"sdcard"]){
- uploadDefaultPath = [uploadDefaultPath stringByReplacingOccurrencesOfString:@"sdcard" withString:NSLocalizedString(@"disk_phone_default_tip",nil)];
- }
-
- _curUploadFileDataModel.savePath = uploadDefaultPath;
-
- _curUploadFileDataModel.videoData = [NSData new];
-
- if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
- [cachesFileManager removeItemAtPath:_curUploadFileDataModel.filename type:uploadFileTypeVideo error:nil];
- }
-
- [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
- [self handleDatabaseArrDeteleInUploading:_curUploadFileDataModel];
- [self handleDatabaseArrByInDone:_curUploadFileDataModel];
- }
- else if(curUploadStateType == uploadStateFail){
- [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
- [self handleDatabaseArrDeteleInUploading:_curUploadFileDataModel];
- [self handleDatabaseArrByInFail:_curUploadFileDataModel];
- }
- [_curUploadFileDataModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
- //HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
- }];
-
-
- if(!_isSuspendType || curUploadStateType != uploadStateSuspend){
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
- }
-
- complete(YES);
-
- return;
- // NSString* where = nil;
- //
- // where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
- // //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
- //
- // KWeakSelf
- // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
- // [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName where:where complete:^(NSArray * _Nullable array) {
- // for (uploadFileDataModel * curModel in array) {
- // curModel.curUploadStateType = curUploadStateType;
- // curModel.didUploadBytes = didUpLoadBytes;
- //
- // if(curUploadStateType == uploadStateDone){
- // curModel.videoData = [NSData new];
- //
- // if(curModel.curUploadFileType == uploadFileTypeVideo){
- // [cachesFileManager removeItemAtPath:curModel.filename type:uploadFileTypeVideo error:nil];
- // }
- //
- // [weakSelf handleDatabaseArrByDelete:curModel];
- // [weakSelf handleDatabaseArrByInDone:curModel];
- // }
- // else if(curUploadStateType == uploadStateFail){
- // [weakSelf handleDatabaseArrByDelete:curModel];
- // [weakSelf handleDatabaseArrByInFail:curModel];
- // }
- //
- // [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
- // //HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
- // }];
- //
- // }
- //
- // complete(YES);
- // }];
- // });
- //
- }
- //修改文件上传的状态
- - (void)changeUploadState:(uploadStateType)curUploadStateType withModelArr:(NSArray*)modelArr complete:(custom_complete_B)complete
- {
-
- for (uploadFileDataModel * model in modelArr) {
- model.curUploadStateType = curUploadStateType;
- }
-
- if(modelArr.count == 0){
- return;
- }
-
- [uploadFileDataModel bg_saveOrUpdateArrayAsync:modelArr complete:^(BOOL isSuccess) {
- complete(YES);
- }];
-
- }
- - (void)handleDatabaseArrByDelete:(uploadFileDataModel*)delModel
- {
- if(_databaseArr && _databaseArr.count == 3 ){
-
- NSMutableArray *uploadingArr = nil;
-
- if(delModel.curUploadStateType == uploadStateUploading
- ||delModel.curUploadStateType == uploadStateSuspend){
- uploadingArr = _databaseArr[0];
- }
- else if(delModel.curUploadStateType == uploadStateDone)
- {
- uploadingArr = _databaseArr[1];
- }
- else{
- uploadingArr = _databaseArr[2];
- }
-
- [uploadingArr removeObject:delModel];
- }
- }
- - (void)handleDatabaseArrDeteleInUploading:(uploadFileDataModel*)uploadmodel
- {
- NSLock *lock = [NSLock new];
- [lock lock];
- if(!uploadmodel){
- [lock unlock];
- return;
- }
-
- if(_databaseArr && _databaseArr.count == 3 ){
- NSMutableArray *uploadingArr = _databaseArr[0];
- NSInteger count = uploadingArr.count;
- [uploadingArr removeObject:uploadmodel];
-
- NSInteger atferDelCount = uploadingArr.count;
-
- if(count == atferDelCount){
- for (uploadFileDataModel*model in uploadingArr) {
- if(model.bg_id.integerValue == uploadmodel.bg_id.integerValue){
- [uploadingArr removeObject:model];
- break;
- }
- }
- }
-
- NSInteger atferDelCount2 = uploadingArr.count;
-
- if(count == atferDelCount2){
- //HLog(@"\n\n\nerror:上传中的任务没有删除掉database\n\n\n\n");
- }
- }
-
- [lock unlock];
- }
- - (void)handleDatabaseArrByInDone:(uploadFileDataModel*)uploadmodel
- {
- NSLock *lock = [NSLock new];
- [lock lock];
- if(_databaseArr && _databaseArr.count == 3 ){
- NSMutableArray *uploadDoneArr = _databaseArr[1];
- if(uploadmodel){
- [uploadDoneArr insertObject:uploadmodel atIndex:0];
- }
- }
- [lock unlock];
- }
- - (void)handleDatabaseArrByInFail:(uploadFileDataModel*)uploadmodel
- {
- NSLock *lock = [NSLock new];
- [lock lock];
- if(_databaseArr && _databaseArr.count == 3 ){
- NSMutableArray *uploadFailArr = _databaseArr[2];
- if(uploadmodel){
- [uploadFailArr insertObject:uploadmodel atIndex:0];
- }
- }
- [lock unlock];
- }
- - (void)handleDatabaseArrAddModelInUploading:(uploadFileDataModel*)uploadmodel
- {
- NSLock *lock = [NSLock new];
- [lock lock];
- if(_databaseArr && _databaseArr.count == 3 ){
- NSMutableArray *uploadlingArr = _databaseArr[0];
- if(uploadmodel){
- [uploadlingArr insertObject:uploadmodel atIndex:0];
- }
- }
- [lock unlock];
- }
- //暂停上传完成
- - (void)suspendUploadFileFun:(BOOL)isSuspendAll withModel:(uploadFileDataModel*)model
- {
-
- if(!_fileModelDataArr || !_curUploadFileDataModel){
- return;
- }
-
- if(isSuspendAll){
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
-
- for (uploadFileDataModel*model in _fileModelDataArr) {
- model.curUploadStateType = uploadStateSuspend;
- }
- _isSuspendType = YES;
- _isUploadIngType = NO;
-
- [self changeUploadState:uploadStateSuspend withModelArr:_fileModelDataArr complete:^(BOOL isSuccess) {
- [self->_fileModelDataArr removeAllObjects];
- }];
- }
- else{
- uploadFileDataModel *curModel = _curUploadFileDataModel;
- if(model){
- curModel = model;
- }
- else{
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
- }
- curModel.curUploadStateType = uploadStateSuspend;
- [self changeUploadState:uploadStateSuspend withModelArr:@[curModel] complete:^(BOOL isSuccess) {
- [self->_fileModelDataArr removeObject:model];
-
- if(curModel.bg_id.integerValue == self->_curUploadFileDataModel.bg_id.integerValue){
- //开启下一个任务
- [self beginUploadFileFun];
- }
-
- }];
-
- }
-
- }
- //某个文件重新上传
- - (void)reUploadFileFunBy:(NSMutableArray*)Arr withAll:(BOOL)isAllType
- {
- if(!_fileModelDataArr){
- _fileModelDataArr = [NSMutableArray new];
- }
-
- _isSuspendType = NO;
-
- // for (uploadFileDataModel*model in Arr) {
- // model.curUploadStateType = uploadStateUploading;
- // }
-
- [self changeUploadState:uploadStateUploading withModelArr:Arr complete:^(BOOL isSuccess) {
- [self reUploadFileSecondBy:Arr withAll:isAllType];
- }];
- }
- - (void)reUploadFileSecondBy:(NSMutableArray*)Arr withAll:(BOOL)isAllType{
- if(isAllType)
- {
- _isNewAddTaskType = YES;
- _fileModelDataArr = [NSMutableArray arrayWithArray:Arr];
- _reUploadIngSelectIndex = -1;
- if(_fileModelDataArr.count >0){
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:_fileModelDataArr.firstObject];
- }
-
- [self beginUploadFileFun];
- return;
- }
-
- _isNewAddTaskType = NO;
- _reUploadIngSelectIndex = 0;
-
- NSMutableArray *curArr = [NSMutableArray arrayWithArray:Arr];
- //_fileModelDataArr = curArr;
-
- for (uploadFileDataModel*addModel in curArr){
- [_fileModelDataArr insertObject:addModel atIndex:0];
- }
-
- [self beginUploadFileFun];
- }
- - (void)uploadFileDoneFun
- {
- long totalSizeByte = _curUploadFileDataModel.totalBytes;
-
- [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
- self->_isUploadIngType = NO;
- if(self->_fileModelDataArr.count > 0){
- [self beginUploadFileFun];
- }
- else{
- [self didUploadAllTaskDoneFun];
- }
- }];
- }
- //文件上传失败
- - (void)uploadFileFailFun
- {
- [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
-
- if(self->_fileModelDataArr.count > 0){
- [self beginUploadFileFun];
- }
- else{
- [self didUploadAllTaskDoneFun];
- }
- }];
-
- mainBlock(^{
- //[[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
- });
- }
- //删除本地数据库记录
- - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache complete:(custom_complete_B)complete
- {
- //逻辑待优化
- BOOL isSuc = NO;
-
- BOOL isDelUploadingModel = NO;
-
- NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
- for (uploadFileDataModel *uploadFileDataMod in curDelArr) {
-
- NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
-
- NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
- [where appendString:curStr];
- isSuc = [uploadFileDataModel bg_delete:upLoadFile_image_tableName where:where];
-
- [self handleDatabaseArrByDelete:uploadFileDataMod];
- //删除本地图片
- if(isSuc && isDelCache){
- //判断是否可以删除本地缓存
- if(_databaseArr && _databaseArr.count == 3 ){
- NSMutableArray *uploadingArr = _databaseArr[0];
- NSMutableArray *uploadDoneArr = _databaseArr[1];
- NSMutableArray *uploadFailArr = _databaseArr[2];
-
-
- BOOL isNeedDel = YES;
- for (uploadFileDataModel *baseUploadFileDataMod in uploadingArr) {
- if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
- isNeedDel = NO;
- break;
- }
- }
-
- if(isNeedDel){
- for (uploadFileDataModel *baseUploadFileDataMod in uploadDoneArr) {
- if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
- isNeedDel = NO;
- break;
- }
- }
- }
-
- if(isNeedDel){
- for (uploadFileDataModel *baseUploadFileDataMod in uploadFailArr) {
- if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
- isNeedDel = NO;
- break;
- }
- }
- }
-
- if(isNeedDel){
- if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
- [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
-
- [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
- }
- else{
- [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
- }
- }
- }
- }
-
- if(isSuc && !isDelCache){//上传报云机已经存在文件了 查询本地是否有任务记录
- [self handleRetryUploadAndDelRecordFun:uploadFileDataMod];
- }
-
- //是否在删除上传中的任务
- if(_fileModelDataArr && _fileModelDataArr.count >0)
- {
- for (uploadFileDataModel *curUploadFileDataMod in _fileModelDataArr)
- {
- if(curUploadFileDataMod.bg_id.integerValue == uploadFileDataMod.bg_id.integerValue){
- [_fileModelDataArr removeObject:curUploadFileDataMod];
- //[self handleDatabaseArrDeteleInUploading:curUploadFileDataMod];
- break;
- }
- }
- }
-
- if(!isDelUploadingModel){
- if(_curUploadFileDataModel.bg_id.integerValue == uploadFileDataMod.bg_id.integerValue){
- isDelUploadingModel = YES;
-
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
- }
- }
- }
-
- if(isDelUploadingModel){
- _isUploadIngType = NO;
- if(_fileModelDataArr.count >=1){
- [self beginUploadFileFun];
- }
- else{
- _curUploadFileDataModel = nil;
-
- _isSuspendType = NO;
- //_curUploadFileDataModel.curUploadStateType = uploadStateDone;
- }
- }
-
- uploadFileDataModel *RefreshUploadFileDataMod = nil;
- if(delArr.count >= 1){
- RefreshUploadFileDataMod = delArr.firstObject;
- }
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:RefreshUploadFileDataMod];
-
- complete(YES);
-
- }
- #pragma mark 处理重复上传文件的问题
- - (void)handleRetryUploadAndDelRecordFun:(uploadFileDataModel *)uploadFileDataMod
- {
- NSLock *lock = [NSLock new];
- [lock lock];
-
- if(!_databaseArr && _databaseArr.count != 3){
- return;
- }
-
- NSMutableArray * doneArr = _databaseArr[1];
- NSMutableArray * failArr = _databaseArr[2];
-
- BOOL isNeedDelType = YES;
-
- for (uploadFileDataModel *doneModel in doneArr) {
- if([doneModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
- isNeedDelType = NO;
- break;
- }
- }
-
- if(isNeedDelType){
- for (uploadFileDataModel *failModel in failArr) {
- if([failModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
- isNeedDelType = NO;
- break;
- }
- }
- }
-
- if(isNeedDelType){
- if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
- [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
-
- [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
- }
- else{
- [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
- }
- }
- else{//视频文件是要删除的
- if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
- [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
- }
- }
-
- [lock unlock];
- }
- //检查是否需要重新上传
- - (void)checkReUploadFileFun
- {
- if(!_curUploadFileDataModel
- || _curUploadFileDataModel.curUploadStateType == uploadStateDone
- || _curUploadFileDataModel.curUploadStateType == uploadStateSuspend){
- return;
- }
-
- if(_isSuspendType){
- return;
- }
-
- NSInteger curTime = [iTools getNowTimeStamp];
-
- if(self.taskRenewTime == 0){
- return;
- }
-
- if(curTime - self.taskRenewTime < 10){
- return;
- }
-
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
- }
- - (BOOL)checkUploadingFun
- {
- if(!_curUploadFileDataModel){
- return NO;
- }
-
- if(_isSuspendType){
- return NO;
- }
-
- return YES;
- }
- - (void)checkHadUploadTaskWithComplete:(custom_complete_B)complete
- {
- [self getDataInDatabaseFun:NO complete:^(NSMutableArray * _Nonnull Arr) {
- mainBlock(^{
- if(!Arr || Arr.count != 3){
- complete(NO);
- }
- else{
- NSArray *firstArr = Arr[0];
- if(firstArr.count >0){
- complete(YES);
- }
- else{
- complete(NO);
- }
- }
- });
-
- }];
- }
- - (void)didUploadAllTaskDoneFun
- {
- self->_isUploadIngType = NO;
- self->_curUploadFileDataModel = nil;
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileAllTaskDoneNotification object:nil];
- }
- @end
|