uploadFileManager.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  151. complete(Arr);
  152. [weakSelf handleUploadFileModelBg_idFun:Arr];
  153. }];
  154. }
  155. #pragma mark 处理当前的model 加上bg_id
  156. - (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
  157. {
  158. if(!totalArr || totalArr.count != 3){
  159. return;
  160. }
  161. NSMutableArray *curArr = totalArr[0];
  162. for (int i=0; i<_curUploadModelNumbers; i++) {
  163. uploadFileDataModel *bg_mod = curArr[i];
  164. [_fileModelDataArr addObject:bg_mod];
  165. }
  166. // for (int i=0; i<_fileModelDataArr.count; i++) {
  167. // uploadFileDataModel *bg_mod = _fileModelDataArr[i];
  168. //
  169. // for (uploadFileDataModel *data_mod in curArr) {
  170. // if([data_mod.filename isEqualToString:bg_mod.filename]){
  171. // bg_mod.bg_id = data_mod.bg_id;
  172. // //HLog(@"bg_id:%@",data_mod.bg_id);
  173. // break;
  174. // }
  175. // }
  176. // }
  177. [self beginUploadFileFun];
  178. }
  179. - (void)beginUploadFileFun
  180. {
  181. if(_isUploadIngType){
  182. return;
  183. }
  184. _isSuspendType = NO;
  185. _isUploadIngType = YES;
  186. _curUploadFileDataModel = _fileModelDataArr.firstObject;
  187. if(!_curUploadFileDataModel){
  188. return;
  189. }
  190. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage){
  191. NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:_curUploadFileDataModel.curUploadFileType];
  192. _curUploadFileDataModel.imageData = [NSData dataWithContentsOfFile:pathStr];
  193. if(_curUploadFileDataModel.imageData && _curUploadFileDataModel.imageData.length >0){
  194. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  195. return;
  196. }
  197. }
  198. else{
  199. //NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
  200. if([cachesFileManager checkFileIsSaveState:_curUploadFileDataModel.filename withType:uploadFileTypeVideo]){
  201. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  202. return;
  203. }
  204. }
  205. if(!_curUploadFileDataModel.asset){
  206. NSString *curLocalIdentifier = _curUploadFileDataModel.localIdentifier;
  207. PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
  208. PHAsset *asset = fetchResult.firstObject;
  209. _curUploadFileDataModel.asset = asset;
  210. }
  211. if(!_curUploadFileDataModel.asset){
  212. [self getDataWrongToChangeFailFun];
  213. return;
  214. }
  215. KWeakSelf
  216. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage)
  217. {
  218. if(!_curUploadFileDataModel.imageData || _curUploadFileDataModel.imageData.length == 0){
  219. [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  220. // 直接得到最终的 NSData 数据
  221. if (imageData) {
  222. self->_curUploadFileDataModel.imageData = imageData;
  223. [weakSelf afterGetImageDataFun];
  224. }
  225. else{
  226. [weakSelf getDataWrongToChangeFailFun];
  227. }
  228. }];
  229. }
  230. }
  231. else if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
  232. //第一帧图片
  233. [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  234. // 直接得到最终的 NSData 数据
  235. if (imageData) {
  236. self->_curUploadFileDataModel.imageData = imageData;
  237. [weakSelf afterGetImageDataInVideoFun];
  238. }
  239. }];
  240. //真正的视频数据
  241. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  242. options.version = PHVideoRequestOptionsVersionOriginal;
  243. [[PHImageManager defaultManager] requestAVAssetForVideo:_curUploadFileDataModel.asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
  244. if ([asset isKindOfClass:[AVURLAsset class]]) {
  245. AVURLAsset* urlAsset = (AVURLAsset*)asset;
  246. BOOL isSuc = [cachesFileManager copyVideoItemAtPath:[urlAsset.URL path] fileName:_curUploadFileDataModel.filename error:nil];
  247. //NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
  248. if (isSuc) {
  249. //self->_curUploadFileDataModel.videoData = videoData;
  250. [weakSelf afterGetVideoDataFun];
  251. }
  252. else{
  253. [weakSelf getDataWrongToChangeFailFun];
  254. }
  255. }
  256. }];
  257. }
  258. }
  259. #pragma mark 获取数据失败 报错
  260. - (void)getDataWrongToChangeFailFun
  261. {
  262. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  263. }];
  264. }
  265. #pragma mark 根据 asset 获取到图片数据
  266. - (void)afterGetImageDataFun
  267. {
  268. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.filename type:uploadFileTypeImage];
  269. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  270. }
  271. - (void)afterGetImageDataInVideoFun
  272. {
  273. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.videoFirstImageName type:uploadFileTypeImage];
  274. }
  275. - (void)afterGetVideoDataFun
  276. {
  277. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.videoData fileName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
  278. _curUploadFileDataModel.videoData = nil;
  279. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  280. }
  281. //修改文件上传的状态
  282. - (void)changeUploadFileState:(uploadStateType)curUploadStateType withDidUploadBytes:(long)didUpLoadBytes withModel:(uploadFileDataModel*)model complete:(custom_complete_B)complete
  283. {
  284. if(model.bg_id.integerValue == _curUploadFileDataModel.bg_id.integerValue){
  285. _curUploadFileDataModel.curUploadStateType = curUploadStateType;
  286. _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
  287. }
  288. model.curUploadStateType = curUploadStateType;
  289. model.didUploadBytes = didUpLoadBytes;
  290. if(!_isSuspendType || curUploadStateType == uploadStateSuspend){
  291. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
  292. }
  293. if(curUploadStateType == uploadStateUploading){
  294. return;
  295. }
  296. //NSNumber * numberUploadState = nil;
  297. NSString* where = nil;
  298. // if(curUploadStateType == uploadStateDone)
  299. // {//只有上传中的才可能是完成的
  300. // numberUploadState = [NSNumber numberWithInt:uploadStateUploading];
  301. // where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  302. // }
  303. // else
  304. // {//查找非上传完成的
  305. // numberUploadState = [NSNumber numberWithInt:uploadStateDone];
  306. // where = [NSString stringWithFormat:@"where %@=%@ and %@!=%@ ",bg_sqlKey(@"filename"),bg_sqlValue(_curUploadFileDataModel.filename),bg_sqlKey(@"curUploadStateType"),bg_sqlValue(numberUploadState)];
  307. // }
  308. where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
  309. //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
  310. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName where:where complete:^(NSArray * _Nullable array) {
  311. for (uploadFileDataModel * curModel in array) {
  312. curModel.curUploadStateType = curUploadStateType;
  313. curModel.didUploadBytes = didUpLoadBytes;
  314. if(curUploadStateType == uploadStateDone){
  315. curModel.videoData = [NSData new];
  316. if(curModel.curUploadFileType == uploadFileTypeVideo){
  317. [cachesFileManager removeItemAtPath:curModel.filename type:uploadFileTypeVideo error:nil];
  318. }
  319. }
  320. else if(curUploadStateType == uploadStateFail){
  321. }
  322. [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  323. HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  324. }];
  325. }
  326. complete(YES);
  327. }];
  328. }
  329. //暂停上传完成
  330. - (void)suspendUploadFileFun:(BOOL)isSuspendAll
  331. {
  332. // if(isSuspendAll){
  333. //
  334. // }
  335. if(!_fileModelDataArr || !_curUploadFileDataModel){
  336. return;
  337. }
  338. _isSuspendType = YES;
  339. _isUploadIngType = NO;
  340. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
  341. NSEnumerator *curArr = [_fileModelDataArr reverseObjectEnumerator];
  342. for (uploadFileDataModel*model in curArr) {
  343. [self changeUploadFileState:uploadStateSuspend withDidUploadBytes:model.didUploadBytes withModel:model complete:^(BOOL isSuccess) {
  344. }];
  345. }
  346. }
  347. //某个文件重新上传
  348. - (void)reUploadFileFunBy:(NSMutableArray*)Arr
  349. {
  350. if(!_fileModelDataArr){
  351. _fileModelDataArr = [NSMutableArray new];
  352. }
  353. for (uploadFileDataModel*addModel in Arr) {
  354. BOOL needAddModel = YES;
  355. for (uploadFileDataModel*preModel in _fileModelDataArr) {
  356. if(addModel.bg_id.integerValue == preModel.bg_id.integerValue
  357. || [addModel.localIdentifier isEqualToString:preModel.localIdentifier])
  358. {
  359. needAddModel = NO;
  360. break;
  361. }
  362. }
  363. if(needAddModel){
  364. [_fileModelDataArr addObject:addModel];
  365. }
  366. }
  367. //[_fileModelDataArr addObjectsFromArray:Arr];
  368. [self beginUploadFileFun];
  369. }
  370. - (void)uploadFileDoneFun
  371. {
  372. long totalSizeByte = _curUploadFileDataModel.totalBytes;
  373. [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  374. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  375. self->_isUploadIngType = NO;
  376. if(self->_fileModelDataArr.count > 0){
  377. [self beginUploadFileFun];
  378. }
  379. }];
  380. }
  381. //文件上传失败
  382. - (void)uploadFileFailFun
  383. {
  384. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  385. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  386. if(self->_fileModelDataArr.count > 0){
  387. [self beginUploadFileFun];
  388. }
  389. else{
  390. self->_isUploadIngType = NO;
  391. }
  392. }];
  393. mainBlock(^{
  394. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  395. });
  396. }
  397. //删除本地数据库记录
  398. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr complete:(custom_complete_B)complete
  399. {
  400. //逻辑待优化
  401. BOOL isSuc = false;
  402. for (uploadFileDataModel *uploadFileDataMod in delArr) {
  403. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  404. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  405. [where appendString:curStr];
  406. isSuc = [uploadFileDataModel bg_delete:upLoadFile_image_tableName where:where];
  407. //删除本地图片
  408. if(isSuc){
  409. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  410. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  411. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  412. }
  413. else{
  414. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  415. }
  416. }
  417. }
  418. complete(isSuc);
  419. //继续下一个
  420. if(_fileModelDataArr){
  421. [_fileModelDataArr removeObject:_curUploadFileDataModel];
  422. _isUploadIngType = NO;
  423. if(_fileModelDataArr.count >=1){
  424. [self beginUploadFileFun];
  425. }
  426. }
  427. }
  428. @end