uploadFileManager.m 33 KB

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