downloadManager.m 16 KB

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