uploadFileManager.m 32 KB

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