uploadFileManager.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
  75. return;
  76. }
  77. if(!_fileModelDataArr){
  78. _fileModelDataArr = [NSMutableArray new];
  79. }
  80. self.curUploadModelNumbers = indexPathsForSelectedItems.count;
  81. for (TZAssetModel * model in indexPathsForSelectedItems) {
  82. uploadFileDataModel * curModel = [uploadFileDataModel new];
  83. curModel.imageData = model.imageData;
  84. curModel.videoData = model.videoData;
  85. curModel.filename = [model.asset valueForKey:@"filename"];
  86. curModel.curUploadStateType = uploadStateWait;
  87. if(model.type == TZAssetModelMediaTypeVideo){
  88. curModel.curUploadFileType = uploadFileTypeVideo;
  89. [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
  90. curModel.totalBytes = [model.videoData length];
  91. curModel.videoData = [NSData new];//视频文件存储到文件后内存清空
  92. NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
  93. curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
  94. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];
  95. }
  96. else{
  97. curModel.curUploadFileType = uploadFileTypeImage;
  98. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
  99. curModel.totalBytes = [model.imageData length];
  100. }
  101. //[_fileModelDataArr addObject:curModel];
  102. //保存到数据库
  103. curModel.bg_tableName = upLoadFile_image_tableName;
  104. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  105. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  106. HLog(@"%@ 写入 %@", curModel.filename, isSuccess ? @"成功":@"失败");
  107. }];
  108. });
  109. }
  110. KWeakSelf
  111. //if(_fileModelDataArr.count > 0){
  112. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  113. complete(Arr);
  114. [weakSelf handleUploadFileModelBg_idFun:Arr];
  115. }];
  116. //骚操作 重新查出来 拿到bg_id
  117. // [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:_fileModelDataArr.count orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  118. //
  119. // for (int i=0; i<array.count; i++) {
  120. // uploadFileDataModel *bg_mod = array[i];
  121. //
  122. // for (uploadFileDataModel *data_mod in self->_fileModelDataArr) {
  123. // if([data_mod.filename isEqualToString:bg_mod.filename]){
  124. // data_mod.bg_id = bg_mod.bg_id;
  125. // //HLog(@"bg_id:%@",data_mod.bg_id);
  126. // break;
  127. // }
  128. // }
  129. // }
  130. //
  131. // [weakSelf beginUploadFileFun];
  132. // }];
  133. //}
  134. }
  135. #pragma mark 处理当前的model 加上bg_id
  136. - (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
  137. {
  138. if(!totalArr || totalArr.count != 3){
  139. return;
  140. }
  141. NSMutableArray *curArr = totalArr[0];
  142. for (int i=0; i<_curUploadModelNumbers; i++) {
  143. uploadFileDataModel *bg_mod = curArr[i];
  144. [_fileModelDataArr addObject:bg_mod];
  145. }
  146. // for (int i=0; i<_fileModelDataArr.count; i++) {
  147. // uploadFileDataModel *bg_mod = _fileModelDataArr[i];
  148. //
  149. // for (uploadFileDataModel *data_mod in curArr) {
  150. // if([data_mod.filename isEqualToString:bg_mod.filename]){
  151. // bg_mod.bg_id = data_mod.bg_id;
  152. // //HLog(@"bg_id:%@",data_mod.bg_id);
  153. // break;
  154. // }
  155. // }
  156. // }
  157. [self beginUploadFileFun];
  158. }
  159. - (void)beginUploadFileFun
  160. {
  161. if(_isUploadIngType){
  162. return;
  163. }
  164. _isSuspendType = NO;
  165. _isUploadIngType = YES;
  166. _curUploadFileDataModel = _fileModelDataArr.firstObject;
  167. if(!_curUploadFileDataModel){
  168. return;
  169. }
  170. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  171. }
  172. //修改文件上传的状态
  173. - (void)changeUploadFileState:(uploadStateType)curUploadStateType withDidUploadBytes:(long)didUpLoadBytes withModel:(uploadFileDataModel*)model complete:(custom_complete_B)complete
  174. {
  175. if(model.bg_id.integerValue == _curUploadFileDataModel.bg_id.integerValue){
  176. _curUploadFileDataModel.curUploadStateType = curUploadStateType;
  177. _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
  178. }
  179. model.curUploadStateType = curUploadStateType;
  180. model.didUploadBytes = didUpLoadBytes;
  181. if(!_isSuspendType || curUploadStateType == uploadStateSuspend){
  182. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
  183. }
  184. if(curUploadStateType == uploadStateUploading){
  185. return;
  186. }
  187. //NSNumber * numberUploadState = nil;
  188. NSString* where = nil;
  189. // if(curUploadStateType == uploadStateDone)
  190. // {//只有上传中的才可能是完成的
  191. // numberUploadState = [NSNumber numberWithInt:uploadStateUploading];
  192. // where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  193. // }
  194. // else
  195. // {//查找非上传完成的
  196. // numberUploadState = [NSNumber numberWithInt:uploadStateDone];
  197. // where = [NSString stringWithFormat:@"where %@=%@ and %@!=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  198. // }
  199. where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
  200. //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
  201. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName where:where complete:^(NSArray * _Nullable array) {
  202. for (uploadFileDataModel * curModel in array) {
  203. curModel.curUploadStateType = curUploadStateType;
  204. curModel.didUploadBytes = didUpLoadBytes;
  205. if(curUploadStateType == uploadStateDone){
  206. curModel.videoData = [NSData new];
  207. if(curModel.curUploadFileType == uploadFileTypeVideo){
  208. [cachesFileManager removeItemAtPath:curModel.filename type:uploadFileTypeVideo error:nil];
  209. }
  210. }
  211. else if(curUploadStateType == uploadStateFail){
  212. }
  213. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  214. HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  215. }];
  216. }
  217. complete(YES);
  218. }];
  219. }
  220. //暂停上传完成
  221. - (void)suspendUploadFileFun:(BOOL)isSuspendAll
  222. {
  223. // if(isSuspendAll){
  224. //
  225. // }
  226. if(!_fileModelDataArr || !_curUploadFileDataModel){
  227. return;
  228. }
  229. _isSuspendType = YES;
  230. _isUploadIngType = NO;
  231. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
  232. NSEnumerator *curArr = [_fileModelDataArr reverseObjectEnumerator];
  233. for (uploadFileDataModel*model in curArr) {
  234. HLog(@"111hxd 2 %@ ",_fileModelDataArr)
  235. [self changeUploadFileState:uploadStateSuspend withDidUploadBytes:model.didUploadBytes withModel:model complete:^(BOOL isSuccess) {
  236. }];
  237. }
  238. }
  239. //某个文件重新上传
  240. - (void)reUploadFileFunBy:(NSMutableArray*)Arr
  241. {
  242. if(!_fileModelDataArr){
  243. _fileModelDataArr = [NSMutableArray new];
  244. }
  245. [_fileModelDataArr addObjectsFromArray:Arr];
  246. [self beginUploadFileFun];
  247. }
  248. - (void)uploadFileDoneFun
  249. {
  250. long totalSizeByte = _curUploadFileDataModel.totalBytes;
  251. [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  252. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  253. self->_isUploadIngType = NO;
  254. if(self->_fileModelDataArr.count > 0){
  255. [self beginUploadFileFun];
  256. }
  257. }];
  258. }
  259. //文件上传失败
  260. - (void)uploadFileFailFun
  261. {
  262. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  263. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  264. if(self->_fileModelDataArr.count > 0){
  265. [self beginUploadFileFun];
  266. }
  267. else{
  268. self->_isUploadIngType = NO;
  269. }
  270. }];
  271. mainBlock(^{
  272. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  273. });
  274. }
  275. //删除本地数据库记录
  276. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr complete:(custom_complete_B)complete
  277. {
  278. //逻辑待优化
  279. BOOL isSuc = false;
  280. for (uploadFileDataModel *uploadFileDataMod in delArr) {
  281. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  282. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  283. [where appendString:curStr];
  284. isSuc = [uploadFileDataModel bg_delete:upLoadFile_image_tableName where:where];
  285. //删除本地图片
  286. if(isSuc){
  287. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  288. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  289. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  290. }
  291. else{
  292. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  293. }
  294. }
  295. }
  296. complete(isSuc);
  297. //继续下一个
  298. if(_fileModelDataArr){
  299. [_fileModelDataArr removeObject:_curUploadFileDataModel];
  300. _isUploadIngType = NO;
  301. if(_fileModelDataArr.count >=1){
  302. [self beginUploadFileFun];
  303. }
  304. }
  305. }
  306. @end