SGCacheManager.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // SGCacheManager.m
  3. // OfflineBreakPointDownload
  4. //
  5. // Created by Shangen Zhang on 16/11/26.
  6. // Copyright © 2016年 Shangen Zhang. All rights reserved.
  7. //
  8. #import "SGCacheManager.h"
  9. #import "NSString+SGHashString.h"
  10. #import "CWFileManager.h"
  11. static NSMutableDictionary *_downloadList;
  12. static dispatch_semaphore_t _semaphore;
  13. //NSString const * filePath = @"filePath";
  14. //NSString const * fileSize = @"fileSize";
  15. //NSString const * fileName = @"fileName";
  16. //NSString const * fileUrl = @"fileUrl";
  17. //NSString const * isFinished = @"isFinished";
  18. //NSString const * totalSize = @"totalSize";
  19. #define SGDownloadInfoPath [self getNewPlistPath]
  20. #define SGDownloadList [self getDownloadList]
  21. @interface SGCacheManager ()
  22. @end
  23. @implementation SGCacheManager
  24. + (void)initialize {
  25. _semaphore = dispatch_semaphore_create(1);
  26. }
  27. #pragma mark-plist
  28. + (NSString *)getNewPlistPath {
  29. NSString *fileFolder = kPath_YunPan_Download_Folder;
  30. HLog(@"登录账号:%@", kPath_YunPan_Download_Folder);
  31. return [fileFolder stringByAppendingPathComponent:@"downloadInfo.plist"];
  32. }
  33. + (NSString *)getFullDirector {
  34. return kPath_YunPan_Download_Folder;
  35. }
  36. + (NSMutableDictionary *)getDownloadList {
  37. if (!_downloadList) { // 内存没有
  38. _downloadList = [[NSDictionary dictionaryWithContentsOfFile:SGDownloadInfoPath] mutableCopy]; // 本地加载
  39. if (!_downloadList) { // 本地没有,分配内存
  40. _downloadList = [NSMutableDictionary dictionary];
  41. }
  42. }
  43. return _downloadList;
  44. }
  45. #pragma mark - save
  46. //+ (void)didReciveDownloadCompleteNoti:(NSNotification *)noti {
  47. //
  48. // // 缓存记录
  49. // NSMutableDictionary *dictM = [noti.userInfo mutableCopy];
  50. //
  51. // // 从磁盘获取到下载了
  52. // if ([noti.object integerValue] == 2 && ([self queryFileInfoWithUrl:dictM[fileUrl]])) {
  53. // return;
  54. // }
  55. //
  56. // // 缓存记录
  57. // [dictM setObject:@(YES) forKey:isFinished];
  58. // NSString *key = [dictM[fileUrl] sg_md5HashString];
  59. //
  60. // [SGDownloadList setObject:dictM forKey:key];
  61. //
  62. // [SGDownloadList writeToFile:SGDownloadInfoPath atomically:YES];
  63. //
  64. //}
  65. #pragma mark - query
  66. + (NSDictionary *)queryFileInfoWithUrl:(NSString *)url {
  67. // 本地查找
  68. NSString *key = [[url lastPathComponent] stringByDeletingPathExtension];
  69. NSMutableDictionary *dictM = [[SGDownloadList objectForKey:key] mutableCopy];
  70. if (dictM) {
  71. NSString *path = [KFullDirector stringByAppendingString:dictM[@"fileName"]];
  72. [dictM setObject:path forKey:@"filePath"];
  73. HLog(@"路径:%@", path);
  74. }
  75. return dictM;
  76. }
  77. + (NSInteger)totalSizeWith:(NSString *)url {
  78. //NSNumber *size = [self queryFileInfoWithUrl:url][totalSize];
  79. return [[self queryFileInfoWithUrl:url][@"totalSize"] integerValue];
  80. }
  81. /** 记录要下载的文件大小 */
  82. + (BOOL)saveTotalSizeWithSize:(NSInteger)size forURL:(NSString *)url {
  83. return YES;
  84. }
  85. /** 增加配置信息 */
  86. + (BOOL)saveFileInfoWithDict:(NSDictionary *)dict {
  87. // 多账号下载 判断是否需要缓存记录
  88. //HLog(@"%@", dict);
  89. if ([[dict allKeys] containsObject:@"fullPath"]) {
  90. NSArray *fileArray = [[dict objectForKey:@"fullPath"] pathComponents];
  91. NSString *filePathAccount = @"0";
  92. if (fileArray.count > 3) {
  93. filePathAccount = fileArray[fileArray.count - 3];
  94. }
  95. // NSString *account = [HWDataManager getStringWithKey:Const_HWAccountPhoneNumber];
  96. // if (account.length == 0) {
  97. // HLog(@"获取accout失败");
  98. // }
  99. //
  100. // if (![filePathAccount isEqualToString:account]) {
  101. // HLog(@"切换账号 文件缓存账号%@ 与 当前登录账号:%@ 不一致", filePathAccount, account);
  102. // return NO;
  103. // }
  104. }
  105. // 线程等待 (信号量 + 1)
  106. dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
  107. NSString *key = [[dict[@"url"] lastPathComponent] stringByDeletingPathExtension];
  108. NSMutableDictionary *dictM = SGDownloadList;
  109. [dictM setObject:dict forKey:key];
  110. BOOL flag = [dictM writeToFile:SGDownloadInfoPath atomically:YES];
  111. // 线程结束 (信号量 - 1)
  112. dispatch_semaphore_signal(_semaphore);
  113. return flag;
  114. }
  115. /** 删除配置信息 */
  116. + (BOOL)deleteFileWithUrl:(NSString *)url {
  117. // 线程等待 分配信号量 (信号量 + 1)
  118. dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
  119. // 删除缓存的文件
  120. NSString *fileFolder = kPath_YunPan_Download_Folder;
  121. NSString *fileName = [url lastPathComponent];
  122. NSString *fileURL = [fileFolder stringByAppendingPathComponent:fileName];
  123. BOOL flag = [[NSFileManager defaultManager] removeItemAtPath:fileURL error:nil];
  124. // BOOL flag = [[NSFileManager defaultManager] removeItemAtPath:dict[@"fullPath"] error:nil];
  125. // 删除plist的文件文件记录
  126. NSString *key = [[url lastPathComponent] stringByDeletingPathExtension];
  127. // NSDictionary *dict = SGDownloadList[key];
  128. [SGDownloadList removeObjectForKey:key];
  129. BOOL writeFlag = [SGDownloadList writeToFile:SGDownloadInfoPath atomically:YES];
  130. // 线程结束 释放信号量(信号量 - 1)
  131. dispatch_semaphore_signal(_semaphore);
  132. return (flag && writeFlag);
  133. }
  134. #pragma mark -
  135. + (BOOL)clearDisks {
  136. // 1.删除所有的文件下载信息关联表
  137. // 2.删除cache 下的download文件夹
  138. return [[NSFileManager defaultManager] removeItemAtPath:KFullDirector error:nil];
  139. }
  140. /** 取消所有当前下载的文件 清理内存缓存的数据 */
  141. + (BOOL)clearMemory {
  142. // 删除信息关联
  143. _downloadList = nil;
  144. return YES;
  145. }
  146. /** 取消所有当前下载的文件 删除磁盘所有的下载 清理内存缓存的数据 */
  147. + (BOOL)clearMemoryAndDisk {
  148. return ([self clearMemory] && [self clearDisks]);
  149. }
  150. @end