uploadFileManager.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // uploadFileManager.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2023/11/15.
  6. //
  7. #import "uploadFileManager.h"
  8. @implementation uploadFileManager
  9. static uploadFileManager * cur_uploadFileShareInstance = nil;
  10. +(uploadFileManager *)shareInstance;
  11. {
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. cur_uploadFileShareInstance = [[uploadFileManager alloc] init];
  15. });
  16. return cur_uploadFileShareInstance;
  17. }
  18. - (id)init
  19. {
  20. self = [super init];
  21. if (self) {
  22. //[self initManager];
  23. }
  24. return self;
  25. }
  26. //把TZAssetModel 转成 我们需要上传的model
  27. - (NSMutableArray*)handlTZAssetModelToUploadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems
  28. {
  29. _fileModelDataArr = [NSMutableArray new];
  30. for (TZAssetModel * model in indexPathsForSelectedItems) {
  31. uploadFileDataModel * curModel = [uploadFileDataModel new];
  32. curModel.imageData = model.imageData;
  33. curModel.videoData = model.videoData;
  34. curModel.filename = [model.asset valueForKey:@"filename"];
  35. curModel.curUploadStateType = uploadStateWait;
  36. if(model.type == TZAssetModelMediaTypeVideo){
  37. curModel.curUploadFileType = uploadFileTypeVideo;
  38. [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
  39. curModel.totalBytes = [model.videoData length];
  40. NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
  41. curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
  42. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];
  43. }
  44. else{
  45. curModel.curUploadFileType = uploadFileTypeImage;
  46. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
  47. curModel.totalBytes = [model.imageData length];
  48. }
  49. [_fileModelDataArr addObject:curModel];
  50. //保存到数据库
  51. curModel.bg_tableName = upLoadFile_image_tableName;
  52. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  53. HLog(@"%@ 写入 %@", curModel.filename, isSuccess ? @"成功":@"失败");
  54. }];
  55. }
  56. KWeakSelf
  57. if(_fileModelDataArr.count > 0){
  58. //骚操作 重新查出来 拿到bg_id
  59. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:_fileModelDataArr.count orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  60. for (int i=0; i<array.count; i++) {
  61. uploadFileDataModel *bg_mod = array[i];
  62. for (uploadFileDataModel *data_mod in _fileModelDataArr) {
  63. if([data_mod.filename isEqualToString:bg_mod.filename]){
  64. data_mod.bg_id = bg_mod.bg_id;
  65. //HLog(@"bg_id:%@",data_mod.bg_id);
  66. break;
  67. }
  68. }
  69. }
  70. [weakSelf beginUploadFileFun];
  71. }];
  72. //[self beginUploadFileFun];
  73. }
  74. return _fileModelDataArr;
  75. }
  76. - (void)beginUploadFileFun
  77. {
  78. _isSuspendType = NO;
  79. _curUploadFileDataModel = _fileModelDataArr.firstObject;
  80. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  81. }
  82. //修改文件上传的状态
  83. - (void)changeUploadFileState:(uploadStateType)curUploadStateType withDidUploadBytes:(long)didUpLoadBytes withModel:(uploadFileDataModel*)model complete:(custom_complete_B)complete
  84. {
  85. if(model.bg_id.integerValue == _curUploadFileDataModel.bg_id.integerValue){
  86. _curUploadFileDataModel.curUploadStateType = curUploadStateType;
  87. _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
  88. }
  89. model.curUploadStateType = curUploadStateType;
  90. model.didUploadBytes = didUpLoadBytes;
  91. if(!_isSuspendType || curUploadStateType == uploadStateSuspend){
  92. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
  93. }
  94. if(curUploadStateType == uploadStateUploading){
  95. return;
  96. }
  97. //NSNumber * numberUploadState = nil;
  98. NSString* where = nil;
  99. // if(curUploadStateType == uploadStateDone)
  100. // {//只有上传中的才可能是完成的
  101. // numberUploadState = [NSNumber numberWithInt:uploadStateUploading];
  102. // where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  103. // }
  104. // else
  105. // {//查找非上传完成的
  106. // numberUploadState = [NSNumber numberWithInt:uploadStateDone];
  107. // where = [NSString stringWithFormat:@"where %@=%@ and %@!=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  108. // }
  109. where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
  110. //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
  111. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName where:where complete:^(NSArray * _Nullable array) {
  112. for (uploadFileDataModel * curModel in array) {
  113. curModel.curUploadStateType = curUploadStateType;
  114. curModel.didUploadBytes = didUpLoadBytes;
  115. if(curUploadStateType == uploadStateDone){
  116. curModel.videoData = [NSData new];
  117. HLog(@"");
  118. }
  119. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  120. HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  121. }];
  122. }
  123. complete(YES);
  124. }];
  125. }
  126. //暂停上传完成
  127. - (void)suspendUploadFileFun:(BOOL)isSuspendAll
  128. {
  129. // if(isSuspendAll){
  130. //
  131. // }
  132. if(!_fileModelDataArr || !_curUploadFileDataModel){
  133. return;
  134. }
  135. _isSuspendType = YES;
  136. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
  137. NSEnumerator *curArr = [_fileModelDataArr reverseObjectEnumerator];
  138. for (uploadFileDataModel*model in curArr) {
  139. HLog(@"111hxd 2 %@ ",_fileModelDataArr)
  140. [self changeUploadFileState:uploadStateSuspend withDidUploadBytes:model.didUploadBytes withModel:model complete:^(BOOL isSuccess) {
  141. }];
  142. }
  143. }
  144. //某个文件重新上传
  145. - (void)reUploadFileFunBy:(NSMutableArray*)Arr
  146. {
  147. _fileModelDataArr = Arr;
  148. [self beginUploadFileFun];
  149. }
  150. - (void)uploadFileDoneFun
  151. {
  152. long totalSizeByte = [_curUploadFileDataModel.imageData length];
  153. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
  154. totalSizeByte = [_curUploadFileDataModel.videoData length];
  155. }
  156. [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  157. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  158. if(self->_fileModelDataArr.count > 0){
  159. [self beginUploadFileFun];
  160. }
  161. }];
  162. }
  163. //删除本地数据库记录
  164. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr complete:(custom_complete_B)complete
  165. {
  166. //逻辑待优化
  167. BOOL isSuc = false;
  168. for (uploadFileDataModel *uploadFileDataMod in delArr) {
  169. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  170. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  171. [where appendString:curStr];
  172. isSuc = [uploadFileDataModel bg_delete:upLoadFile_image_tableName where:where];
  173. }
  174. complete(isSuc);
  175. }
  176. @end