mixDownloadCacheManager.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // mixDownloadCacheManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/6/27.
  6. //
  7. #import "mixDownloadCacheManager.h"
  8. #import "mixDownloadManager.h"
  9. static NSMutableDictionary *_downloadList;
  10. static dispatch_semaphore_t _semaphore;
  11. @implementation mixDownloadCacheManager
  12. + (void)initialize {
  13. _semaphore = dispatch_semaphore_create(1);
  14. }
  15. #pragma mark-plist
  16. + (NSString *)getNewPlistPath {
  17. NSString *account = KMixDownloadUID;
  18. if (account.length != 0) {
  19. NSString *fileFolder = [HWDataManager documentPathForAccount:account fileFolder:KMixDownloadDirector];
  20. HLog(@"下载账号:%@", fileFolder);
  21. return [fileFolder stringByAppendingPathComponent:@"downloadInfo.plist"];;
  22. }else {
  23. HLog(@" 创建下载plist文件失败!");
  24. return @"";
  25. }
  26. }
  27. + (NSMutableDictionary *)getDownloadList {
  28. if (!_downloadList) { // 内存没有
  29. _downloadList = [[NSDictionary dictionaryWithContentsOfFile:[self getNewPlistPath]] mutableCopy]; // 本地加载
  30. if (!_downloadList) { // 本地没有,分配内存
  31. _downloadList = [NSMutableDictionary dictionary];
  32. }
  33. }
  34. return _downloadList;
  35. }
  36. + (NSString *)getFullDirector {
  37. NSString *account = KMixDownloadUID;
  38. if (account.length != 0)
  39. {
  40. NSString *fileFolder = [HWDataManager documentPathForAccount:account fileFolder:KMixDownloadDirector];
  41. return fileFolder;
  42. }else {
  43. HLog(@"创建下载plist文件失败!");
  44. return @"";
  45. }
  46. }
  47. /** 查询文件信息 */
  48. + (NSMutableDictionary *)queryFileInfoWithUrl:(NSString *)url
  49. {
  50. // 本地查找
  51. NSString *key = [[url lastPathComponent] stringByDeletingPathExtension];
  52. NSMutableDictionary *dictM = [[[self getDownloadList] objectForKey:key] mutableCopy];
  53. if (dictM) {
  54. NSString *path = [KFullDirector stringByAppendingString:dictM[@"fileName"]];
  55. [dictM setObject:path forKey:@"filePath"];
  56. HLog(@"路径:%@", path);
  57. }
  58. return dictM;
  59. }
  60. + (NSInteger)totalSizeWith:(NSString *)url {
  61. //NSNumber *size = [self queryFileInfoWithUrl:url][totalSize];
  62. return [[self queryFileInfoWithUrl:url][@"totalSize"] integerValue];
  63. }
  64. /** 增加配置信息 */
  65. + (BOOL)saveFileInfoWithDict:(NSDictionary *)dict {
  66. // 多账号下载 判断是否需要缓存记录
  67. //HLog(@"增加配置信息:%@", dict);
  68. if ([[dict allKeys] containsObject:@"fullPath"]) {
  69. NSArray *fileArray = [[dict objectForKey:@"fullPath"] pathComponents];
  70. NSString *filePathAccount = @"0";
  71. if (fileArray.count > 3) {
  72. filePathAccount = fileArray[fileArray.count - 3];
  73. }
  74. NSString *account = KMixDownloadUID;
  75. if (account.length == 0) {
  76. }
  77. if (![filePathAccount isEqualToString:account]) {
  78. HLog(@"切换账号 文件缓存账号%@ 与 当前登录账号:%@ 不一致", filePathAccount, account);
  79. return NO;
  80. }
  81. }
  82. // 线程等待 (信号量 + 1)
  83. dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
  84. NSString *key = [[dict[@"url"] lastPathComponent] stringByDeletingPathExtension];
  85. NSMutableDictionary *dictM = [self getDownloadList];
  86. [dictM setObject:dict forKey:key];
  87. BOOL flag = [dictM writeToFile:[self getNewPlistPath] atomically:YES];
  88. // 线程结束 (信号量 - 1)
  89. dispatch_semaphore_signal(_semaphore);
  90. return flag;
  91. }
  92. /** 删除配置信息 */
  93. + (BOOL)deleteFileWithUrl:(NSString *)url {
  94. // 线程等待 分配信号量 (信号量 + 1)
  95. dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
  96. // 删除缓存的文件
  97. NSString *fileFolder = [mixDownloadCacheManager getFullDirector];
  98. NSString *urlFileName = [url lastPathComponent];
  99. NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  100. NSString *fileName = decodeUrlFileName;
  101. NSString *fileURL = [fileFolder stringByAppendingPathComponent:fileName];
  102. BOOL flag = [[NSFileManager defaultManager] removeItemAtPath:fileURL error:nil];
  103. // BOOL flag = [[NSFileManager defaultManager] removeItemAtPath:dict[@"fullPath"] error:nil];
  104. // 删除plist的文件文件记录
  105. NSString *key = [[url lastPathComponent] stringByDeletingPathExtension];
  106. // NSDictionary *dict = SGDownloadList[key];
  107. [[self getDownloadList] removeObjectForKey:key];
  108. BOOL writeFlag = [[self getDownloadList] writeToFile:[self getNewPlistPath] atomically:YES];
  109. // 线程结束 释放信号量(信号量 - 1)
  110. dispatch_semaphore_signal(_semaphore);
  111. return (flag && writeFlag);
  112. }
  113. #pragma mark -
  114. + (BOOL)clearDisks {
  115. // 1.删除所有的文件下载信息关联表
  116. // 2.删除cache 下的download文件夹
  117. return [[NSFileManager defaultManager] removeItemAtPath:KFullDirector error:nil];
  118. }
  119. /** 取消所有当前下载的文件 清理内存缓存的数据 */
  120. + (BOOL)clearMemory {
  121. // 删除信息关联
  122. _downloadList = nil;
  123. return YES;
  124. }
  125. /** 取消所有当前下载的文件 删除磁盘所有的下载 清理内存缓存的数据 */
  126. + (BOOL)clearMemoryAndDisk {
  127. return ([self clearMemory] && [self clearDisks]);
  128. }
  129. @end