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. AVURLAsset* urlAsset = (AVURLAsset*)asset;
  112. NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
  113. if (videoData) {
  114. curModel.videoData = videoData;
  115. [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
  116. curModel.videoData = nil;
  117. }
  118. }
  119. }];
  120. }
  121. else{
  122. curModel.curUploadFileType = uploadFileTypeImage;
  123. curModel.totalBytes = model.totalBytes;
  124. if(curModel.imageData)
  125. {
  126. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
  127. }
  128. else{
  129. [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  130. // 直接得到最终的 NSData 数据
  131. if (imageData) {
  132. curModel.imageData = imageData;
  133. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];;
  134. }
  135. }];
  136. }
  137. }
  138. //[_fileModelDataArr addObject:curModel];
  139. //保存到数据库
  140. curModel.bg_tableName = upLoadFile_image_tableName;
  141. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  142. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  143. HLog(@"%@ 写入 %@", curModel.filename, isSuccess ? @"成功":@"失败");
  144. }];
  145. });
  146. }
  147. KWeakSelf
  148. //if(_fileModelDataArr.count > 0){
  149. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  150. complete(Arr);
  151. [weakSelf handleUploadFileModelBg_idFun:Arr];
  152. }];
  153. //骚操作 重新查出来 拿到bg_id
  154. // [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:_fileModelDataArr.count orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  155. //
  156. // for (int i=0; i<array.count; i++) {
  157. // uploadFileDataModel *bg_mod = array[i];
  158. //
  159. // for (uploadFileDataModel *data_mod in self->_fileModelDataArr) {
  160. // if([data_mod.filename isEqualToString:bg_mod.filename]){
  161. // data_mod.bg_id = bg_mod.bg_id;
  162. // //HLog(@"bg_id:%@",data_mod.bg_id);
  163. // break;
  164. // }
  165. // }
  166. // }
  167. //
  168. // [weakSelf beginUploadFileFun];
  169. // }];
  170. //}
  171. }
  172. #pragma mark 处理当前的model 加上bg_id
  173. - (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
  174. {
  175. if(!totalArr || totalArr.count != 3){
  176. return;
  177. }
  178. NSMutableArray *curArr = totalArr[0];
  179. for (int i=0; i<_curUploadModelNumbers; i++) {
  180. uploadFileDataModel *bg_mod = curArr[i];
  181. [_fileModelDataArr addObject:bg_mod];
  182. }
  183. // for (int i=0; i<_fileModelDataArr.count; i++) {
  184. // uploadFileDataModel *bg_mod = _fileModelDataArr[i];
  185. //
  186. // for (uploadFileDataModel *data_mod in curArr) {
  187. // if([data_mod.filename isEqualToString:bg_mod.filename]){
  188. // bg_mod.bg_id = data_mod.bg_id;
  189. // //HLog(@"bg_id:%@",data_mod.bg_id);
  190. // break;
  191. // }
  192. // }
  193. // }
  194. [self beginUploadFileFun];
  195. }
  196. - (void)beginUploadFileFun
  197. {
  198. if(_isUploadIngType){
  199. return;
  200. }
  201. _isSuspendType = NO;
  202. _isUploadIngType = YES;
  203. _curUploadFileDataModel = _fileModelDataArr.firstObject;
  204. if(!_curUploadFileDataModel){
  205. return;
  206. }
  207. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage){
  208. NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:_curUploadFileDataModel.curUploadFileType];
  209. _curUploadFileDataModel.imageData = [NSData dataWithContentsOfFile:pathStr];
  210. if(_curUploadFileDataModel.imageData && _curUploadFileDataModel.imageData.length >0){
  211. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  212. return;
  213. }
  214. }
  215. else{
  216. NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
  217. NSData *curdata = [NSData dataWithContentsOfFile:pathStr];
  218. if(curdata && curdata.length >0){
  219. curdata = nil;
  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. NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
  266. if (videoData) {
  267. self->_curUploadFileDataModel.videoData = videoData;
  268. [weakSelf afterGetVideoDataFun];
  269. }
  270. else{
  271. [weakSelf getDataWrongToChangeFailFun];
  272. }
  273. }
  274. }];
  275. }
  276. }
  277. #pragma mark 获取数据失败 报错
  278. - (void)getDataWrongToChangeFailFun
  279. {
  280. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  281. }];
  282. }
  283. #pragma mark 根据 asset 获取到图片数据
  284. - (void)afterGetImageDataFun
  285. {
  286. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.filename type:uploadFileTypeImage];
  287. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  288. }
  289. - (void)afterGetImageDataInVideoFun
  290. {
  291. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.videoFirstImageName type:uploadFileTypeImage];
  292. }
  293. - (void)afterGetVideoDataFun
  294. {
  295. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.videoData fileName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
  296. _curUploadFileDataModel.videoData = nil;
  297. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  298. }
  299. //修改文件上传的状态
  300. - (void)changeUploadFileState:(uploadStateType)curUploadStateType withDidUploadBytes:(long)didUpLoadBytes withModel:(uploadFileDataModel*)model complete:(custom_complete_B)complete
  301. {
  302. if(model.bg_id.integerValue == _curUploadFileDataModel.bg_id.integerValue){
  303. _curUploadFileDataModel.curUploadStateType = curUploadStateType;
  304. _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
  305. }
  306. model.curUploadStateType = curUploadStateType;
  307. model.didUploadBytes = didUpLoadBytes;
  308. if(!_isSuspendType || curUploadStateType == uploadStateSuspend){
  309. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
  310. }
  311. if(curUploadStateType == uploadStateUploading){
  312. return;
  313. }
  314. //NSNumber * numberUploadState = nil;
  315. NSString* where = nil;
  316. // if(curUploadStateType == uploadStateDone)
  317. // {//只有上传中的才可能是完成的
  318. // numberUploadState = [NSNumber numberWithInt:uploadStateUploading];
  319. // where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  320. // }
  321. // else
  322. // {//查找非上传完成的
  323. // numberUploadState = [NSNumber numberWithInt:uploadStateDone];
  324. // where = [NSString stringWithFormat:@"where %@=%@ and %@!=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  325. // }
  326. where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
  327. //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
  328. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName where:where complete:^(NSArray * _Nullable array) {
  329. for (uploadFileDataModel * curModel in array) {
  330. curModel.curUploadStateType = curUploadStateType;
  331. curModel.didUploadBytes = didUpLoadBytes;
  332. if(curUploadStateType == uploadStateDone){
  333. curModel.videoData = [NSData new];
  334. if(curModel.curUploadFileType == uploadFileTypeVideo){
  335. [cachesFileManager removeItemAtPath:curModel.filename type:uploadFileTypeVideo error:nil];
  336. }
  337. }
  338. else if(curUploadStateType == uploadStateFail){
  339. }
  340. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  341. HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  342. }];
  343. }
  344. complete(YES);
  345. }];
  346. }
  347. //暂停上传完成
  348. - (void)suspendUploadFileFun:(BOOL)isSuspendAll
  349. {
  350. // if(isSuspendAll){
  351. //
  352. // }
  353. if(!_fileModelDataArr || !_curUploadFileDataModel){
  354. return;
  355. }
  356. _isSuspendType = YES;
  357. _isUploadIngType = NO;
  358. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
  359. NSEnumerator *curArr = [_fileModelDataArr reverseObjectEnumerator];
  360. for (uploadFileDataModel*model in curArr) {
  361. HLog(@"111hxd 2 %@ ",_fileModelDataArr)
  362. [self changeUploadFileState:uploadStateSuspend withDidUploadBytes:model.didUploadBytes withModel:model complete:^(BOOL isSuccess) {
  363. }];
  364. }
  365. }
  366. //某个文件重新上传
  367. - (void)reUploadFileFunBy:(NSMutableArray*)Arr
  368. {
  369. if(!_fileModelDataArr){
  370. _fileModelDataArr = [NSMutableArray new];
  371. }
  372. [_fileModelDataArr addObjectsFromArray:Arr];
  373. [self beginUploadFileFun];
  374. }
  375. - (void)uploadFileDoneFun
  376. {
  377. long totalSizeByte = _curUploadFileDataModel.totalBytes;
  378. [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  379. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  380. self->_isUploadIngType = NO;
  381. if(self->_fileModelDataArr.count > 0){
  382. [self beginUploadFileFun];
  383. }
  384. }];
  385. }
  386. //文件上传失败
  387. - (void)uploadFileFailFun
  388. {
  389. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  390. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  391. if(self->_fileModelDataArr.count > 0){
  392. [self beginUploadFileFun];
  393. }
  394. else{
  395. self->_isUploadIngType = NO;
  396. }
  397. }];
  398. mainBlock(^{
  399. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  400. });
  401. }
  402. //删除本地数据库记录
  403. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr complete:(custom_complete_B)complete
  404. {
  405. //逻辑待优化
  406. BOOL isSuc = false;
  407. for (uploadFileDataModel *uploadFileDataMod in delArr) {
  408. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  409. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  410. [where appendString:curStr];
  411. isSuc = [uploadFileDataModel bg_delete:upLoadFile_image_tableName where:where];
  412. //删除本地图片
  413. if(isSuc){
  414. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  415. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  416. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  417. }
  418. else{
  419. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  420. }
  421. }
  422. }
  423. complete(isSuc);
  424. //继续下一个
  425. if(_fileModelDataArr){
  426. [_fileModelDataArr removeObject:_curUploadFileDataModel];
  427. _isUploadIngType = NO;
  428. if(_fileModelDataArr.count >=1){
  429. [self beginUploadFileFun];
  430. }
  431. }
  432. }
  433. @end