uploadFileManager.m 34 KB

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