nasUploadFileManager.m 29 KB

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