123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- //
- // uploadFileManager.m
- // 隐私保护
- //
- // Created by xd h on 2023/11/15.
- //
- #import "uploadFileManager.h"
- @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;
- }
-
- _databaseArr = [NSMutableArray new];
-
- KWeakSelf
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
- [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){
- NSString*pathStr = [cachesFileManager getFilePathWithName:curModel.filename type:uploadFileTypeImage];
- curModel.imageData = [NSData dataWithContentsOfFile:pathStr];
- }
- else if(curModel.curUploadFileType == uploadFileTypeVideo){
- NSString*pathStr = [cachesFileManager getFilePathWithName:curModel.videoFirstImageName type:uploadFileTypeImage];
- curModel.imageData = [NSData dataWithContentsOfFile:pathStr];
-
- if(curModel.curUploadStateType != uploadStateDone){
- NSString*videoPathStr = [cachesFileManager getFilePathWithName:curModel.filename type:uploadFileTypeVideo];
- //curModel.videoData = [NSData dataWithContentsOfFile:videoPathStr];
- }
- }
-
- if(curModel.curUploadStateType == uploadStateFail){
- [failArr addObject:curModel];
- }
- else if(curModel.curUploadStateType == uploadStateDone){
- [doneArr addObject:curModel];
- }
- else{
- [otherArr addObject:curModel];
- }
- }
-
- [self->_databaseArr addObject:otherArr];
- [self->_databaseArr addObject:doneArr];
- [self->_databaseArr addObject:failArr];
-
- complete(self->_databaseArr);
- }];
- });
- }
- //把TZAssetModel 转成 我们需要上传的model
- - (void)handlTZAssetModelToUploadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
- {
-
- if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
- return;
- }
-
- if(!_fileModelDataArr){
- _fileModelDataArr = [NSMutableArray new];
- }
-
- self.curUploadModelNumbers = indexPathsForSelectedItems.count;
-
- for (TZAssetModel * model in indexPathsForSelectedItems) {
- uploadFileDataModel * curModel = [uploadFileDataModel new];
- curModel.imageData = model.imageData;
- curModel.videoData = model.videoData;
- curModel.filename = [model.asset valueForKey:@"filename"];
- curModel.curUploadStateType = uploadStateWait;
-
- if(model.type == TZAssetModelMediaTypeVideo){
- curModel.curUploadFileType = uploadFileTypeVideo;
- [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
-
-
- curModel.totalBytes = [model.videoData length];
- curModel.videoData = [NSData new];//视频文件存储到文件后内存清空
-
- NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
- curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
- [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];
- }
- else{
- curModel.curUploadFileType = uploadFileTypeImage;
- [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
- curModel.totalBytes = [model.imageData length];
- }
-
-
- //[_fileModelDataArr addObject:curModel];
-
- //保存到数据库
- curModel.bg_tableName = upLoadFile_image_tableName;
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
- [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
- HLog(@"%@ 写入 %@", curModel.filename, isSuccess ? @"成功":@"失败");
- }];
- });
-
- }
-
- KWeakSelf
- //if(_fileModelDataArr.count > 0){
-
- [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
- complete(Arr);
- [weakSelf handleUploadFileModelBg_idFun:Arr];
- }];
-
- //骚操作 重新查出来 拿到bg_id
- // [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:_fileModelDataArr.count orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
- //
- // for (int i=0; i<array.count; i++) {
- // uploadFileDataModel *bg_mod = array[i];
- //
- // for (uploadFileDataModel *data_mod in self->_fileModelDataArr) {
- // if([data_mod.filename isEqualToString:bg_mod.filename]){
- // data_mod.bg_id = bg_mod.bg_id;
- // //HLog(@"bg_id:%@",data_mod.bg_id);
- // break;
- // }
- // }
- // }
- //
- // [weakSelf beginUploadFileFun];
- // }];
-
- //}
- }
- #pragma mark 处理当前的model 加上bg_id
- - (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
- {
- if(!totalArr || totalArr.count != 3){
- return;
- }
-
- NSMutableArray *curArr = totalArr[0];
-
- for (int i=0; i<_curUploadModelNumbers; i++) {
- uploadFileDataModel *bg_mod = curArr[i];
- [_fileModelDataArr addObject:bg_mod];
- }
-
- // for (int i=0; i<_fileModelDataArr.count; i++) {
- // uploadFileDataModel *bg_mod = _fileModelDataArr[i];
- //
- // for (uploadFileDataModel *data_mod in curArr) {
- // if([data_mod.filename isEqualToString:bg_mod.filename]){
- // bg_mod.bg_id = data_mod.bg_id;
- // //HLog(@"bg_id:%@",data_mod.bg_id);
- // break;
- // }
- // }
- // }
-
- [self beginUploadFileFun];
- }
- - (void)beginUploadFileFun
- {
- if(_isUploadIngType){
- return;
- }
-
- _isSuspendType = NO;
- _isUploadIngType = YES;
-
- _curUploadFileDataModel = _fileModelDataArr.firstObject;
-
- if(!_curUploadFileDataModel){
- return;
- }
-
- [[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){
- _curUploadFileDataModel.curUploadStateType = curUploadStateType;
- _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
- }
- model.curUploadStateType = curUploadStateType;
- model.didUploadBytes = didUpLoadBytes;
-
-
- if(!_isSuspendType || curUploadStateType == uploadStateSuspend){
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
- }
-
- if(curUploadStateType == uploadStateUploading){
- return;
- }
-
-
- //NSNumber * numberUploadState = nil;
- NSString* where = nil;
-
- // if(curUploadStateType == uploadStateDone)
- // {//只有上传中的才可能是完成的
- // numberUploadState = [NSNumber numberWithInt:uploadStateUploading];
- // where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
- // }
- // else
- // {//查找非上传完成的
- // numberUploadState = [NSNumber numberWithInt:uploadStateDone];
- // where = [NSString stringWithFormat:@"where %@=%@ and %@!=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
- // }
-
- where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
- //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
-
- [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];
- }
-
- }
- else if(curUploadStateType == uploadStateFail){
-
- }
-
- [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
- HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
- }];
-
- }
-
- complete(YES);
- }];
-
- }
- //暂停上传完成
- - (void)suspendUploadFileFun:(BOOL)isSuspendAll
- {
- // if(isSuspendAll){
- //
- // }
-
- if(!_fileModelDataArr || !_curUploadFileDataModel){
- return;
- }
-
- _isSuspendType = YES;
- _isUploadIngType = NO;
-
- [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
-
- NSEnumerator *curArr = [_fileModelDataArr reverseObjectEnumerator];
- for (uploadFileDataModel*model in curArr) {
- HLog(@"111hxd 2 %@ ",_fileModelDataArr)
- [self changeUploadFileState:uploadStateSuspend withDidUploadBytes:model.didUploadBytes withModel:model complete:^(BOOL isSuccess) {
-
- }];
- }
-
- }
- //某个文件重新上传
- - (void)reUploadFileFunBy:(NSMutableArray*)Arr
- {
- if(!_fileModelDataArr){
- _fileModelDataArr = [NSMutableArray new];
- }
- [_fileModelDataArr addObjectsFromArray:Arr];
- [self beginUploadFileFun];
- }
- - (void)uploadFileDoneFun
- {
- long totalSizeByte = _curUploadFileDataModel.totalBytes;
-
- [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
- [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
-
- self->_isUploadIngType = NO;
- if(self->_fileModelDataArr.count > 0){
- [self beginUploadFileFun];
- }
- }];
- }
- //文件上传失败
- - (void)uploadFileFailFun
- {
- [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
- [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
-
- if(self->_fileModelDataArr.count > 0){
- [self beginUploadFileFun];
- }
- else{
- self->_isUploadIngType = NO;
- }
- }];
-
- mainBlock(^{
- [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
- });
- }
- //删除本地数据库记录
- - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr complete:(custom_complete_B)complete
- {
- //逻辑待优化
- BOOL isSuc = false;
- for (uploadFileDataModel *uploadFileDataMod in delArr) {
-
- 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];
-
- //删除本地图片
- if(isSuc){
- 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];
- }
-
- }
- }
-
- complete(isSuc);
-
- //继续下一个
- if(_fileModelDataArr){
- [_fileModelDataArr removeObject:_curUploadFileDataModel];
- _isUploadIngType = NO;
- if(_fileModelDataArr.count >=1){
- [self beginUploadFileFun];
- }
- }
-
- }
- @end
|