uploadFileManager.m 33 KB

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