downloadManager.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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:NO 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 = _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. _curDownloadFileModel = nil;
  169. [self didDownloadAllTaskDoneFun];
  170. return;
  171. }
  172. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
  173. if(!isCanUseCellular){//不允许流量上传
  174. //
  175. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  176. mainBlock(^{
  177. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileSuspendAllNotification object:nil];
  178. [[iToast makeText:NSLocalizedString(@"File_Transfer_By_Cellular_tip",nil)] show];
  179. });
  180. return;
  181. }
  182. }
  183. if(ksharedAppDelegate.DisabledFileTransferType){
  184. if(ksharedAppDelegate.isImageNewFor130){
  185. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
  186. }
  187. else{
  188. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
  189. }
  190. return;
  191. }
  192. self.taskRenewTime = [iTools getNowTimeStamp];
  193. _isSuspendType = NO;
  194. _isDownloadIngType = YES;
  195. if(_reDownloadIngSelectIndex > 0
  196. && _reDownloadIngSelectIndex < _downLoadFileModelDataArr.count){
  197. _curDownloadFileModel = _downLoadFileModelDataArr[_reDownloadIngSelectIndex];
  198. }
  199. else
  200. {
  201. _curDownloadFileModel = _downLoadFileModelDataArr.firstObject;
  202. }
  203. if(!_curDownloadFileModel){
  204. return;
  205. }
  206. //做个异常处理
  207. if(_databaseArr.count == 3 && _isNewAddTaskType){
  208. NSMutableArray *downloadIngArr = _databaseArr[0];
  209. if(_downLoadFileModelDataArr.count != downloadIngArr.count){
  210. _databaseArr[0] = _downLoadFileModelDataArr;
  211. }
  212. }
  213. if(![self checkFreeDiskSpaceInBytesIsOK:_curDownloadFileModel.length]){
  214. mainBlock(^{
  215. [[iToast makeText:NSLocalizedString(@"phone_space_Insufficient",nil)] show];
  216. });
  217. return;
  218. }
  219. _curDownloadFileModel.curDownloadStateType = downloadStateUploading;
  220. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileBeginNotification object:_curDownloadFileModel];
  221. }
  222. - (void)DownloadFileDoneOneFileFun
  223. {
  224. self.taskRenewTime = [iTools getNowTimeStamp];
  225. _curDownloadFileModel.curDownloadStateType = downloadStateDone;
  226. // NSString *fileName = [_curDownloadFileModel getFileNameFun];
  227. // [cachesFileManager removeItemAtPath:fileName type:DownLoadFileType error:nil];
  228. [self DownloadFileChangeingOneFileFun];
  229. if(_isSuspendType){
  230. return;
  231. }
  232. [self beginDownloadFileFun];
  233. }
  234. - (void)DownloadFileFailOneFileFun
  235. {
  236. self.taskRenewTime = [iTools getNowTimeStamp];
  237. _curDownloadFileModel.curDownloadStateType = downloadStateFail;
  238. [self DownloadFileChangeingOneFileFun];
  239. if(_isSuspendType){
  240. return;
  241. }
  242. [self beginDownloadFileFun];
  243. }
  244. - (void)DownloadFileChangeingOneFileFun
  245. {
  246. self.taskRenewTime = [iTools getNowTimeStamp];
  247. if(_curDownloadFileModel.curDownloadStateType == downloadStateDone){
  248. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  249. [self handleDatabaseArrDeleteObjectInDownloading:_curDownloadFileModel];
  250. [self handleDatabaseArrAddObjectInDone:_curDownloadFileModel];
  251. }
  252. else if(_curDownloadFileModel.curDownloadStateType == uploadStateFail){
  253. [_downLoadFileModelDataArr removeObject:_curDownloadFileModel];
  254. [self handleDatabaseArrDeleteObjectInDownloading:_curDownloadFileModel];
  255. [self handleDatabaseArrAddObjectInFail:_curDownloadFileModel];
  256. }
  257. [_curDownloadFileModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  258. //HLog(@"%@ 写入 %@", model.filename, isSuccess ? @"成功":@"失败");
  259. }];
  260. if(!_isSuspendType || _curDownloadFileModel.curDownloadStateType != uploadStateSuspend){
  261. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  262. }
  263. }
  264. - (void)handleDatabaseArrDeleteObjectInDownloading:(couldPhoneFileModel*)model
  265. {
  266. NSLock *lock = [NSLock new];
  267. [lock lock];
  268. if(_databaseArr && _databaseArr.count == 3 ){
  269. NSMutableArray *ingArr = _databaseArr[0];
  270. NSInteger count = ingArr.count;
  271. [ingArr removeObject:model];
  272. NSInteger atferDelCount = ingArr.count;
  273. if(count == atferDelCount){
  274. for (couldPhoneFileModel*preModel in ingArr) {
  275. if(preModel.bg_id.integerValue == model.bg_id.integerValue){
  276. [ingArr removeObject:preModel];
  277. break;
  278. }
  279. }
  280. }
  281. NSInteger atferDelCount2 = ingArr.count;
  282. if(count == atferDelCount2){
  283. // HLog(@"\n\n\nerror:下载中的任务没有删除掉database bgid:%@\n\n\n\n",model.bg_id);
  284. }
  285. }
  286. [lock unlock];
  287. }
  288. - (void)handleDatabaseArrAddObjectInDone:(couldPhoneFileModel*)model
  289. {
  290. NSLock *lock = [NSLock new];
  291. [lock lock];
  292. if(_databaseArr && _databaseArr.count == 3 ){
  293. NSMutableArray *doneArr = _databaseArr[1];
  294. if(model){
  295. [doneArr insertObject:model atIndex:0];
  296. }
  297. }
  298. [lock unlock];
  299. }
  300. - (void)handleDatabaseArrAddObjectInFail:(couldPhoneFileModel*)model
  301. {
  302. NSLock *lock = [NSLock new];
  303. [lock lock];
  304. if(_databaseArr && _databaseArr.count == 3 ){
  305. NSMutableArray *failArr = _databaseArr[2];
  306. if(model){
  307. [failArr insertObject:model atIndex:0];
  308. }
  309. }
  310. [lock unlock];
  311. }
  312. - (void)suspendDownloadFileFun:(BOOL)isSuspendAll
  313. {
  314. _isSuspendType = YES;
  315. if(isSuspendAll){
  316. for (couldPhoneFileModel *couldPhoneFileMod in _downLoadFileModelDataArr) {
  317. couldPhoneFileMod.curDownloadStateType = downloadStateSuspend;
  318. }
  319. }
  320. }
  321. - (void)reDownloadFileFunBy:(NSMutableArray*)Arr withAll:(BOOL)isAllType
  322. {
  323. if(!_downLoadFileModelDataArr){
  324. _downLoadFileModelDataArr = [NSMutableArray new];
  325. }
  326. if(isAllType)
  327. {
  328. _isNewAddTaskType = YES;
  329. _downLoadFileModelDataArr = Arr;
  330. _reDownloadIngSelectIndex = -1;
  331. [self beginDownloadFileFun];
  332. return;
  333. }
  334. _isNewAddTaskType = NO;
  335. _reDownloadIngSelectIndex = 0;
  336. NSMutableArray *curArr = [NSMutableArray arrayWithArray:Arr];
  337. _downLoadFileModelDataArr = curArr;
  338. // for (uploadFileDataModel*addModel in Arr) {
  339. //
  340. // BOOL needAddModel = YES;
  341. //
  342. // for (int i=0;i< _downLoadFileModelDataArr.count;i++)
  343. // {
  344. // couldPhoneFileModel*preModel = _downLoadFileModelDataArr[i];
  345. //
  346. // if(addModel.bg_id.integerValue == preModel.bg_id.integerValue)
  347. // {
  348. // needAddModel = NO;
  349. //
  350. // if(_reDownloadIngSelectIndex == 0){
  351. // _reDownloadIngSelectIndex = i;
  352. //
  353. // if(i==0){
  354. // _reDownloadIngSelectIndex = -1;
  355. // }
  356. // }
  357. //
  358. // break;
  359. // }
  360. // }
  361. //
  362. // if(needAddModel){
  363. // [_downLoadFileModelDataArr addObject:addModel];
  364. // _reDownloadIngSelectIndex = _downLoadFileModelDataArr.count -1;
  365. // }
  366. // }
  367. [self beginDownloadFileFun];
  368. }
  369. - (void)handleDatabaseArrByDelete:(couldPhoneFileModel*)delModel
  370. {
  371. if(_databaseArr && _databaseArr.count == 3 ){
  372. if(delModel.curDownloadStateType == downloadStateDone)
  373. {
  374. NSMutableArray *arr = _databaseArr[1];
  375. [arr removeObject:delModel];
  376. }
  377. else if(delModel.curDownloadStateType == downloadStateFail){
  378. NSMutableArray *arr = _databaseArr[2];
  379. [arr removeObject:delModel];
  380. }
  381. else{
  382. NSMutableArray *arr = _databaseArr[0];
  383. [arr removeObject:delModel];
  384. }
  385. }
  386. }
  387. - (void)deleteDownloadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache
  388. {
  389. //逻辑待优化
  390. BOOL isSuc = false;
  391. BOOL isDelUploadingModel = false;
  392. NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
  393. for (couldPhoneFileModel *couldPhoneFileMod in curDelArr) {
  394. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  395. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(couldPhoneFileMod.bg_id)];
  396. [where appendString:curStr];
  397. [couldPhoneFileModel bg_deleteAsync:download_tableName where:where complete:^(BOOL isSuccess) {
  398. }];
  399. //isSuc = [couldPhoneFileModel bg_delete:download_tableName where:where];
  400. [self handleDatabaseArrByDelete:couldPhoneFileMod];
  401. if(couldPhoneFileMod.bg_id.integerValue == _curDownloadFileModel.bg_id.integerValue){
  402. isDelUploadingModel = YES;
  403. [self suspendDownloadFileFun:NO];
  404. }
  405. }
  406. if(isDelUploadingModel){
  407. _isDownloadIngType = NO;
  408. if(_downLoadFileModelDataArr > 0){
  409. [self beginDownloadFileFun];
  410. }
  411. }
  412. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileRefreshNotification object:_curDownloadFileModel];
  413. }
  414. //检查是否需要重新下载
  415. - (void)checkReDownloadFileFun
  416. {
  417. if(!_curDownloadFileModel){
  418. return;
  419. }
  420. if(_isSuspendType){
  421. return;
  422. }
  423. NSInteger curTime = [iTools getNowTimeStamp];
  424. if(self.taskRenewTime == 0){
  425. return;
  426. }
  427. if(curTime - self.taskRenewTime < 10){
  428. return;
  429. }
  430. if(![self checkFreeDiskSpaceInBytesIsOK:_curDownloadFileModel.length]){
  431. mainBlock(^{
  432. [[iToast makeText:NSLocalizedString(@"phone_space_Insufficient",nil)] show];
  433. });
  434. _curDownloadFileModel = nil;
  435. return;
  436. }
  437. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileBeginNotification object:_curDownloadFileModel];
  438. }
  439. - (BOOL)checkDownloadingFun
  440. {
  441. if(!_curDownloadFileModel){
  442. return NO;
  443. }
  444. if(_curDownloadFileModel.curDownloadStateType == downloadStateDone
  445. ||_curDownloadFileModel.curDownloadStateType == downloadStateFail
  446. ||_curDownloadFileModel.curDownloadStateType == downloadStateSuspend){
  447. return NO;
  448. }
  449. if(_isSuspendType){
  450. return NO;
  451. }
  452. return YES;
  453. }
  454. - (void)didDownloadAllTaskDoneFun
  455. {
  456. [[NSNotificationCenter defaultCenter] postNotificationName:downloadFileAllTaskDoneNotification object:nil];
  457. }
  458. - (void)checkHadDownloadTaskWithComplete:(custom_complete_B)complete
  459. {
  460. [self getDataInDatabaseFun:NO complete:^(NSMutableArray * _Nonnull Arr) {
  461. mainBlock(^{
  462. if(!Arr || Arr.count != 3){
  463. complete(NO);
  464. }
  465. else{
  466. NSArray *firstArr = Arr[0];
  467. if(firstArr.count >0){
  468. complete(YES);
  469. }
  470. else{
  471. complete(NO);
  472. }
  473. }
  474. });
  475. }];
  476. }
  477. @end