mixDownloadCacheManager.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. @end