uploadFileManager.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. #import "photosBackupsTaskModel.h"
  10. #import "AFNetworkReachabilityManager.h"
  11. @interface uploadFileManager ()
  12. /**检测任务10s后 无反馈重新开始 */
  13. @property (nonatomic, assign) NSInteger taskRenewTime; //
  14. @end
  15. @implementation uploadFileManager
  16. static uploadFileManager * cur_uploadFileShareInstance = nil;
  17. +(uploadFileManager *)shareInstance;
  18. {
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. cur_uploadFileShareInstance = [[uploadFileManager alloc] init];
  22. });
  23. return cur_uploadFileShareInstance;
  24. }
  25. - (id)init
  26. {
  27. self = [super init];
  28. if (self) {
  29. //[self initManager];
  30. }
  31. return self;
  32. }
  33. #pragma mark 读取数据库数据
  34. - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
  35. {
  36. if(_databaseArr && _databaseArr.count == 3 && !isReGet){
  37. complete(_databaseArr);
  38. return;
  39. }
  40. if(!_databaseArr)
  41. {
  42. _databaseArr = [NSMutableArray new];
  43. }
  44. //KWeakSelf
  45. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  46. [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName limit:0 orderBy:nil desc:NO complete:^(NSArray * _Nullable array) {
  47. NSMutableArray *failArr = [NSMutableArray new];
  48. NSMutableArray *doneArr = [NSMutableArray new];
  49. NSMutableArray *otherArr = [NSMutableArray new];
  50. for (uploadFileDataModel * curModel in array) {
  51. //图片 和视频 还原
  52. if(curModel.curUploadFileType == uploadFileTypeImage){
  53. }
  54. else if(curModel.curUploadFileType == uploadFileTypeVideo){
  55. if(curModel.curUploadStateType != uploadStateDone){
  56. }
  57. }
  58. if(curModel.curUploadStateType == uploadStateFail){
  59. [failArr addObject:curModel];
  60. }
  61. else if(curModel.curUploadStateType == uploadStateDone){
  62. [doneArr addObject:curModel];
  63. }
  64. else{
  65. [otherArr addObject:curModel];
  66. }
  67. }
  68. NSLock *lock = [NSLock new];
  69. [lock lock];
  70. NSMutableArray *newArr = [NSMutableArray new];
  71. [newArr addObject:otherArr];
  72. [newArr addObject:doneArr];
  73. [newArr addObject:failArr];
  74. self->_databaseArr = newArr;
  75. [lock unlock];
  76. complete(self->_databaseArr);
  77. }];
  78. });
  79. }
  80. //把TZAssetModel 转成 我们需要上传的model
  81. - (void)handlTZAssetModelToUploadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
  82. {
  83. if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
  84. complete(nil);
  85. return;
  86. }
  87. if(!_fileModelDataArr){
  88. _fileModelDataArr = [NSMutableArray new];
  89. }
  90. _isNewAddTaskType = YES;
  91. self.curUploadModelNumbers = indexPathsForSelectedItems.count;
  92. NSMutableArray *newUploadTaskArr = [NSMutableArray new];
  93. NSLock *lock = [NSLock new];
  94. for (TZAssetModel * model in indexPathsForSelectedItems) {
  95. uploadFileDataModel * curModel = [uploadFileDataModel new];
  96. curModel.asset = model.asset;
  97. curModel.localIdentifier = model.asset.localIdentifier;
  98. BOOL isRepeatingTasksType = NO;
  99. [lock lock];
  100. //查询上传任务是否存在
  101. if(_databaseArr && _databaseArr.count == 3 ){
  102. NSMutableArray *uploadingArr = [[NSMutableArray alloc] initWithArray:_databaseArr[0]];
  103. for (uploadFileDataModel *preModel in uploadingArr) {
  104. if([preModel.localIdentifier isEqualToString:curModel.localIdentifier]){
  105. isRepeatingTasksType = YES;
  106. break;
  107. }
  108. }
  109. }
  110. [lock unlock];
  111. if(isRepeatingTasksType){
  112. continue;
  113. }
  114. curModel.imageData = model.imageData;
  115. curModel.videoData = model.videoData;
  116. curModel.filename = [model.asset valueForKey:@"filename"];
  117. //curModel.curUploadStateType = uploadStateWait;
  118. curModel.curUploadStateType = uploadStateUploading;
  119. if(model.type == TZAssetModelMediaTypeVideo){
  120. curModel.curUploadFileType = uploadFileTypeVideo;
  121. [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
  122. //curModel.totalBytes = [model.videoData length];
  123. curModel.totalBytes = model.totalBytes;
  124. curModel.videoData = [NSData new];//视频文件存储到文件后内存清空
  125. NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
  126. curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
  127. //第一帧图片
  128. [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  129. // 直接得到最终的 NSData 数据
  130. if (imageData) {
  131. curModel.imageData = imageData;
  132. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];;
  133. }
  134. }];
  135. }
  136. else{
  137. curModel.curUploadFileType = uploadFileTypeImage;
  138. curModel.totalBytes = model.totalBytes;
  139. if(curModel.imageData)
  140. {
  141. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
  142. }
  143. else{
  144. // [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  145. // // 直接得到最终的 NSData 数据
  146. // if (imageData) {
  147. // curModel.imageData = imageData;
  148. // [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];;
  149. // }
  150. // }];
  151. }
  152. }
  153. //保存到数据库
  154. curModel.bg_tableName = upLoadFile_image_tableName;
  155. [newUploadTaskArr addObject:curModel];
  156. }
  157. KWeakSelf
  158. if(newUploadTaskArr.count == 0){
  159. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  160. complete(Arr);
  161. [weakSelf handleUploadFileModelBg_idFun:Arr];
  162. }];
  163. }
  164. else{
  165. [uploadFileDataModel bg_saveOrUpdateArrayAsync:newUploadTaskArr complete:^(BOOL isSuccess) {
  166. [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  167. complete(Arr);
  168. [weakSelf handleUploadFileModelBg_idFun:Arr];
  169. }];
  170. }];
  171. }
  172. }
  173. #pragma mark 处理当前的model 加上bg_id
  174. - (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
  175. {
  176. if(!totalArr || totalArr.count != 3){
  177. return;
  178. }
  179. _fileModelDataArr = totalArr[0];
  180. [self beginUploadFileFun];
  181. }
  182. - (void)beginUploadFileFun
  183. {
  184. // if(_isUploadIngType && _reUploadIngSelectIndex <= 0){
  185. // return;
  186. // }
  187. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular)];
  188. if(!isCanUseCellular){//不允许流量上传
  189. //
  190. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  191. mainBlock(^{
  192. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendAllNotification object:nil];
  193. [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
  194. });
  195. return;
  196. }
  197. }
  198. if(_isSuspendType)
  199. {
  200. return;
  201. }
  202. _isUploadIngType = YES;
  203. _curUploadFileDataModel.imageData = nil;
  204. _curUploadFileDataModel.videoData = nil;
  205. _curUploadFileDataModel = nil;
  206. if(_reUploadIngSelectIndex > 0 && _reUploadIngSelectIndex < _fileModelDataArr.count){
  207. _curUploadFileDataModel = _fileModelDataArr[_reUploadIngSelectIndex];
  208. }
  209. else
  210. {
  211. _curUploadFileDataModel = _fileModelDataArr.firstObject;
  212. //_curUploadFileDataModel = _fileModelDataArr.lastObject;
  213. }
  214. //做个异常处理
  215. if(_databaseArr.count == 3 && _isNewAddTaskType){
  216. NSMutableArray *uploadIngArr = _databaseArr[0];
  217. if(_fileModelDataArr.count != uploadIngArr.count){
  218. _databaseArr[0] = _fileModelDataArr;
  219. }
  220. }
  221. if(!_curUploadFileDataModel){
  222. return;
  223. }
  224. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage){
  225. NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:_curUploadFileDataModel.curUploadFileType];
  226. _curUploadFileDataModel.imageData = [NSData dataWithContentsOfFile:pathStr];
  227. if(_curUploadFileDataModel.imageData && _curUploadFileDataModel.imageData.length >0){
  228. if(_curUploadFileDataModel.totalBytes == 0){
  229. _curUploadFileDataModel.totalBytes = _curUploadFileDataModel.imageData.length;
  230. }
  231. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  232. return;
  233. }
  234. }
  235. else{
  236. //NSString*pathStr = [cachesFileManager getFilePathWithName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
  237. if([cachesFileManager checkFileIsSaveState:_curUploadFileDataModel.filename withType:uploadFileTypeVideo]){
  238. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  239. return;
  240. }
  241. }
  242. if(!_curUploadFileDataModel.asset){
  243. NSString *curLocalIdentifier = _curUploadFileDataModel.localIdentifier;
  244. PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[curLocalIdentifier] options:nil];
  245. PHAsset *asset = fetchResult.firstObject;
  246. _curUploadFileDataModel.asset = asset;
  247. }
  248. if(!_curUploadFileDataModel.asset){
  249. [self getDataWrongToChangeFailFun];
  250. return;
  251. }
  252. KWeakSelf
  253. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeImage)
  254. {
  255. if(!_curUploadFileDataModel.imageData || _curUploadFileDataModel.imageData.length == 0){
  256. [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  257. // 直接得到最终的 NSData 数据
  258. if (imageData) {
  259. self->_curUploadFileDataModel.imageData = imageData;
  260. [weakSelf afterGetImageDataFun];
  261. }
  262. else{
  263. [weakSelf getDataWrongToChangeFailFun];
  264. }
  265. }];
  266. }
  267. }
  268. else if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
  269. //第一帧图片
  270. [[PHImageManager defaultManager] requestImageDataForAsset:_curUploadFileDataModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  271. // 直接得到最终的 NSData 数据
  272. if (imageData) {
  273. self->_curUploadFileDataModel.imageData = imageData;
  274. [weakSelf afterGetImageDataInVideoFun];
  275. }
  276. }];
  277. //真正的视频数据
  278. PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
  279. options.version = PHVideoRequestOptionsVersionOriginal;
  280. [[PHImageManager defaultManager] requestAVAssetForVideo:_curUploadFileDataModel.asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
  281. if ([asset isKindOfClass:[AVURLAsset class]]) {
  282. AVURLAsset* urlAsset = (AVURLAsset*)asset;
  283. BOOL isSuc = [cachesFileManager copyVideoItemAtPath:[urlAsset.URL path] fileName:_curUploadFileDataModel.filename error:nil];
  284. //NSData *videoData = [NSData dataWithContentsOfURL:urlAsset.URL];
  285. if (isSuc) {
  286. //self->_curUploadFileDataModel.videoData = videoData;
  287. [weakSelf afterGetVideoDataFun];
  288. }
  289. else{
  290. [weakSelf getDataWrongToChangeFailFun];
  291. }
  292. }
  293. else{
  294. [weakSelf getDataWrongToChangeFailFun];
  295. }
  296. }];
  297. }
  298. }
  299. #pragma mark 获取数据失败 报错
  300. - (void)getDataWrongToChangeFailFun
  301. {
  302. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  303. }];
  304. }
  305. #pragma mark 根据 asset 获取到图片数据
  306. - (void)afterGetImageDataFun
  307. {
  308. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.filename type:uploadFileTypeImage];
  309. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  310. }
  311. - (void)afterGetImageDataInVideoFun
  312. {
  313. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.imageData fileName:_curUploadFileDataModel.videoFirstImageName type:uploadFileTypeImage];
  314. }
  315. - (void)afterGetVideoDataFun
  316. {
  317. [cachesFileManager getFileNameWithContent:_curUploadFileDataModel.videoData fileName:_curUploadFileDataModel.filename type:uploadFileTypeVideo];
  318. _curUploadFileDataModel.videoData = nil;
  319. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  320. }
  321. //修改文件上传的状态
  322. - (void)changeUploadFileState:(uploadStateType)curUploadStateType withDidUploadBytes:(long)didUpLoadBytes withModel:(uploadFileDataModel*)model complete:(custom_complete_B)complete
  323. {
  324. if(model.bg_id.integerValue != _curUploadFileDataModel.bg_id.integerValue){
  325. return;
  326. }
  327. self.taskRenewTime = [iTools getNowTimeStamp];
  328. _curUploadFileDataModel.curUploadStateType = curUploadStateType;
  329. _curUploadFileDataModel.didUploadBytes = didUpLoadBytes;
  330. if(curUploadStateType == uploadStateDone){
  331. NSString *uploadDefaultPath = [HWDataManager getStringWithKey:stringKeyAddSn(Const_photo_upload_default_path)];
  332. if([uploadDefaultPath containsString:@"mnt/media_rw"]){
  333. uploadDefaultPath = [uploadDefaultPath stringByReplacingOccurrencesOfString:@"mnt/media_rw" withString:NSLocalizedString(@"disk_Extra_default_tip",nil)];
  334. }
  335. else if([uploadDefaultPath containsString:@"storage/emulated/0"]){
  336. uploadDefaultPath = [uploadDefaultPath stringByReplacingOccurrencesOfString:@"storage/emulated/0" withString:NSLocalizedString(@"disk_phone_default_tip",nil)];
  337. }
  338. else if([uploadDefaultPath containsString:@"sdcard"]){
  339. uploadDefaultPath = [uploadDefaultPath stringByReplacingOccurrencesOfString:@"sdcard" withString:NSLocalizedString(@"disk_phone_default_tip",nil)];
  340. }
  341. _curUploadFileDataModel.savePath = uploadDefaultPath;
  342. _curUploadFileDataModel.videoData = [NSData new];
  343. if(_curUploadFileDataModel.curUploadFileType == uploadFileTypeVideo){
  344. [cachesFileManager removeItemAtPath:_curUploadFileDataModel.filename type:uploadFileTypeVideo error:nil];
  345. }
  346. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  347. [self handleDatabaseArrDeteleInUploading:_curUploadFileDataModel];
  348. [self handleDatabaseArrByInDone:_curUploadFileDataModel];
  349. }
  350. else if(curUploadStateType == uploadStateFail){
  351. [self->_fileModelDataArr removeObject:self->_curUploadFileDataModel];
  352. [self handleDatabaseArrDeteleInUploading:_curUploadFileDataModel];
  353. [self handleDatabaseArrByInFail:_curUploadFileDataModel];
  354. }
  355. [_curUploadFileDataModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  356. //HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  357. }];
  358. if(!_isSuspendType || curUploadStateType != uploadStateSuspend){
  359. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:model];
  360. }
  361. complete(YES);
  362. return;
  363. // NSString* where = nil;
  364. //
  365. // where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(model.bg_id)];
  366. // //HLog(@"ffff:%@",_curUploadFileDataModel.bg_id);
  367. //
  368. // KWeakSelf
  369. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  370. // [uploadFileDataModel bg_findAsync:upLoadFile_image_tableName where:where complete:^(NSArray * _Nullable array) {
  371. // for (uploadFileDataModel * curModel in array) {
  372. // curModel.curUploadStateType = curUploadStateType;
  373. // curModel.didUploadBytes = didUpLoadBytes;
  374. //
  375. // if(curUploadStateType == uploadStateDone){
  376. // curModel.videoData = [NSData new];
  377. //
  378. // if(curModel.curUploadFileType == uploadFileTypeVideo){
  379. // [cachesFileManager removeItemAtPath:curModel.filename type:uploadFileTypeVideo error:nil];
  380. // }
  381. //
  382. // [weakSelf handleDatabaseArrByDelete:curModel];
  383. // [weakSelf handleDatabaseArrByInDone:curModel];
  384. // }
  385. // else if(curUploadStateType == uploadStateFail){
  386. // [weakSelf handleDatabaseArrByDelete:curModel];
  387. // [weakSelf handleDatabaseArrByInFail:curModel];
  388. // }
  389. //
  390. // [curModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  391. // //HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  392. // }];
  393. //
  394. // }
  395. //
  396. // complete(YES);
  397. // }];
  398. // });
  399. //
  400. }
  401. - (void)handleDatabaseArrByDelete:(uploadFileDataModel*)delModel
  402. {
  403. if(_databaseArr && _databaseArr.count == 3 ){
  404. NSMutableArray *uploadingArr = nil;
  405. if(delModel.curUploadStateType == uploadStateUploading
  406. ||delModel.curUploadStateType == uploadStateSuspend){
  407. uploadingArr = _databaseArr[0];
  408. }
  409. else if(delModel.curUploadStateType == uploadStateDone)
  410. {
  411. uploadingArr = _databaseArr[1];
  412. }
  413. else{
  414. uploadingArr = _databaseArr[2];
  415. }
  416. [uploadingArr removeObject:delModel];
  417. }
  418. }
  419. - (void)handleDatabaseArrDeteleInUploading:(uploadFileDataModel*)uploadmodel
  420. {
  421. NSLock *lock = [NSLock new];
  422. [lock lock];
  423. if(!uploadmodel){
  424. [lock unlock];
  425. return;
  426. }
  427. if(_databaseArr && _databaseArr.count == 3 ){
  428. NSMutableArray *uploadingArr = _databaseArr[0];
  429. NSInteger count = uploadingArr.count;
  430. [uploadingArr removeObject:uploadmodel];
  431. NSInteger atferDelCount = uploadingArr.count;
  432. if(count == atferDelCount){
  433. for (uploadFileDataModel*model in uploadingArr) {
  434. if(model.bg_id.integerValue == uploadmodel.bg_id.integerValue){
  435. [uploadingArr removeObject:model];
  436. break;
  437. }
  438. }
  439. }
  440. NSInteger atferDelCount2 = uploadingArr.count;
  441. if(count == atferDelCount2){
  442. //HLog(@"\n\n\nerror:上传中的任务没有删除掉database\n\n\n\n");
  443. }
  444. }
  445. [lock unlock];
  446. }
  447. - (void)handleDatabaseArrByInDone:(uploadFileDataModel*)uploadmodel
  448. {
  449. NSLock *lock = [NSLock new];
  450. [lock lock];
  451. if(_databaseArr && _databaseArr.count == 3 ){
  452. NSMutableArray *uploadDoneArr = _databaseArr[1];
  453. if(uploadmodel){
  454. [uploadDoneArr insertObject:uploadmodel atIndex:0];
  455. }
  456. }
  457. [lock unlock];
  458. }
  459. - (void)handleDatabaseArrByInFail:(uploadFileDataModel*)uploadmodel
  460. {
  461. NSLock *lock = [NSLock new];
  462. [lock lock];
  463. if(_databaseArr && _databaseArr.count == 3 ){
  464. NSMutableArray *uploadFailArr = _databaseArr[2];
  465. if(uploadmodel){
  466. [uploadFailArr insertObject:uploadmodel atIndex:0];
  467. }
  468. }
  469. [lock unlock];
  470. }
  471. - (void)handleDatabaseArrAddModelInUploading:(uploadFileDataModel*)uploadmodel
  472. {
  473. NSLock *lock = [NSLock new];
  474. [lock lock];
  475. if(_databaseArr && _databaseArr.count == 3 ){
  476. NSMutableArray *uploadlingArr = _databaseArr[0];
  477. if(uploadmodel){
  478. [uploadlingArr insertObject:uploadmodel atIndex:0];
  479. }
  480. }
  481. [lock unlock];
  482. }
  483. //暂停上传完成
  484. - (void)suspendUploadFileFun:(BOOL)isSuspendAll
  485. {
  486. if(!_fileModelDataArr || !_curUploadFileDataModel){
  487. return;
  488. }
  489. if(isSuspendAll){
  490. for (uploadFileDataModel*model in _fileModelDataArr) {
  491. model.curUploadStateType = uploadStateSuspend;
  492. }
  493. }
  494. _isSuspendType = YES;
  495. _isUploadIngType = NO;
  496. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
  497. NSEnumerator *curArr = [_fileModelDataArr reverseObjectEnumerator];
  498. for (uploadFileDataModel*model in curArr) {
  499. [self changeUploadFileState:uploadStateSuspend withDidUploadBytes:model.didUploadBytes withModel:model complete:^(BOOL isSuccess) {
  500. }];
  501. }
  502. }
  503. //某个文件重新上传
  504. - (void)reUploadFileFunBy:(NSMutableArray*)Arr withAll:(BOOL)isAllType
  505. {
  506. if(!_fileModelDataArr){
  507. _fileModelDataArr = [NSMutableArray new];
  508. }
  509. for (uploadFileDataModel*model in Arr) {
  510. model.curUploadStateType = uploadStateUploading;
  511. }
  512. _isSuspendType = NO;
  513. if(isAllType)
  514. {
  515. _isNewAddTaskType = YES;
  516. _fileModelDataArr = Arr;
  517. _reUploadIngSelectIndex = -1;
  518. [self beginUploadFileFun];
  519. return;
  520. }
  521. _isNewAddTaskType = NO;
  522. _reUploadIngSelectIndex = 0;
  523. NSMutableArray *curArr = [NSMutableArray arrayWithArray:Arr];
  524. _fileModelDataArr = curArr;
  525. // for (uploadFileDataModel*addModel in curArr) {
  526. //
  527. // BOOL needAddModel = YES;
  528. //
  529. // //for (uploadFileDataModel*preModel in _fileModelDataArr)
  530. // for (int i=0;i< _fileModelDataArr.count;i++)
  531. // {
  532. // uploadFileDataModel*preModel = _fileModelDataArr[i];
  533. //
  534. // if(addModel.bg_id.integerValue == preModel.bg_id.integerValue
  535. // || [addModel.localIdentifier isEqualToString:preModel.localIdentifier])
  536. // {
  537. // needAddModel = NO;
  538. //
  539. // if(_reUploadIngSelectIndex == 0){
  540. // _reUploadIngSelectIndex = i;
  541. //
  542. // if(i==0){
  543. // _reUploadIngSelectIndex = -1;
  544. // }
  545. // }
  546. //
  547. // break;
  548. // }
  549. // }
  550. //
  551. // if(needAddModel){
  552. // [_fileModelDataArr addObject:addModel];
  553. // [self handleDatabaseArrAddModelInUploading:addModel];
  554. // }
  555. // }
  556. //[_fileModelDataArr addObjectsFromArray:Arr];
  557. [self beginUploadFileFun];
  558. }
  559. - (void)uploadFileDoneFun
  560. {
  561. long totalSizeByte = _curUploadFileDataModel.totalBytes;
  562. [self changeUploadFileState:uploadStateDone withDidUploadBytes:totalSizeByte withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  563. self->_isUploadIngType = NO;
  564. if(self->_fileModelDataArr.count > 0){
  565. [self beginUploadFileFun];
  566. }
  567. else{
  568. [self didUploadAllTaskDoneFun];
  569. }
  570. }];
  571. }
  572. //文件上传失败
  573. - (void)uploadFileFailFun
  574. {
  575. [self changeUploadFileState:uploadStateFail withDidUploadBytes:_curUploadFileDataModel.didUploadBytes withModel:_curUploadFileDataModel complete:^(BOOL isSuccess) {
  576. if(self->_fileModelDataArr.count > 0){
  577. [self beginUploadFileFun];
  578. }
  579. else{
  580. self->_isUploadIngType = NO;
  581. self->_curUploadFileDataModel = nil;
  582. [self didUploadAllTaskDoneFun];
  583. }
  584. }];
  585. mainBlock(^{
  586. //[[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  587. });
  588. }
  589. //删除本地数据库记录
  590. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache complete:(custom_complete_B)complete
  591. {
  592. //逻辑待优化
  593. BOOL isSuc = NO;
  594. BOOL isDelUploadingModel = NO;
  595. NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
  596. for (uploadFileDataModel *uploadFileDataMod in curDelArr) {
  597. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  598. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  599. [where appendString:curStr];
  600. isSuc = [uploadFileDataModel bg_delete:upLoadFile_image_tableName where:where];
  601. [self handleDatabaseArrByDelete:uploadFileDataMod];
  602. //删除本地图片
  603. if(isSuc && isDelCache){
  604. //判断是否可以删除本地缓存
  605. if(_databaseArr && _databaseArr.count == 3 ){
  606. NSMutableArray *uploadingArr = _databaseArr[0];
  607. NSMutableArray *uploadDoneArr = _databaseArr[1];
  608. NSMutableArray *uploadFailArr = _databaseArr[2];
  609. BOOL isNeedDel = YES;
  610. for (uploadFileDataModel *baseUploadFileDataMod in uploadingArr) {
  611. if([_curUploadFileDataModel.filename isEqualToString:baseUploadFileDataMod.filename] ){
  612. isNeedDel = NO;
  613. break;
  614. }
  615. }
  616. if(!isNeedDel){
  617. for (uploadFileDataModel *baseUploadFileDataMod in uploadDoneArr) {
  618. if([_curUploadFileDataModel.filename isEqualToString:baseUploadFileDataMod.filename] ){
  619. isNeedDel = NO;
  620. break;
  621. }
  622. }
  623. }
  624. if(!isNeedDel){
  625. for (uploadFileDataModel *baseUploadFileDataMod in uploadFailArr) {
  626. if([_curUploadFileDataModel.filename isEqualToString:baseUploadFileDataMod.filename] ){
  627. isNeedDel = NO;
  628. break;
  629. }
  630. }
  631. }
  632. if(isNeedDel){
  633. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  634. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  635. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  636. }
  637. else{
  638. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  639. }
  640. }
  641. }
  642. }
  643. if(isSuc && !isDelCache){//上传报云机已经存在文件了 查询本地是否有任务记录
  644. [self handleRetryUploadAndDelRecordFun:uploadFileDataMod];
  645. }
  646. //是否在删除上传中的任务
  647. if(_fileModelDataArr && _fileModelDataArr.count >0)
  648. {
  649. for (uploadFileDataModel *curUploadFileDataMod in _fileModelDataArr)
  650. {
  651. if(curUploadFileDataMod.bg_id.integerValue == uploadFileDataMod.bg_id.integerValue){
  652. [_fileModelDataArr removeObject:curUploadFileDataMod];
  653. //[self handleDatabaseArrDeteleInUploading:curUploadFileDataMod];
  654. break;
  655. }
  656. }
  657. }
  658. if(!isDelUploadingModel){
  659. if(_curUploadFileDataModel.bg_id.integerValue == uploadFileDataMod.bg_id.integerValue){
  660. isDelUploadingModel = YES;
  661. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileSuspendNotification object:nil];
  662. }
  663. }
  664. }
  665. if(isDelUploadingModel){
  666. _isUploadIngType = NO;
  667. if(_fileModelDataArr.count >=1){
  668. [self beginUploadFileFun];
  669. }
  670. else{
  671. //_curUploadFileDataModel = nil;
  672. _isSuspendType = NO;
  673. _curUploadFileDataModel.curUploadStateType = uploadStateDone;
  674. }
  675. }
  676. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:nil];
  677. complete(YES);
  678. }
  679. #pragma mark 处理重复上传文件的问题
  680. - (void)handleRetryUploadAndDelRecordFun:(uploadFileDataModel *)uploadFileDataMod
  681. {
  682. NSLock *lock = [NSLock new];
  683. [lock lock];
  684. if(!_databaseArr && _databaseArr.count != 3){
  685. return;
  686. }
  687. NSMutableArray * doneArr = _databaseArr[1];
  688. NSMutableArray * failArr = _databaseArr[2];
  689. BOOL isNeedDelType = YES;
  690. for (uploadFileDataModel *doneModel in doneArr) {
  691. if([doneModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
  692. isNeedDelType = NO;
  693. break;
  694. }
  695. }
  696. if(isNeedDelType){
  697. for (uploadFileDataModel *failModel in failArr) {
  698. if([failModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
  699. isNeedDelType = NO;
  700. break;
  701. }
  702. }
  703. }
  704. if(isNeedDelType){
  705. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  706. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  707. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  708. }
  709. else{
  710. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  711. }
  712. }
  713. else{//视频文件是要删除的
  714. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  715. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  716. }
  717. }
  718. [lock unlock];
  719. }
  720. //检查是否需要重新上传
  721. - (void)checkReUploadFileFun
  722. {
  723. if(!_curUploadFileDataModel
  724. || _curUploadFileDataModel.curUploadStateType == uploadStateDone
  725. || _curUploadFileDataModel.curUploadStateType == uploadStateSuspend){
  726. return;
  727. }
  728. if(_isSuspendType){
  729. return;
  730. }
  731. NSInteger curTime = [iTools getNowTimeStamp];
  732. if(self.taskRenewTime == 0){
  733. return;
  734. }
  735. if(curTime - self.taskRenewTime < 10){
  736. return;
  737. }
  738. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileBeginNotification object:_curUploadFileDataModel];
  739. }
  740. - (void)checkHadUploadTaskWithComplete:(custom_complete_B)complete
  741. {
  742. [self getDataInDatabaseFun:NO complete:^(NSMutableArray * _Nonnull Arr) {
  743. mainBlock(^{
  744. if(!Arr || Arr.count != 3){
  745. complete(NO);
  746. }
  747. else{
  748. NSArray *firstArr = Arr[0];
  749. if(firstArr.count >0){
  750. complete(YES);
  751. }
  752. else{
  753. complete(NO);
  754. }
  755. }
  756. });
  757. }];
  758. }
  759. - (void)didUploadAllTaskDoneFun
  760. {
  761. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileAllTaskDoneNotification object:nil];
  762. }
  763. @end