uploadFileManager.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. //
  2. // uploadFileManager.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2023/11/15.
  6. //
  7. #import "uploadFileManager.h"
  8. #import <AssetsLibrary/AssetsLibrary.h>
  9. @implementation uploadFileManager
  10. static uploadFileManager * cur_uploadFileShareInstance = nil;
  11. +(uploadFileManager *)shareInstance;
  12. {
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. cur_uploadFileShareInstance = [[uploadFileManager alloc] init];
  16. });
  17. return cur_uploadFileShareInstance;
  18. }
  19. - (id)init
  20. {
  21. self = [super init];
  22. if (self) {
  23. //[self initManager];
  24. }
  25. return self;
  26. }
  27. #pragma mark 读取数据库数据
  28. - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
  29. {
  30. if(_databaseArr && _databaseArr.count == 3 && !isReGet){
  31. complete(_databaseArr);
  32. return;
  33. }
  34. _databaseArr = [NSMutableArray new];
  35. KWeakSelf
  36. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  37. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:0 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  38. NSMutableArray *failArr = [NSMutableArray new];
  39. NSMutableArray *doneArr = [NSMutableArray new];
  40. NSMutableArray *otherArr = [NSMutableArray new];
  41. for (uploadFileDataModel * curModel in array) {
  42. //图片 和视频 还原
  43. if(curModel.curUploadFileType == uploadFileTypeImage){
  44. NSString*pathStr = [cachesFileManager getFilePathWithName:curModel.filename type:uploadFileTypeImage];
  45. curModel.imageData = [NSData dataWithContentsOfFile:pathStr];
  46. }
  47. else if(curModel.curUploadFileType == uploadFileTypeVideo){
  48. NSString*pathStr = [cachesFileManager getFilePathWithName:curModel.videoFirstImageName type:uploadFileTypeImage];
  49. curModel.imageData = [NSData dataWithContentsOfFile:pathStr];
  50. if(curModel.curUploadStateType != uploadStateDone){
  51. NSString*videoPathStr = [cachesFileManager getFilePathWithName:curModel.filename type:uploadFileTypeVideo];
  52. //curModel.videoData = [NSData dataWithContentsOfFile:videoPathStr];
  53. }
  54. }
  55. if(curModel.curUploadStateType == uploadStateFail){
  56. [failArr addObject:curModel];
  57. }
  58. else if(curModel.curUploadStateType == uploadStateDone){
  59. [doneArr addObject:curModel];
  60. }
  61. else{
  62. [otherArr addObject:curModel];
  63. }
  64. }
  65. [self->_databaseArr addObject:otherArr];
  66. [self->_databaseArr addObject:doneArr];
  67. [self->_databaseArr addObject:failArr];
  68. complete(self->_databaseArr);
  69. }];
  70. });
  71. }
  72. //把TZAssetModel 转成 我们需要上传的model
  73. - (void)handlTZAssetModelToUploadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
  74. {
  75. if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
  76. return;
  77. }
  78. if(!_fileModelDataArr){
  79. _fileModelDataArr = [NSMutableArray new];
  80. }
  81. self.curUploadModelNumbers = indexPathsForSelectedItems.count;
  82. for (TZAssetModel * model in indexPathsForSelectedItems) {
  83. uploadFileDataModel * curModel = [uploadFileDataModel new];
  84. curModel.asset = model.asset;
  85. curModel.localIdentifier = model.asset.localIdentifier;
  86. curModel.imageData = model.imageData;
  87. curModel.videoData = model.videoData;
  88. curModel.filename = [model.asset valueForKey:@"filename"];
  89. curModel.curUploadStateType = uploadStateWait;
  90. if(model.type == TZAssetModelMediaTypeVideo){
  91. curModel.curUploadFileType = uploadFileTypeVideo;
  92. [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
  93. //curModel.totalBytes = [model.videoData length];
  94. curModel.totalBytes = model.totalBytes;
  95. curModel.videoData = [NSData new];//视频文件存储到文件后内存清空
  96. NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
  97. curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
  98. //第一帧图片
  99. [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  100. // 直接得到最终的 NSData 数据
  101. if (imageData) {
  102. curModel.imageData = imageData;
  103. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];;
  104. }
  105. }];
  106. //真正的视频数据
  107. // PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  108. // options.version = PHVideoRequestOptionsVersionOriginal;
  109. // [[PHImageManager defaultManager] requestAVAssetForVideo:curModel.asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
  110. // if ([asset isKindOfClass:[AVURLAsset class]]) {
  111. //
  112. // AVURLAsset* urlAsset = (AVURLAsset*)asset;
  113. // NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
  114. //
  115. // if (videoData) {
  116. // curModel.videoData = videoData;
  117. // [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
  118. // curModel.videoData = nil;
  119. // }
  120. // }
  121. // }];
  122. }
  123. else{
  124. curModel.curUploadFileType = uploadFileTypeImage;
  125. curModel.totalBytes = model.totalBytes;
  126. if(curModel.imageData)
  127. {
  128. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
  129. }
  130. else{
  131. [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  132. // 直接得到最终的 NSData 数据
  133. if (imageData) {
  134. curModel.imageData = imageData;
  135. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];;
  136. }
  137. }];
  138. }
  139. }
  140. //[_fileModelDataArr addObject:curModel];
  141. //保存到数据库
  142. curModel.bg_tableName = upLoadFile_image_tableName;
  143. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  144. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  145. HLog(@"%@ 写入 %@", curModel.filename, isSuccess ? @"成功":@"失败");
  146. }];
  147. });
  148. }
  149. KWeakSelf
  150. //if(_fileModelDataArr.count > 0){
  151. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  152. complete(Arr);
  153. [weakSelf handleUploadFileModelBg_idFun:Arr];
  154. }];
  155. //骚操作 重新查出来 拿到bg_id
  156. // [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:_fileModelDataArr.count orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  157. //
  158. // for (int i=0; i<array.count; i++) {
  159. // uploadFileDataModel *bg_mod = array[i];
  160. //
  161. // for (uploadFileDataModel *data_mod in self->_fileModelDataArr) {
  162. // if([data_mod.filename isEqualToString:bg_mod.filename]){
  163. // data_mod.bg_id = bg_mod.bg_id;
  164. // //HLog(@"bg_id:%@",data_mod.bg_id);
  165. // break;
  166. // }
  167. // }
  168. // }
  169. //
  170. // [weakSelf beginUploadFileFun];
  171. // }];
  172. //}
  173. }
  174. #pragma mark 处理当前的model 加上bg_id
  175. - (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
  176. {
  177. if(!totalArr || totalArr.count != 3){
  178. return;
  179. }
  180. NSMutableArray *curArr = totalArr[0];
  181. for (int i=0; i<_curUploadModelNumbers; i++) {
  182. uploadFileDataModel *bg_mod = curArr[i];
  183. [_fileModelDataArr addObject:bg_mod];
  184. }
  185. // for (int i=0; i<_fileModelDataArr.count; i++) {
  186. // uploadFileDataModel *bg_mod = _fileModelDataArr[i];
  187. //
  188. // for (uploadFileDataModel *data_mod in curArr) {
  189. // if([data_mod.filename isEqualToString:bg_mod.filename]){
  190. // bg_mod.bg_id = data_mod.bg_id;
  191. // //HLog(@"bg_id:%@",data_mod.bg_id);
  192. // break;
  193. // }
  194. // }
  195. // }
  196. [self beginUploadFileFun];
  197. }
  198. - (void)beginUploadFileFun
  199. {
  200. if(_isUploadIngType){
  201. return;
  202. }
  203. _isSuspendType = NO;
  204. _isUploadIngType = YES;
  205. _curUploadFileDataModel = _fileModelDataArr.firstObject;
  206. if(!_curUploadFileDataModel){
  207. return;
  208. }
  209. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage){
  210. NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:_curUploadFileDataModel.curUploadFileType];
  211. _curUploadFileDataModel.imageData = [NSData dataWithContentsOfFile:pathStr];
  212. if(_curUploadFileDataModel.imageData && _curUploadFileDataModel.imageData.length >0){
  213. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  214. return;
  215. }
  216. }
  217. else{
  218. //NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
  219. if([cachesFileManager checkFileIsSaveState:_curUploadFileDataModel.filename withType:uploadFileTypeVideo]){
  220. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  221. return;
  222. }
  223. }
  224. if(!_curUploadFileDataModel.asset){
  225. NSString *curLocalIdentifier = _curUploadFileDataModel.localIdentifier;
  226. PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
  227. PHAsset *asset = fetchResult.firstObject;
  228. _curUploadFileDataModel.asset = asset;
  229. }
  230. if(!_curUploadFileDataModel.asset){
  231. [self getDataWrongToChangeFailFun];
  232. return;
  233. }
  234. KWeakSelf
  235. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage)
  236. {
  237. if(!_curUploadFileDataModel.imageData || _curUploadFileDataModel.imageData.length == 0){
  238. [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  239. // 直接得到最终的 NSData 数据
  240. if (imageData) {
  241. self->_curUploadFileDataModel.imageData = imageData;
  242. [weakSelf afterGetImageDataFun];
  243. }
  244. else{
  245. [weakSelf getDataWrongToChangeFailFun];
  246. }
  247. }];
  248. }
  249. }
  250. else if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
  251. //第一帧图片
  252. [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  253. // 直接得到最终的 NSData 数据
  254. if (imageData) {
  255. self->_curUploadFileDataModel.imageData = imageData;
  256. [weakSelf afterGetImageDataInVideoFun];
  257. }
  258. }];
  259. //真正的视频数据
  260. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  261. options.version = PHVideoRequestOptionsVersionOriginal;
  262. [[PHImageManager defaultManager] requestAVAssetForVideo:_curUploadFileDataModel.asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
  263. if ([asset isKindOfClass:[AVURLAsset class]]) {
  264. AVURLAsset* urlAsset = (AVURLAsset*)asset;
  265. BOOL isSuc = [cachesFileManager copyVideoItemAtPath:[urlAsset.URL path] fileName:_curUploadFileDataModel.filename error:nil];
  266. //NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
  267. if (isSuc) {
  268. //self->_curUploadFileDataModel.videoData = videoData;
  269. [weakSelf afterGetVideoDataFun];
  270. }
  271. else{
  272. [weakSelf getDataWrongToChangeFailFun];
  273. }
  274. }
  275. }];
  276. }
  277. }
  278. #pragma mark 获取数据失败 报错
  279. - (void)getDataWrongToChangeFailFun
  280. {
  281. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  282. }];
  283. }
  284. #pragma mark 根据 asset 获取到图片数据
  285. - (void)afterGetImageDataFun
  286. {
  287. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.filename type:uploadFileTypeImage];
  288. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  289. }
  290. - (void)afterGetImageDataInVideoFun
  291. {
  292. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.videoFirstImageName type:uploadFileTypeImage];
  293. }
  294. - (void)afterGetVideoDataFun
  295. {
  296. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.videoData fileName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
  297. _curUploadFileDataModel.videoData = nil;
  298. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  299. }
  300. //修改文件上传的状态
  301. - (void)changeUploadFileState:(uploadStateType)curUploadStateType withDidUploadBytes:(long)didUpLoadBytes withModel:(uploadFileDataModel*)model complete:(custom_complete_B)complete
  302. {
  303. if(model.bg_id.integerValue == _curUploadFileDataModel.bg_id.integerValue){
  304. _curUploadFileDataModel.curUploadStateType = curUploadStateType;
  305. _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
  306. }
  307. model.curUploadStateType = curUploadStateType;
  308. model.didUploadBytes = didUpLoadBytes;
  309. if(!_isSuspendType || curUploadStateType == uploadStateSuspend){
  310. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
  311. }
  312. if(curUploadStateType == uploadStateUploading){
  313. return;
  314. }
  315. //NSNumber * numberUploadState = nil;
  316. NSString* where = nil;
  317. // if(curUploadStateType == uploadStateDone)
  318. // {//只有上传中的才可能是完成的
  319. // numberUploadState = [NSNumber numberWithInt:uploadStateUploading];
  320. // where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  321. // }
  322. // else
  323. // {//查找非上传完成的
  324. // numberUploadState = [NSNumber numberWithInt:uploadStateDone];
  325. // where = [NSString stringWithFormat:@"where %@=%@ and %@!=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  326. // }
  327. where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
  328. //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
  329. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName where:where complete:^(NSArray * _Nullable array) {
  330. for (uploadFileDataModel * curModel in array) {
  331. curModel.curUploadStateType = curUploadStateType;
  332. curModel.didUploadBytes = didUpLoadBytes;
  333. if(curUploadStateType == uploadStateDone){
  334. curModel.videoData = [NSData new];
  335. if(curModel.curUploadFileType == uploadFileTypeVideo){
  336. [cachesFileManager removeItemAtPath:curModel.filename type:uploadFileTypeVideo error:nil];
  337. }
  338. }
  339. else if(curUploadStateType == uploadStateFail){
  340. }
  341. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  342. HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  343. }];
  344. }
  345. complete(YES);
  346. }];
  347. }
  348. //暂停上传完成
  349. - (void)suspendUploadFileFun:(BOOL)isSuspendAll
  350. {
  351. // if(isSuspendAll){
  352. //
  353. // }
  354. if(!_fileModelDataArr || !_curUploadFileDataModel){
  355. return;
  356. }
  357. _isSuspendType = YES;
  358. _isUploadIngType = NO;
  359. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
  360. NSEnumerator *curArr = [_fileModelDataArr reverseObjectEnumerator];
  361. for (uploadFileDataModel*model in curArr) {
  362. HLog(@"111hxd 2 %@ ",_fileModelDataArr)
  363. [self changeUploadFileState:uploadStateSuspend withDidUploadBytes:model.didUploadBytes withModel:model complete:^(BOOL isSuccess) {
  364. }];
  365. }
  366. }
  367. //某个文件重新上传
  368. - (void)reUploadFileFunBy:(NSMutableArray*)Arr
  369. {
  370. if(!_fileModelDataArr){
  371. _fileModelDataArr = [NSMutableArray new];
  372. }
  373. [_fileModelDataArr addObjectsFromArray:Arr];
  374. [self beginUploadFileFun];
  375. }
  376. - (void)uploadFileDoneFun
  377. {
  378. long totalSizeByte = _curUploadFileDataModel.totalBytes;
  379. [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  380. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  381. self->_isUploadIngType = NO;
  382. if(self->_fileModelDataArr.count > 0){
  383. [self beginUploadFileFun];
  384. }
  385. }];
  386. }
  387. //文件上传失败
  388. - (void)uploadFileFailFun
  389. {
  390. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  391. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  392. if(self->_fileModelDataArr.count > 0){
  393. [self beginUploadFileFun];
  394. }
  395. else{
  396. self->_isUploadIngType = NO;
  397. }
  398. }];
  399. mainBlock(^{
  400. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  401. });
  402. }
  403. //删除本地数据库记录
  404. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr complete:(custom_complete_B)complete
  405. {
  406. //逻辑待优化
  407. BOOL isSuc = false;
  408. for (uploadFileDataModel *uploadFileDataMod in delArr) {
  409. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  410. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  411. [where appendString:curStr];
  412. isSuc = [uploadFileDataModel bg_delete:upLoadFile_image_tableName where:where];
  413. //删除本地图片
  414. if(isSuc){
  415. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  416. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  417. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  418. }
  419. else{
  420. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  421. }
  422. }
  423. }
  424. complete(isSuc);
  425. //继续下一个
  426. if(_fileModelDataArr){
  427. [_fileModelDataArr removeObject:_curUploadFileDataModel];
  428. _isUploadIngType = NO;
  429. if(_fileModelDataArr.count >=1){
  430. [self beginUploadFileFun];
  431. }
  432. }
  433. }
  434. @end