nasUploadFileManager.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. //
  2. // nasUploadFileManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/8/7.
  6. //
  7. #import "nasUploadFileManager.h"
  8. #import "nasUploadManager.h"
  9. @implementation nasUploadFileManager
  10. + (instancetype)shareInstance {
  11. static nasUploadFileManager *_instance;
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. _instance = [[self alloc] init];
  15. });
  16. return _instance;
  17. }
  18. - (id)init
  19. {
  20. self = [super init];
  21. if (self) {
  22. [self initManager];
  23. }
  24. return self;
  25. }
  26. - (void)initManager
  27. {
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadTaskFinishedNoti:) name:nasUploadTaskExeEnd object:nil];
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadTaskErrorNoti:) name:nasUploadTaskExeError object:nil];
  30. }
  31. #pragma mark 读取数据库数据
  32. - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
  33. {
  34. if(_databaseArr && _databaseArr.count == 3 && !isReGet){
  35. complete(_databaseArr);
  36. return;
  37. }
  38. if(!_databaseArr)
  39. {
  40. _databaseArr = [NSMutableArray new];
  41. }
  42. //KWeakSelf
  43. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  44. HLog(@"begin bg_findAsync");
  45. [uploadFileDataModel bg_findAsync:nasUpLoadFile_tableName limit:0 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  46. HLog(@"end bg_findAsync");
  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. NSMutableArray *newArr = [NSMutableArray new];
  69. [newArr addObject:otherArr];
  70. [newArr addObject:doneArr];
  71. [newArr addObject:failArr];
  72. self->_databaseArr = newArr;
  73. complete(self->_databaseArr);
  74. }];
  75. });
  76. }
  77. //把TZAssetModel 转成 我们需要上传的model
  78. - (void)handlTZAssetModelToUploadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
  79. {
  80. if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
  81. complete(_databaseArr);
  82. return;
  83. }
  84. // if(ksharedAppDelegate.DisabledFileTransferType){
  85. // if(ksharedAppDelegate.isImageNewFor130){
  86. // [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
  87. // }
  88. // else{
  89. // [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
  90. // }
  91. //
  92. // complete(_databaseArr);
  93. // return;
  94. // }
  95. _isNewAddTaskType = YES;
  96. //self.curUploadModelNumbers = indexPathsForSelectedItems.count;
  97. NSMutableArray *newUploadTaskArr = [NSMutableArray new];
  98. for (TZAssetModel * model in indexPathsForSelectedItems) {
  99. uploadFileDataModel * curModel = [uploadFileDataModel new];
  100. curModel.asset = model.asset;
  101. curModel.assetTimerStr = [iTools convertToBeijingTimeString:model.asset.creationDate];
  102. curModel.localIdentifier = model.asset.localIdentifier;
  103. BOOL isRepeatingTasksType = NO;
  104. //查询上传任务是否存在
  105. if(_databaseArr && _databaseArr.count == 3 ){
  106. NSMutableArray *uploadingArr = [[NSMutableArray alloc] initWithArray:_databaseArr[0]];
  107. for (uploadFileDataModel *preModel in uploadingArr) {
  108. if([preModel.localIdentifier isEqualToString:curModel.localIdentifier]){
  109. isRepeatingTasksType = YES;
  110. break;
  111. }
  112. }
  113. }
  114. if(isRepeatingTasksType){
  115. continue;
  116. }
  117. NSString * uploadDefaultPath = [HWDataManager getStringWithKey:stringKeyAddSn(Const_photo_upload_default_path)];
  118. if(!uploadDefaultPath || uploadDefaultPath.length == 0){
  119. [HWDataManager setStringWithKey:stringKeyAddSn(Const_photo_upload_default_path) value:Const_default_upload_path];
  120. uploadDefaultPath = Const_default_upload_path;
  121. }
  122. //判断最后一个是否是/
  123. NSString *lastStr= [uploadDefaultPath substringFromIndex:uploadDefaultPath.length-1];
  124. if(![lastStr isEqualToString:@"/"]){
  125. uploadDefaultPath = [[NSString alloc] initWithFormat:@"%@/",uploadDefaultPath];
  126. }
  127. //NSString*savePath = [[NSString alloc] initWithFormat:@"%@%@",uploadDefaultPath,filePath];
  128. curModel.savePath = uploadDefaultPath;
  129. // curModel.imageData = model.imageData;
  130. // curModel.videoData = model.videoData;
  131. curModel.filename = [model.asset valueForKey:@"filename"];
  132. //curModel.curUploadStateType = uploadStateWait;
  133. curModel.curUploadStateType = uploadStateUploading;
  134. if(model.type == TZAssetModelMediaTypeVideo){
  135. curModel.curUploadFileType = uploadFileTypeVideo;
  136. [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
  137. //curModel.totalBytes = [model.videoData length];
  138. curModel.totalBytes = model.totalBytes;
  139. curModel.videoData = [NSData new];//视频文件存储到文件后内存清空
  140. NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
  141. curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
  142. PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
  143. options.networkAccessAllowed = YES; // Allow downloading from iCloud
  144. options.version = PHImageRequestOptionsVersionCurrent;
  145. options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
  146. //第一帧图片
  147. [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  148. // 直接得到最终的 NSData 数据
  149. if (imageData) {
  150. curModel.imageData = imageData;
  151. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];
  152. }
  153. }];
  154. }
  155. else{
  156. curModel.curUploadFileType = uploadFileTypeImage;
  157. curModel.totalBytes = model.totalBytes;
  158. if(curModel.imageData)
  159. {
  160. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
  161. curModel.imageData = nil;
  162. }
  163. else{
  164. }
  165. }
  166. //保存到数据库
  167. curModel.bg_tableName = nasUpLoadFile_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. NSMutableArray* uploadTaskArr = [NSMutableArray arrayWithArray:totalArr[0]];
  193. //多任务同时进行模式
  194. if(uploadTaskArr.count >0){
  195. [[nasMixUploadManager shareManager] addUploadWithModels:uploadTaskArr];
  196. }
  197. }
  198. #pragma mark 上传完成
  199. - (void)uploadTaskFinishedNoti:(NSNotification *)notification
  200. {
  201. uploadFileDataModel *model = notification.object;
  202. if(!model)return;
  203. model.curUploadStateType = uploadStateDone;
  204. [self nasUploadFileChangeingOneFileFunBy:model];
  205. [self deleteCacheDataBy:model];
  206. }
  207. #pragma mark 上传失败
  208. - (void)uploadTaskErrorNoti:(NSNotification *)notification
  209. {
  210. uploadFileDataModel *model = notification.object;
  211. if(!model)return;
  212. model.curUploadStateType = uploadStateFail;
  213. [self nasUploadFileChangeingOneFileFunBy:model];
  214. [self deleteCacheDataBy:model];
  215. }
  216. #pragma mark 上传完成 清理缓存
  217. - (void)deleteCacheDataBy:(uploadFileDataModel*)uploadFileDataMod
  218. {
  219. HLog(@"上传--清理缓存")
  220. uploadFileDataMod.imageData = nil;
  221. uploadFileDataMod.videoData = nil;
  222. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  223. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  224. }
  225. else if(uploadFileDataMod.curUploadFileType == uploadFileTypeFileAPP){
  226. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeFileAPP error:nil];
  227. }
  228. uploadFileDataMod = nil;
  229. }
  230. #pragma mark 更新数据状态
  231. - (void)nasUploadFileChangeingOneFileFunBy:(uploadFileDataModel*)dataModel
  232. {
  233. //test code
  234. //return;
  235. HLog(@"更新数据状态")
  236. if(dataModel.curUploadStateType == uploadStateDone){
  237. [self handleDatabaseArrDeleteObjectInUploading:dataModel];
  238. [self handleDatabaseArrAddObjectInDone:dataModel];
  239. }
  240. else if(dataModel.curUploadStateType == uploadStateFail){
  241. if (!_databaseArr || _databaseArr.count != 3) {
  242. return;
  243. }
  244. //删除操作导致的失败
  245. NSMutableArray *ingArr = _databaseArr[0];
  246. NSInteger count = ingArr.count;
  247. if(count == 0){
  248. return;
  249. }
  250. [self handleDatabaseArrDeleteObjectInUploading:dataModel];
  251. [self handleDatabaseArrAddObjectInFail:dataModel];
  252. }
  253. else{
  254. //[cachesFileManager writeLogsWithMsg:@"nasUploadFileChangeingOneFileFunBy else"];
  255. }
  256. dataModel.bg_tableName = nasUpLoadFile_tableName;
  257. [dataModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  258. HLog(@"%@ 写入 %@", dataModel.filename, isSuccess ? @"成功":@"失败");
  259. }];
  260. // if(_databaseArr.count == 3)
  261. // {
  262. // NSMutableArray *ingArr = _databaseArr[0];
  263. // NSMutableArray* doneArr = _databaseArr[1];
  264. // NSMutableArray* failArr = _databaseArr[2];
  265. //
  266. // NSString *logstr = [[NSString alloc] initWithFormat:@"nasUploadFileChangeingOneFileFunBy ing:%ld,done:%ld,fail:%ld---total:%ld",ingArr.count,doneArr.count,failArr.count,ingArr.count + doneArr.count +failArr.count];
  267. // [cachesFileManager writeLogsWithMsg:logstr];
  268. // }
  269. if(!_isSuspendType || dataModel.curUploadStateType != uploadStateSuspend){
  270. HLog(@"nasUploadFileChangeingOneFileFunBy uploadFileRefreshNotification")
  271. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:dataModel];
  272. }
  273. }
  274. #pragma mark 删除上传中的任务
  275. - (void)handleDatabaseArrDeleteObjectInUploading:(uploadFileDataModel*)model
  276. {
  277. HLog(@"删除上传中的任务")
  278. @synchronized (self) {
  279. if(_databaseArr && _databaseArr.count == 3 ){
  280. NSMutableArray *ingArr = _databaseArr[0];
  281. NSInteger count = ingArr.count;
  282. [ingArr removeObject:model];
  283. NSInteger atferDelCount = ingArr.count;
  284. if(count == atferDelCount){
  285. // NSString* logStr = [[NSString alloc] initWithFormat:@"DeleteObjectInUploading error begin %ld--model:%@--%@",ingArr.count,model,model.filename];
  286. // HLog(@"%@",logStr)
  287. // [cachesFileManager writeLogsWithMsg:logStr];
  288. for (uploadFileDataModel*preModel in ingArr) {
  289. if(preModel.bg_id.integerValue == model.bg_id.integerValue){
  290. if (//[preModel.filename isEqualToString:model.filename]&&
  291. [preModel.localIdentifier isEqualToString:model.localIdentifier]) {
  292. [ingArr removeObject:preModel];
  293. }else{
  294. // NSString* logStr22 = [[NSString alloc] initWithFormat:@"filename error %@",preModel.filename];
  295. // [cachesFileManager writeLogsWithMsg:logStr22];
  296. }
  297. break;
  298. }
  299. }
  300. NSString* logStr2 = [[NSString alloc] initWithFormat:@"DeleteObjectInUploading error end %ld",ingArr.count];
  301. HLog(@"%@",logStr2)
  302. //[cachesFileManager writeLogsWithMsg:logStr2];
  303. }
  304. }
  305. }
  306. }
  307. #pragma mark 添加任务到上传完成
  308. - (void)handleDatabaseArrAddObjectInDone:(uploadFileDataModel*)model
  309. {
  310. HLog(@"添加任务到上传完成")
  311. @synchronized (self) {
  312. if(_databaseArr && _databaseArr.count == 3 ){
  313. NSMutableArray *doneArr = _databaseArr[1];
  314. if(model){
  315. [doneArr insertObject:model atIndex:0];
  316. }
  317. }
  318. }
  319. }
  320. #pragma mark 添加任务到上传失败
  321. - (void)handleDatabaseArrAddObjectInFail:(uploadFileDataModel*)model
  322. {
  323. HLog(@"添加任务到上传失败")
  324. @synchronized(self){
  325. if(_databaseArr && _databaseArr.count == 3 ){
  326. NSMutableArray *failArr = _databaseArr[2];
  327. if(model){
  328. [failArr insertObject:model atIndex:0];
  329. }
  330. }
  331. }
  332. }
  333. #pragma mark 上传完成所有任务
  334. - (void)didUploadAllTaskDoneFun
  335. {
  336. //self->_isUploadIngType = NO;
  337. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileAllTaskDoneNotification object:nil];
  338. }
  339. #pragma mark 删除内存数据记录
  340. - (void)handleDatabaseArrByDelete:(uploadFileDataModel*)delModel
  341. {
  342. if(_databaseArr && _databaseArr.count == 3 ){
  343. NSMutableArray *uploadingArr = nil;
  344. if(delModel.curUploadStateType == uploadStateUploading
  345. ||delModel.curUploadStateType == uploadStateSuspend
  346. ||delModel.curUploadStateType == uploadStateWait){
  347. uploadingArr = _databaseArr[0];
  348. }
  349. else if(delModel.curUploadStateType == uploadStateDone)
  350. {
  351. uploadingArr = _databaseArr[1];
  352. }
  353. else{
  354. uploadingArr = _databaseArr[2];
  355. }
  356. [uploadingArr removeObject:delModel];
  357. }
  358. }
  359. #pragma mark 删除本地数据库记录
  360. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache complete:(custom_complete_B)complete
  361. {
  362. //
  363. BOOL isDelUploadingModel = NO;
  364. NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
  365. for (uploadFileDataModel *uploadFileDataMod in curDelArr) {
  366. //1.删除数据库
  367. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  368. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  369. [where appendString:curStr];
  370. //isSuc = [uploadFileDataModel bg_delete:nasUpLoadFile_tableName where:where];
  371. [uploadFileDataModel bg_deleteAsync:nasUpLoadFile_tableName where:where complete:^(BOOL isSuccess) {
  372. if(isSuccess){
  373. }
  374. }];
  375. // 2.删除内存数据
  376. [self handleDatabaseArrByDelete:uploadFileDataMod];
  377. //3.删除缓存的本地数据
  378. if(isDelCache){
  379. //判断是否可以删除本地缓存
  380. if(_databaseArr && _databaseArr.count == 3 ){
  381. NSMutableArray *uploadingArr = _databaseArr[0];
  382. NSMutableArray *uploadDoneArr = _databaseArr[1];
  383. NSMutableArray *uploadFailArr = _databaseArr[2];
  384. BOOL isNeedDel = YES;
  385. for (uploadFileDataModel *baseUploadFileDataMod in uploadingArr) {
  386. if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
  387. isNeedDel = NO;
  388. break;
  389. }
  390. }
  391. if(isNeedDel){
  392. for (uploadFileDataModel *baseUploadFileDataMod in uploadDoneArr) {
  393. if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
  394. isNeedDel = NO;
  395. break;
  396. }
  397. }
  398. }
  399. if(isNeedDel){
  400. for (uploadFileDataModel *baseUploadFileDataMod in uploadFailArr) {
  401. if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
  402. isNeedDel = NO;
  403. break;
  404. }
  405. }
  406. }
  407. if(isNeedDel){
  408. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  409. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  410. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  411. }
  412. else{
  413. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  414. }
  415. }
  416. }
  417. }
  418. if(!isDelCache){//上传报云机已经存在文件了 查询本地是否有任务记录
  419. [self handleRetryUploadAndDelRecordFun:uploadFileDataMod];
  420. }
  421. //是否在删除上传中的任务
  422. if(uploadFileDataMod.curUploadStateType == uploadStateUploading){
  423. [[nasMixUploadManager shareManager] cancelUploadTaskFunWith:uploadFileDataMod];
  424. }
  425. }
  426. uploadFileDataModel *RefreshUploadFileDataMod = nil;
  427. if(delArr.count >= 1){
  428. RefreshUploadFileDataMod = delArr.firstObject;
  429. }
  430. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:RefreshUploadFileDataMod];
  431. complete(YES);
  432. }
  433. #pragma mark 处理重复上传文件的问题
  434. - (void)handleRetryUploadAndDelRecordFun:(uploadFileDataModel *)uploadFileDataMod
  435. {
  436. @synchronized (self) {
  437. if (!_databaseArr || _databaseArr.count != 3) {
  438. return;
  439. }
  440. NSMutableArray * doneArr = _databaseArr[1];
  441. NSMutableArray * failArr = _databaseArr[2];
  442. BOOL isNeedDelType = YES;
  443. for (uploadFileDataModel *doneModel in doneArr) {
  444. if([doneModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
  445. isNeedDelType = NO;
  446. break;
  447. }
  448. }
  449. if(isNeedDelType){
  450. for (uploadFileDataModel *failModel in failArr) {
  451. if([failModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
  452. isNeedDelType = NO;
  453. break;
  454. }
  455. }
  456. }
  457. if(isNeedDelType){
  458. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  459. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  460. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  461. }
  462. else{
  463. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  464. }
  465. }
  466. else{//视频文件是要删除的
  467. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  468. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  469. }
  470. }
  471. }
  472. }
  473. - (void)checkHadUploadTaskWithComplete:(custom_complete_B)complete
  474. {
  475. [self getDataInDatabaseFun:NO complete:^(NSMutableArray * _Nonnull Arr) {
  476. mainBlock(^{
  477. if(!Arr || Arr.count != 3){
  478. complete(NO);
  479. }
  480. else{
  481. NSArray *firstArr = Arr[0];
  482. if(firstArr.count >0){
  483. complete(YES);
  484. }
  485. else{
  486. complete(NO);
  487. }
  488. }
  489. });
  490. }];
  491. }
  492. //文件重新上传
  493. - (void)reUploadFileFunBy:(NSMutableArray*)Arr withAll:(BOOL)isAllType
  494. {
  495. if(!Arr || Arr.count == 0){
  496. return;
  497. }
  498. //1.修改状态从失败改为上传中
  499. for (uploadFileDataModel * model in Arr) {
  500. model.curUploadStateType = uploadStateUploading;
  501. //修改已经读取的内存数据
  502. NSMutableArray * failArr = _databaseArr[2];
  503. for (uploadFileDataModel * failModel in failArr) {
  504. if(model.bg_id.integerValue == failModel.bg_id.integerValue){
  505. [failArr removeObject:failModel];
  506. break;
  507. }
  508. }
  509. }
  510. //2、保存到数控库
  511. [uploadFileDataModel bg_saveOrUpdateArrayAsync:Arr complete:^(BOOL isSuccess) {
  512. }];
  513. //重新开始上传
  514. [[nasMixUploadManager shareManager] addUploadWithModels:Arr];
  515. }
  516. //暂停上传 当 isSuspendAll为NO时候 传指定暂停哪一个
  517. - (void)suspendUploadFileFun:(BOOL)isSuspendAll withModel:(uploadFileDataModel*)model
  518. {
  519. //[[nasUploadManager shareInstance] cancelUploadTaskFun];
  520. if (isSuspendAll) {
  521. [[nasMixUploadManager shareManager] cancelUploadAllTaskFun];
  522. }
  523. else{
  524. [[nasMixUploadManager shareManager] cancelUploadTaskFunWith:model];
  525. }
  526. [self suspendUploadingTaskBy:isSuspendAll withModel:model];
  527. }
  528. #pragma mark 暂停任务
  529. - (void)suspendUploadingTaskBy:(BOOL)isDeleteAll withModel:(uploadFileDataModel*)model
  530. {
  531. NSMutableArray *uploadingArr = [NSMutableArray new];
  532. if(!isDeleteAll){
  533. if(model){
  534. model.curUploadStateType = uploadStateSuspend;
  535. [uploadingArr addObject:model];
  536. }
  537. }
  538. else
  539. {
  540. if (!_databaseArr || _databaseArr.count != 3) {
  541. return;
  542. }
  543. uploadingArr = _databaseArr[0];
  544. for (uploadFileDataModel*preModel in uploadingArr) {
  545. preModel.curUploadStateType = uploadStateSuspend;
  546. }
  547. }
  548. if(uploadingArr.count > 0){
  549. //更新数据库
  550. [uploadFileDataModel bg_saveOrUpdateArrayAsync:uploadingArr complete:^(BOOL isSuccess) {
  551. }];
  552. }
  553. }
  554. - (NSMutableArray*)uploadingArr
  555. {
  556. if(_databaseArr && _databaseArr.count == 3){
  557. NSMutableArray *uploadingArr = _databaseArr[0];
  558. return uploadingArr;
  559. }
  560. return [NSMutableArray new];
  561. }
  562. //网络失败后任务暂时保存
  563. - (void)saveUploadingTaskByNetWorkErrorFun
  564. {
  565. HLog(@"hxd saveUploadingTaskByNetWorkErrorFun")
  566. if(self.uploadingArr.count > 0){
  567. [[nasMixUploadManager shareManager] cancelUploadAllTaskFun];
  568. _needToReUploadTaskType = YES;
  569. }
  570. }
  571. //重新上传网络失败的任务
  572. - (void)reUploadFileFunByNetWork
  573. {
  574. [nasUploadFileManager shareInstance].needToReUploadTaskType = NO;
  575. HLog(@"reUploadFileFunByNetWork")
  576. //[cachesFileManager writeLogsWithMsg:@"reUploadFileFunByNetWork"];
  577. if(self.uploadingArr.count >0 && _databaseArr.count == 3){
  578. // NSMutableArray* doneArr = _databaseArr[1];
  579. // NSMutableArray* failArr = _databaseArr[2];
  580. //
  581. // NSString *logstr = [[NSString alloc] initWithFormat:@"ing:%ld,done:%ld,fail:%ld",self.uploadingArr.count,doneArr.count,failArr.count];
  582. //
  583. // [cachesFileManager writeLogsWithMsg:logstr];
  584. //1.修改状态从失败改为上传中
  585. // for (uploadFileDataModel * model in _netWorkFailSaveArr) {
  586. // model.curUploadStateType = uploadStateUploading;
  587. //// for (uploadFileDataModel * uploadingModel in self.uploadingArr){
  588. //// if (model.bg_id.integerValue == uploadingModel.bg_id.integerValue) {
  589. //// uploadingModel.curUploadStateType = uploadStateUploading;
  590. //// continue;
  591. //// }
  592. //// }
  593. // }
  594. //2、保存到数控库
  595. // [uploadFileDataModel bg_saveOrUpdateArrayAsync:_netWorkFailSaveArr complete:^(BOOL isSuccess) {
  596. //
  597. // }];
  598. //3.判断是否可以上传
  599. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
  600. if(!isCanUseCellular){//不允许流量上传
  601. //
  602. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  603. return;
  604. }
  605. }
  606. //4.重新开始上传
  607. [[nasMixUploadManager shareManager] addUploadWithModels:self.uploadingArr];
  608. }
  609. }
  610. #pragma mark 文件APP里的文件上传
  611. - (void)handlUrlToUploadFileDataFunBy:(NSArray*)UrlArr complete:(custom_complete_Arr)complete
  612. {
  613. if(!UrlArr && UrlArr.count == 0){
  614. complete(_databaseArr);
  615. return;
  616. }
  617. _isNewAddTaskType = YES;
  618. NSMutableArray *newUploadTaskArr = [NSMutableArray new];
  619. for (NSURL * fileURL in UrlArr) {
  620. uploadFileDataModel * curModel = [uploadFileDataModel new];
  621. // 获取文件名称
  622. NSString *fileName = [fileURL lastPathComponent];
  623. curModel.filename = fileName;
  624. curModel.curUploadFileType = uploadFileTypeFileAPP;
  625. BOOL isRepeatingTasksType = NO;
  626. //查询上传任务是否存在
  627. if(_databaseArr && _databaseArr.count == 3 ){
  628. NSMutableArray *uploadingArr = [[NSMutableArray alloc] initWithArray:_databaseArr[0]];
  629. for (uploadFileDataModel *preModel in uploadingArr) {
  630. if([preModel.filename isEqualToString:curModel.filename]
  631. && preModel.curUploadFileType == uploadFileTypeFileAPP){
  632. isRepeatingTasksType = YES;
  633. break;
  634. }
  635. }
  636. }
  637. if(isRepeatingTasksType){
  638. continue;
  639. }
  640. NSString * uploadDefaultPath = [HWDataManager getStringWithKey:stringKeyAddSn(Const_photo_upload_default_path)];
  641. if(!uploadDefaultPath || uploadDefaultPath.length == 0){
  642. [HWDataManager setStringWithKey:stringKeyAddSn(Const_photo_upload_default_path) value:Const_default_upload_path];
  643. uploadDefaultPath = Const_default_upload_path;
  644. }
  645. //判断最后一个是否是/
  646. NSString *lastStr= [uploadDefaultPath substringFromIndex:uploadDefaultPath.length-1];
  647. if(![lastStr isEqualToString:@"/"]){
  648. uploadDefaultPath = [[NSString alloc] initWithFormat:@"%@/",uploadDefaultPath];
  649. }
  650. //NSString*savePath = [[NSString alloc] initWithFormat:@"%@%@",uploadDefaultPath,filePath];
  651. curModel.savePath = uploadDefaultPath;
  652. curModel.curUploadStateType = uploadStateUploading;
  653. NSString *documentsDirectory = kSHPath_FileAPP;
  654. // 构建目标文件路径
  655. NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:fileName];
  656. curModel.totalBytes = [iTools getFileSize:destinationPath];
  657. //保存到数据库
  658. curModel.bg_tableName = nasUpLoadFile_tableName;
  659. [newUploadTaskArr addObject:curModel];
  660. }
  661. KWeakSelf
  662. if(newUploadTaskArr.count == 0){
  663. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  664. complete(Arr);
  665. [weakSelf handleUploadFileModelBg_idFun:Arr];
  666. }];
  667. }
  668. else{
  669. [uploadFileDataModel bg_saveOrUpdateArrayAsync:newUploadTaskArr complete:^(BOOL isSuccess) {
  670. [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  671. complete(Arr);
  672. [weakSelf handleUploadFileModelBg_idFun:Arr];
  673. }];
  674. }];
  675. }
  676. }
  677. @end