downloadManager.m 16 KB

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