uploadFileManager.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. _isUploadIngType = YES;
  80. _curUploadFileDataModel = _fileModelDataArr.firstObject;
  81. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  82. }
  83. //修改文件上传的状态
  84. - (void)changeUploadFileState:(uploadStateType)curUploadStateType withDidUploadBytes:(long)didUpLoadBytes withModel:(uploadFileDataModel*)model complete:(custom_complete_B)complete
  85. {
  86. if(model.bg_id.integerValue == _curUploadFileDataModel.bg_id.integerValue){
  87. _curUploadFileDataModel.curUploadStateType = curUploadStateType;
  88. _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
  89. }
  90. model.curUploadStateType = curUploadStateType;
  91. model.didUploadBytes = didUpLoadBytes;
  92. if(!_isSuspendType || curUploadStateType == uploadStateSuspend){
  93. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
  94. }
  95. if(curUploadStateType == uploadStateUploading){
  96. return;
  97. }
  98. //NSNumber * numberUploadState = nil;
  99. NSString* where = nil;
  100. // if(curUploadStateType == uploadStateDone)
  101. // {//只有上传中的才可能是完成的
  102. // numberUploadState = [NSNumber numberWithInt:uploadStateUploading];
  103. // where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  104. // }
  105. // else
  106. // {//查找非上传完成的
  107. // numberUploadState = [NSNumber numberWithInt:uploadStateDone];
  108. // where = [NSString stringWithFormat:@"where %@=%@ and %@!=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  109. // }
  110. where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
  111. //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
  112. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName where:where complete:^(NSArray * _Nullable array) {
  113. for (uploadFileDataModel * curModel in array) {
  114. curModel.curUploadStateType = curUploadStateType;
  115. curModel.didUploadBytes = didUpLoadBytes;
  116. if(curUploadStateType == uploadStateDone){
  117. curModel.videoData = [NSData new];
  118. HLog(@"");
  119. }
  120. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  121. HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  122. }];
  123. }
  124. complete(YES);
  125. }];
  126. }
  127. //暂停上传完成
  128. - (void)suspendUploadFileFun:(BOOL)isSuspendAll
  129. {
  130. // if(isSuspendAll){
  131. //
  132. // }
  133. if(!_fileModelDataArr || !_curUploadFileDataModel){
  134. return;
  135. }
  136. _isSuspendType = YES;
  137. _isUploadIngType = NO;
  138. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
  139. NSEnumerator *curArr = [_fileModelDataArr reverseObjectEnumerator];
  140. for (uploadFileDataModel*model in curArr) {
  141. HLog(@"111hxd 2 %@ ",_fileModelDataArr)
  142. [self changeUploadFileState:uploadStateSuspend withDidUploadBytes:model.didUploadBytes withModel:model complete:^(BOOL isSuccess) {
  143. }];
  144. }
  145. }
  146. //某个文件重新上传
  147. - (void)reUploadFileFunBy:(NSMutableArray*)Arr
  148. {
  149. _fileModelDataArr = Arr;
  150. [self beginUploadFileFun];
  151. }
  152. - (void)uploadFileDoneFun
  153. {
  154. long totalSizeByte = [_curUploadFileDataModel.imageData length];
  155. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
  156. totalSizeByte = [_curUploadFileDataModel.videoData length];
  157. }
  158. [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  159. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  160. if(self->_fileModelDataArr.count > 0){
  161. [self beginUploadFileFun];
  162. }
  163. else{
  164. self->_isUploadIngType = NO;
  165. }
  166. }];
  167. }
  168. //删除本地数据库记录
  169. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr complete:(custom_complete_B)complete
  170. {
  171. //逻辑待优化
  172. BOOL isSuc = false;
  173. for (uploadFileDataModel *uploadFileDataMod in delArr) {
  174. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  175. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  176. [where appendString:curStr];
  177. isSuc = [uploadFileDataModel bg_delete:upLoadFile_image_tableName where:where];
  178. }
  179. complete(isSuc);
  180. //继续下一个
  181. [_fileModelDataArr removeObject:_curUploadFileDataModel];
  182. [self beginUploadFileFun];
  183. }
  184. @end