uploadFileManager.m 33 KB

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