uploadFileManager.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #pragma mark 读取数据库数据
  27. - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
  28. {
  29. if(_databaseArr && _databaseArr.count == 3 && !isReGet){
  30. complete(_databaseArr);
  31. return;
  32. }
  33. _databaseArr = [NSMutableArray new];
  34. KWeakSelf
  35. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  36. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:0 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  37. NSMutableArray *failArr = [NSMutableArray new];
  38. NSMutableArray *doneArr = [NSMutableArray new];
  39. NSMutableArray *otherArr = [NSMutableArray new];
  40. for (uploadFileDataModel * curModel in array) {
  41. //图片 和视频 还原
  42. if(curModel.curUploadFileType == uploadFileTypeImage){
  43. NSString*pathStr = [cachesFileManager getFilePathWithName:curModel.filename type:uploadFileTypeImage];
  44. curModel.imageData = [NSData dataWithContentsOfFile:pathStr];
  45. }
  46. else if(curModel.curUploadFileType == uploadFileTypeVideo){
  47. NSString*pathStr = [cachesFileManager getFilePathWithName:curModel.videoFirstImageName type:uploadFileTypeImage];
  48. curModel.imageData = [NSData dataWithContentsOfFile:pathStr];
  49. if(curModel.curUploadStateType != uploadStateDone){
  50. NSString*videoPathStr = [cachesFileManager getFilePathWithName:curModel.filename type:uploadFileTypeVideo];
  51. curModel.videoData = [NSData dataWithContentsOfFile:videoPathStr];
  52. }
  53. }
  54. if(curModel.curUploadStateType == uploadStateFail){
  55. [failArr addObject:curModel];
  56. }
  57. else if(curModel.curUploadStateType == uploadStateDone){
  58. [doneArr addObject:curModel];
  59. }
  60. else{
  61. [otherArr addObject:curModel];
  62. }
  63. }
  64. [self->_databaseArr addObject:otherArr];
  65. [self->_databaseArr addObject:doneArr];
  66. [self->_databaseArr addObject:failArr];
  67. complete(self->_databaseArr);
  68. }];
  69. });
  70. }
  71. //把TZAssetModel 转成 我们需要上传的model
  72. - (void)handlTZAssetModelToUploadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
  73. {
  74. _fileModelDataArr = [NSMutableArray new];
  75. for (TZAssetModel * model in indexPathsForSelectedItems) {
  76. uploadFileDataModel * curModel = [uploadFileDataModel new];
  77. curModel.imageData = model.imageData;
  78. curModel.videoData = model.videoData;
  79. curModel.filename = [model.asset valueForKey:@"filename"];
  80. curModel.curUploadStateType = uploadStateWait;
  81. if(model.type == TZAssetModelMediaTypeVideo){
  82. curModel.curUploadFileType = uploadFileTypeVideo;
  83. [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
  84. curModel.totalBytes = [model.videoData length];
  85. NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
  86. curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
  87. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];
  88. }
  89. else{
  90. curModel.curUploadFileType = uploadFileTypeImage;
  91. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
  92. curModel.totalBytes = [model.imageData length];
  93. }
  94. [_fileModelDataArr addObject:curModel];
  95. //保存到数据库
  96. curModel.bg_tableName = upLoadFile_image_tableName;
  97. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  98. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  99. HLog(@"%@ 写入 %@", curModel.filename, isSuccess ? @"成功":@"失败");
  100. }];
  101. });
  102. }
  103. KWeakSelf
  104. if(_fileModelDataArr.count > 0){
  105. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  106. complete(Arr);
  107. [weakSelf handleUploadFileModelBg_idFun:Arr];
  108. }];
  109. //骚操作 重新查出来 拿到bg_id
  110. // [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:_fileModelDataArr.count orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  111. //
  112. // for (int i=0; i<array.count; i++) {
  113. // uploadFileDataModel *bg_mod = array[i];
  114. //
  115. // for (uploadFileDataModel *data_mod in self->_fileModelDataArr) {
  116. // if([data_mod.filename isEqualToString:bg_mod.filename]){
  117. // data_mod.bg_id = bg_mod.bg_id;
  118. // //HLog(@"bg_id:%@",data_mod.bg_id);
  119. // break;
  120. // }
  121. // }
  122. // }
  123. //
  124. // [weakSelf beginUploadFileFun];
  125. // }];
  126. }
  127. }
  128. #pragma mark 处理当前的model 加上bg_id
  129. - (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
  130. {
  131. if(!totalArr || totalArr.count != 3){
  132. return;
  133. }
  134. NSMutableArray *curArr = totalArr[0];
  135. NSArray *UploadArr = [curArr subarrayWithRange:NSMakeRange(0, _fileModelDataArr.count-1)];
  136. [_fileModelDataArr removeAllObjects];
  137. [_fileModelDataArr addObjectsFromArray:UploadArr];
  138. [self beginUploadFileFun];
  139. }
  140. - (void)beginUploadFileFun
  141. {
  142. _isSuspendType = NO;
  143. _isUploadIngType = YES;
  144. _curUploadFileDataModel = _fileModelDataArr.firstObject;
  145. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  146. }
  147. //修改文件上传的状态
  148. - (void)changeUploadFileState:(uploadStateType)curUploadStateType withDidUploadBytes:(long)didUpLoadBytes withModel:(uploadFileDataModel*)model complete:(custom_complete_B)complete
  149. {
  150. if(model.bg_id.integerValue == _curUploadFileDataModel.bg_id.integerValue){
  151. _curUploadFileDataModel.curUploadStateType = curUploadStateType;
  152. _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
  153. }
  154. model.curUploadStateType = curUploadStateType;
  155. model.didUploadBytes = didUpLoadBytes;
  156. if(!_isSuspendType || curUploadStateType == uploadStateSuspend){
  157. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
  158. }
  159. if(curUploadStateType == uploadStateUploading){
  160. return;
  161. }
  162. //NSNumber * numberUploadState = nil;
  163. NSString* where = nil;
  164. // if(curUploadStateType == uploadStateDone)
  165. // {//只有上传中的才可能是完成的
  166. // numberUploadState = [NSNumber numberWithInt:uploadStateUploading];
  167. // where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  168. // }
  169. // else
  170. // {//查找非上传完成的
  171. // numberUploadState = [NSNumber numberWithInt:uploadStateDone];
  172. // where = [NSString stringWithFormat:@"where %@=%@ and %@!=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  173. // }
  174. where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
  175. //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
  176. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName where:where complete:^(NSArray * _Nullable array) {
  177. for (uploadFileDataModel * curModel in array) {
  178. curModel.curUploadStateType = curUploadStateType;
  179. curModel.didUploadBytes = didUpLoadBytes;
  180. if(curUploadStateType == uploadStateDone){
  181. curModel.videoData = [NSData new];
  182. HLog(@"");
  183. }
  184. else if(curUploadStateType == uploadStateFail){
  185. }
  186. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  187. HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  188. }];
  189. }
  190. complete(YES);
  191. }];
  192. }
  193. //暂停上传完成
  194. - (void)suspendUploadFileFun:(BOOL)isSuspendAll
  195. {
  196. // if(isSuspendAll){
  197. //
  198. // }
  199. if(!_fileModelDataArr || !_curUploadFileDataModel){
  200. return;
  201. }
  202. _isSuspendType = YES;
  203. _isUploadIngType = NO;
  204. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
  205. NSEnumerator *curArr = [_fileModelDataArr reverseObjectEnumerator];
  206. for (uploadFileDataModel*model in curArr) {
  207. HLog(@"111hxd 2 %@ ",_fileModelDataArr)
  208. [self changeUploadFileState:uploadStateSuspend withDidUploadBytes:model.didUploadBytes withModel:model complete:^(BOOL isSuccess) {
  209. }];
  210. }
  211. }
  212. //某个文件重新上传
  213. - (void)reUploadFileFunBy:(NSMutableArray*)Arr
  214. {
  215. if(!_fileModelDataArr){
  216. _fileModelDataArr = [NSMutableArray new];
  217. }
  218. [_fileModelDataArr addObjectsFromArray:Arr];
  219. [self beginUploadFileFun];
  220. }
  221. - (void)uploadFileDoneFun
  222. {
  223. long totalSizeByte = [_curUploadFileDataModel.imageData length];
  224. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
  225. totalSizeByte = [_curUploadFileDataModel.videoData length];
  226. }
  227. [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  228. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  229. if(self->_fileModelDataArr.count > 0){
  230. [self beginUploadFileFun];
  231. }
  232. else{
  233. self->_isUploadIngType = NO;
  234. }
  235. }];
  236. }
  237. //文件上传失败
  238. - (void)uploadFileFailFun
  239. {
  240. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  241. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  242. if(self->_fileModelDataArr.count > 0){
  243. [self beginUploadFileFun];
  244. }
  245. else{
  246. self->_isUploadIngType = NO;
  247. }
  248. }];
  249. mainBlock(^{
  250. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  251. });
  252. }
  253. //删除本地数据库记录
  254. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr complete:(custom_complete_B)complete
  255. {
  256. //逻辑待优化
  257. BOOL isSuc = false;
  258. for (uploadFileDataModel *uploadFileDataMod in delArr) {
  259. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  260. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  261. [where appendString:curStr];
  262. isSuc = [uploadFileDataModel bg_delete:upLoadFile_image_tableName where:where];
  263. }
  264. complete(isSuc);
  265. //继续下一个
  266. [_fileModelDataArr removeObject:_curUploadFileDataModel];
  267. [self beginUploadFileFun];
  268. }
  269. @end