downloadManager.m 16 KB

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