uploadFileManager.m 13 KB

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