downloadManager.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. //
  2. // downloadManager.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2024/1/9.
  6. //
  7. #import "downloadManager.h"
  8. #import "cachesFileManager.h"
  9. #import <sys/mount.h>
  10. #include <sys/param.h>
  11. #import "AFNetworkReachabilityManager.h"
  12. @interface downloadManager ()
  13. /**检测任务10s后 无反馈重新开始 */
  14. @property (nonatomic, assign) NSInteger taskRenewTime; //
  15. @end
  16. @implementation downloadManager
  17. static downloadManager * cur_downloadManager = nil;
  18. +(downloadManager *)shareInstance;
  19. {
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. cur_downloadManager = [[downloadManager alloc] init];
  23. });
  24. return cur_downloadManager;
  25. }
  26. - (id)init
  27. {
  28. self = [super init];
  29. if (self) {
  30. //[self initManager];
  31. }
  32. return self;
  33. }
  34. #pragma mark 读取数据库数据
  35. - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
  36. {
  37. if(_databaseArr && _databaseArr.count == 3 && !isReGet){
  38. complete(_databaseArr);
  39. return;
  40. }
  41. if(!_databaseArr)
  42. {
  43. _databaseArr = [NSMutableArray new];
  44. }
  45. //KWeakSelf
  46. //dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  47. // NSLock *lock = [NSLock new];
  48. // [lock lock];
  49. [couldPhoneFileModel bg_findAsync:download_tableName limit:0 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  50. NSMutableArray *failArr = [NSMutableArray new];
  51. NSMutableArray *doneArr = [NSMutableArray new];
  52. NSMutableArray *otherArr = [NSMutableArray new];
  53. if(array && array.count >0){
  54. for (couldPhoneFileModel * curModel in array) {
  55. if(curModel.curDownloadStateType == downloadStateFail){
  56. [failArr addObject:curModel];
  57. }
  58. else if(curModel.curDownloadStateType == downloadStateDone){
  59. [doneArr addObject:curModel];
  60. }
  61. else{
  62. [otherArr addObject:curModel];
  63. }
  64. }
  65. }
  66. NSLock *lock = [NSLock new];
  67. [lock lock];
  68. NSMutableArray *newArr = [NSMutableArray new];
  69. [newArr addObject:otherArr];
  70. [newArr addObject:doneArr];
  71. [newArr addObject:failArr];
  72. self->_databaseArr = newArr;
  73. [lock unlock];
  74. complete(self->_databaseArr);
  75. }];
  76. // [lock unlock];
  77. //});
  78. }
  79. -(void)handleCouldPhoneFileModelToDownloadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
  80. {
  81. if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
  82. complete(nil);
  83. return;
  84. }
  85. if(ksharedAppDelegate.DisabledFileTransferType){
  86. if(ksharedAppDelegate.isImageNewFor130){
  87. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
  88. }
  89. else{
  90. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
  91. }
  92. complete(_databaseArr);
  93. return;
  94. }
  95. for (couldPhoneFileModel*model in indexPathsForSelectedItems) {
  96. model.bg_tableName = download_tableName;
  97. }
  98. _isNewAddTaskType = YES;
  99. KWeakSelf
  100. //查
  101. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  102. //排重
  103. NSMutableArray *newDownArr = [weakSelf checkDownloadTaskRepeatFun:indexPathsForSelectedItems];
  104. if(newDownArr.count>0){
  105. //存
  106. [couldPhoneFileModel bg_saveOrUpdateArrayAsync:newDownArr complete:^(BOOL isSuccess) {
  107. //再查
  108. [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  109. complete(Arr);
  110. [weakSelf planToDownloadFileFun];
  111. }];
  112. }];
  113. }
  114. else{
  115. complete(Arr);
  116. [weakSelf planToDownloadFileFun];
  117. }
  118. }];
  119. }
  120. #pragma mark 检测是否有下载任务重复
  121. - (NSMutableArray*)checkDownloadTaskRepeatFun:(NSMutableArray*)indexPathsForSelectedItems
  122. {
  123. if(!_databaseArr || _databaseArr.count != 3){
  124. return indexPathsForSelectedItems;
  125. }
  126. NSMutableArray *downloadArr = _databaseArr[0];
  127. NSMutableArray *newTaskArr = [NSMutableArray new];
  128. for (couldPhoneFileModel*model in indexPathsForSelectedItems) {
  129. NSString *fileName = [model getFileNameFun];
  130. BOOL isNeedAddType = YES;
  131. for (couldPhoneFileModel* hadModel in downloadArr) {
  132. NSString *hadFileName = [hadModel getFileNameFun];
  133. if([fileName isEqualToString:hadFileName]){
  134. isNeedAddType = NO;
  135. break;
  136. }
  137. }
  138. if(isNeedAddType){
  139. [newTaskArr addObject:model];
  140. }
  141. }
  142. return newTaskArr;
  143. }
  144. - (void)planToDownloadFileFun
  145. {
  146. if(!_databaseArr || _databaseArr.count != 3){
  147. return;
  148. }
  149. _downLoadFileModelDataArr = [NSMutableArray arrayWithArray:_databaseArr[0]];
  150. [self beginDownloadFileFun];
  151. }
  152. - (BOOL)checkFreeDiskSpaceInBytesIsOK:(long)fileByte{
  153. struct statfs buf;
  154. unsigned long long freeSpace = -1;
  155. if (statfs("/var", &buf) >= 0) {
  156. freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
  157. }
  158. long space = freeSpace - fileByte;
  159. if( space/fileByte < 2.0 || space < 500*1024*1024){
  160. return NO;
  161. }
  162. return YES;
  163. }
  164. - (void)beginDownloadFileFun
  165. {
  166. if(_downLoadFileModelDataArr.count == 0)
  167. {
  168. [self didDownloadAllTaskDoneFun];
  169. return;
  170. }
  171. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
  172. if(!isCanUseCellular){//不允许流量上传
  173. //
  174. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  175. mainBlock(^{
  176. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileSuspendAllNotification object:nil];
  177. [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
  178. });
  179. return;
  180. }
  181. }
  182. if(ksharedAppDelegate.DisabledFileTransferType){
  183. if(ksharedAppDelegate.isImageNewFor130){
  184. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
  185. }
  186. else{
  187. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
  188. }
  189. return;
  190. }
  191. self.taskRenewTime = [iTools getNowTimeStamp];
  192. _isSuspendType = NO;
  193. _isDownloadIngType = YES;
  194. if(_reDownloadIngSelectIndex > 0
  195. && _reDownloadIngSelectIndex < _downLoadFileModelDataArr.count){
  196. _curDownloadFileModel = _downLoadFileModelDataArr[_reDownloadIngSelectIndex];
  197. }
  198. else
  199. {
  200. _curDownloadFileModel = _downLoadFileModelDataArr.firstObject;
  201. }
  202. if(!_curDownloadFileModel){
  203. return;
  204. }
  205. // //做个异常处理
  206. // if(_databaseArr.count == 3 && _isNewAddTaskType){
  207. // NSMutableArray *downloadIngArr = _databaseArr[0];
  208. // if(_downLoadFileModelDataArr.count != downloadIngArr.count){
  209. // _databaseArr[0] = [NSMutableArray arrayWithArray:_downLoadFileModelDataArr];
  210. // }
  211. // }
  212. if(![self checkFreeDiskSpaceInBytesIsOK:_curDownloadFileModel.length]){
  213. mainBlock(^{
  214. [[iToast makeText:NSLocalizedString(@"phone_space_Insufficient",nil)] show];
  215. });
  216. return;
  217. }
  218. _curDownloadFileModel.curDownloadStateType = downloadStateUploading;
  219. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileBeginNotification object:_curDownloadFileModel];
  220. }
  221. - (void)DownloadFileDoneOneFileFun
  222. {
  223. self.taskRenewTime = [iTools getNowTimeStamp];
  224. _curDownloadFileModel.curDownloadStateType = downloadStateDone;
  225. // NSString *fileName = [_curDownloadFileModel getFileNameFun];
  226. // [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  227. [self DownloadFileChangeingOneFileFun];
  228. if(_isSuspendType){
  229. return;
  230. }
  231. [self beginDownloadFileFun];
  232. }
  233. - (void)DownloadFileFailOneFileFun
  234. {
  235. self.taskRenewTime = [iTools getNowTimeStamp];
  236. _curDownloadFileModel.curDownloadStateType = downloadStateFail;
  237. [self DownloadFileChangeingOneFileFun];
  238. if(_isSuspendType){
  239. return;
  240. }
  241. [self beginDownloadFileFun];
  242. }
  243. - (void)DownloadFileChangeingOneFileFun
  244. {
  245. self.taskRenewTime = [iTools getNowTimeStamp];
  246. if(_curDownloadFileModel.curDownloadStateType == downloadStateDone){
  247. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  248. [self handleDatabaseArrDeleteObjectInDownloading:_curDownloadFileModel];
  249. [self handleDatabaseArrAddObjectInDone:_curDownloadFileModel];
  250. }
  251. else if(_curDownloadFileModel.curDownloadStateType == uploadStateFail){
  252. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  253. [self handleDatabaseArrDeleteObjectInDownloading:_curDownloadFileModel];
  254. [self handleDatabaseArrAddObjectInFail:_curDownloadFileModel];
  255. }
  256. [_curDownloadFileModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  257. //HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  258. }];
  259. if(!_isSuspendType || _curDownloadFileModel.curDownloadStateType != uploadStateSuspend){
  260. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  261. }
  262. }
  263. - (void)handleDatabaseArrDeleteObjectInDownloading:(couldPhoneFileModel*)model
  264. {
  265. NSLock *lock = [NSLock new];
  266. [lock lock];
  267. if(_databaseArr && _databaseArr.count == 3 ){
  268. NSMutableArray *ingArr = _databaseArr[0];
  269. NSInteger count = ingArr.count;
  270. [ingArr removeObject:model];
  271. NSInteger atferDelCount = ingArr.count;
  272. if(count == atferDelCount){
  273. for (couldPhoneFileModel*preModel in ingArr) {
  274. if(preModel.bg_id.integerValue == model.bg_id.integerValue){
  275. [ingArr removeObject:preModel];
  276. break;
  277. }
  278. }
  279. }
  280. NSInteger atferDelCount2 = ingArr.count;
  281. if(count == atferDelCount2){
  282. // HLog(@"\n\n\nerror:下载中的任务没有删除掉database bgid:%@\n\n\n\n",model.bg_id);
  283. }
  284. }
  285. [lock unlock];
  286. }
  287. - (void)handleDatabaseArrAddObjectInDone:(couldPhoneFileModel*)model
  288. {
  289. NSLock *lock = [NSLock new];
  290. [lock lock];
  291. if(_databaseArr && _databaseArr.count == 3 ){
  292. NSMutableArray *doneArr = _databaseArr[1];
  293. if(model){
  294. [doneArr insertObject:model atIndex:0];
  295. }
  296. }
  297. [lock unlock];
  298. }
  299. - (void)handleDatabaseArrAddObjectInFail:(couldPhoneFileModel*)model
  300. {
  301. NSLock *lock = [NSLock new];
  302. [lock lock];
  303. if(_databaseArr && _databaseArr.count == 3 ){
  304. NSMutableArray *failArr = _databaseArr[2];
  305. if(model){
  306. [failArr insertObject:model atIndex:0];
  307. }
  308. }
  309. [lock unlock];
  310. }
  311. - (void)suspendDownloadFileFun:(BOOL)isSuspendAll withModel:(couldPhoneFileModel*)model
  312. {
  313. if(isSuspendAll){
  314. _isSuspendType = YES;
  315. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileSuspendAllNotification object:nil];
  316. // for (couldPhoneFileModel *couldPhoneFileMod in _downLoadFileModelDataArr) {
  317. // couldPhoneFileMod.curDownloadStateType = downloadStateSuspend;
  318. // }
  319. [self changeDownloadState:downloadStateSuspend withModelArr:_downLoadFileModelDataArr complete:^(BOOL isSuccess) {
  320. [self->_downLoadFileModelDataArr removeAllObjects];
  321. }];
  322. }
  323. else{
  324. couldPhoneFileModel *curModel = _curDownloadFileModel;
  325. if(model){
  326. curModel = model;
  327. }
  328. else{
  329. _isSuspendType = YES;
  330. }
  331. [self changeDownloadState:downloadStateSuspend withModelArr:@[curModel] complete:^(BOOL isSuccess) {
  332. [self->_downLoadFileModelDataArr removeObject:model];
  333. if(curModel.bg_id.integerValue == self->_curDownloadFileModel.bg_id.integerValue){
  334. //开启下一个任务
  335. _reDownloadIngSelectIndex = -1;
  336. [self beginDownloadFileFun];
  337. }
  338. }];
  339. }
  340. }
  341. - (void)reDownloadFileFunBy:(NSMutableArray*)Arr withAll:(BOOL)isAllType
  342. {
  343. if(!_downLoadFileModelDataArr){
  344. _downLoadFileModelDataArr = [NSMutableArray new];
  345. }
  346. if(isAllType)
  347. {
  348. _isNewAddTaskType = YES;
  349. _downLoadFileModelDataArr = [NSMutableArray arrayWithArray:Arr];
  350. _reDownloadIngSelectIndex = -1;
  351. }
  352. else{
  353. _isNewAddTaskType = NO;
  354. _reDownloadIngSelectIndex = 0;
  355. NSMutableArray *curArr = [NSMutableArray arrayWithArray:Arr];
  356. for (uploadFileDataModel*addModel in curArr){
  357. [_downLoadFileModelDataArr insertObject:addModel atIndex:0];
  358. }
  359. }
  360. [self changeDownloadState:downloadStateUploading withModelArr:_downLoadFileModelDataArr complete:^(BOOL isSuccess) {
  361. }];
  362. [self beginDownloadFileFun];
  363. }
  364. - (void)handleDatabaseArrByDelete:(couldPhoneFileModel*)delModel
  365. {
  366. if(_databaseArr && _databaseArr.count == 3 ){
  367. if(delModel.curDownloadStateType == downloadStateDone)
  368. {
  369. NSMutableArray *arr = _databaseArr[1];
  370. [arr removeObject:delModel];
  371. }
  372. else if(delModel.curDownloadStateType == downloadStateFail){
  373. NSMutableArray *arr = _databaseArr[2];
  374. [arr removeObject:delModel];
  375. }
  376. else{
  377. NSMutableArray *arr = _databaseArr[0];
  378. [arr removeObject:delModel];
  379. }
  380. }
  381. }
  382. - (void)deleteDownloadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache
  383. {
  384. //逻辑待优化
  385. BOOL isSuc = false;
  386. BOOL isDelUploadingModel = false;
  387. NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
  388. for (couldPhoneFileModel *couldPhoneFileMod in curDelArr) {
  389. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  390. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(couldPhoneFileMod.bg_id)];
  391. [where appendString:curStr];
  392. [couldPhoneFileModel bg_deleteAsync:download_tableName where:where complete:^(BOOL isSuccess) {
  393. }];
  394. //isSuc = [couldPhoneFileModel bg_delete:download_tableName where:where];
  395. [self handleDatabaseArrByDelete:couldPhoneFileMod];
  396. if(couldPhoneFileMod.bg_id.integerValue == _curDownloadFileModel.bg_id.integerValue){
  397. isDelUploadingModel = YES;
  398. [self suspendDownloadFileFun:NO withModel:_curDownloadFileModel];
  399. }
  400. }
  401. if(isDelUploadingModel){
  402. _isDownloadIngType = NO;
  403. if(_downLoadFileModelDataArr > 0){
  404. [self beginDownloadFileFun];
  405. }
  406. }
  407. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  408. }
  409. //修改文件上传的状态
  410. - (void)changeDownloadState:(downloadStateType)curDownloadStateType withModelArr:(NSArray*)modelArr complete:(custom_complete_B)complete
  411. {
  412. for (couldPhoneFileModel * model in modelArr) {
  413. model.curDownloadStateType = curDownloadStateType;
  414. }
  415. if(modelArr.count == 0){
  416. return;
  417. }
  418. [couldPhoneFileModel bg_saveOrUpdateArrayAsync:modelArr complete:^(BOOL isSuccess) {
  419. complete(YES);
  420. }];
  421. }
  422. //检查是否需要重新下载
  423. - (void)checkReDownloadFileFun
  424. {
  425. if(!_curDownloadFileModel){
  426. return;
  427. }
  428. if(_isSuspendType){
  429. return;
  430. }
  431. NSInteger curTime = [iTools getNowTimeStamp];
  432. if(self.taskRenewTime == 0){
  433. return;
  434. }
  435. if(curTime - self.taskRenewTime < 10){
  436. return;
  437. }
  438. if(![self checkFreeDiskSpaceInBytesIsOK:_curDownloadFileModel.length]){
  439. mainBlock(^{
  440. [[iToast makeText:NSLocalizedString(@"phone_space_Insufficient",nil)] show];
  441. });
  442. _curDownloadFileModel = nil;
  443. return;
  444. }
  445. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileBeginNotification object:_curDownloadFileModel];
  446. }
  447. - (BOOL)checkDownloadingFun
  448. {
  449. if(!_curDownloadFileModel){
  450. return NO;
  451. }
  452. // if(_curDownloadFileModel.curDownloadStateType == downloadStateDone
  453. // ||_curDownloadFileModel.curDownloadStateType == downloadStateFail
  454. // ||_curDownloadFileModel.curDownloadStateType == downloadStateSuspend){
  455. // return NO;
  456. // }
  457. if(_isSuspendType){
  458. return NO;
  459. }
  460. return YES;
  461. }
  462. - (void)didDownloadAllTaskDoneFun
  463. {
  464. _curDownloadFileModel = nil;
  465. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileAllTaskDoneNotification object:nil];
  466. }
  467. - (void)checkHadDownloadTaskWithComplete:(custom_complete_B)complete
  468. {
  469. [self getDataInDatabaseFun:NO complete:^(NSMutableArray * _Nonnull Arr) {
  470. mainBlock(^{
  471. if(!Arr || Arr.count != 3){
  472. complete(NO);
  473. }
  474. else{
  475. NSArray *firstArr = Arr[0];
  476. if(firstArr.count >0){
  477. complete(YES);
  478. }
  479. else{
  480. complete(NO);
  481. }
  482. }
  483. });
  484. }];
  485. }
  486. @end