123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- //
- // SGCacheManager.m
- // OfflineBreakPointDownload
- //
- // Created by Shangen Zhang on 16/11/26.
- // Copyright © 2016年 Shangen Zhang. All rights reserved.
- //
- #import "SGCacheManager.h"
- #import "NSString+SGHashString.h"
- #import "CWFileManager.h"
- static NSMutableDictionary *_downloadList;
- static dispatch_semaphore_t _semaphore;
- //NSString const * filePath = @"filePath";
- //NSString const * fileSize = @"fileSize";
- //NSString const * fileName = @"fileName";
- //NSString const * fileUrl = @"fileUrl";
- //NSString const * isFinished = @"isFinished";
- //NSString const * totalSize = @"totalSize";
- #define SGDownloadInfoPath [self getNewPlistPath]
- #define SGDownloadList [self getDownloadList]
- @interface SGCacheManager ()
- @end
- @implementation SGCacheManager
- + (void)initialize {
- _semaphore = dispatch_semaphore_create(1);
- }
- #pragma mark-plist
- + (NSString *)getNewPlistPath {
-
- NSString *fileFolder = kPath_YunPan_Download_Folder;
- HLog(@"登录账号:%@", kPath_YunPan_Download_Folder);
- return [fileFolder stringByAppendingPathComponent:@"downloadInfo.plist"];
- }
- + (NSString *)getFullDirector {
- return kPath_YunPan_Download_Folder;
- }
- + (NSMutableDictionary *)getDownloadList {
-
- if (!_downloadList) { // 内存没有
- _downloadList = [[NSDictionary dictionaryWithContentsOfFile:SGDownloadInfoPath] mutableCopy]; // 本地加载
- if (!_downloadList) { // 本地没有,分配内存
- _downloadList = [NSMutableDictionary dictionary];
- }
- }
- return _downloadList;
- }
- #pragma mark - save
- //+ (void)didReciveDownloadCompleteNoti:(NSNotification *)noti {
- //
- // // 缓存记录
- // NSMutableDictionary *dictM = [noti.userInfo mutableCopy];
- //
- // // 从磁盘获取到下载了
- // if ([noti.object integerValue] == 2 && ([self queryFileInfoWithUrl:dictM[fileUrl]])) {
- // return;
- // }
- //
- // // 缓存记录
- // [dictM setObject:@(YES) forKey:isFinished];
- // NSString *key = [dictM[fileUrl] sg_md5HashString];
- //
- // [SGDownloadList setObject:dictM forKey:key];
- //
- // [SGDownloadList writeToFile:SGDownloadInfoPath atomically:YES];
- //
- //}
- #pragma mark - query
- + (NSDictionary *)queryFileInfoWithUrl:(NSString *)url {
- // 本地查找
- NSString *key = [[url lastPathComponent] stringByDeletingPathExtension];
- NSMutableDictionary *dictM = [[SGDownloadList objectForKey:key] mutableCopy];
-
- if (dictM) {
- NSString *path = [KFullDirector stringByAppendingString:dictM[@"fileName"]];
- [dictM setObject:path forKey:@"filePath"];
- HLog(@"路径:%@", path);
- }
-
- return dictM;
-
- }
- + (NSInteger)totalSizeWith:(NSString *)url {
- //NSNumber *size = [self queryFileInfoWithUrl:url][totalSize];
-
- return [[self queryFileInfoWithUrl:url][@"totalSize"] integerValue];
- }
- /** 记录要下载的文件大小 */
- + (BOOL)saveTotalSizeWithSize:(NSInteger)size forURL:(NSString *)url {
-
- return YES;
- }
- /** 增加配置信息 */
- + (BOOL)saveFileInfoWithDict:(NSDictionary *)dict {
-
- // 多账号下载 判断是否需要缓存记录
- //HLog(@"%@", dict);
- if ([[dict allKeys] containsObject:@"fullPath"]) {
- NSArray *fileArray = [[dict objectForKey:@"fullPath"] pathComponents];
- NSString *filePathAccount = @"0";
- if (fileArray.count > 3) {
- filePathAccount = fileArray[fileArray.count - 3];
- }
- // NSString *account = [HWDataManager getStringWithKey:Const_HWAccountPhoneNumber];
- // if (account.length == 0) {
- // HLog(@"获取accout失败");
- // }
- //
- // if (![filePathAccount isEqualToString:account]) {
- // HLog(@"切换账号 文件缓存账号%@ 与 当前登录账号:%@ 不一致", filePathAccount, account);
- // return NO;
- // }
- }
-
-
- // 线程等待 (信号量 + 1)
- dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
-
- NSString *key = [[dict[@"url"] lastPathComponent] stringByDeletingPathExtension];
- NSMutableDictionary *dictM = SGDownloadList;
- [dictM setObject:dict forKey:key];
- BOOL flag = [dictM writeToFile:SGDownloadInfoPath atomically:YES];
-
- // 线程结束 (信号量 - 1)
- dispatch_semaphore_signal(_semaphore);
-
- return flag;
-
- }
- /** 删除配置信息 */
- + (BOOL)deleteFileWithUrl:(NSString *)url {
- // 线程等待 分配信号量 (信号量 + 1)
- dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
-
- // 删除缓存的文件
- NSString *fileFolder = kPath_YunPan_Download_Folder;
- NSString *fileName = [url lastPathComponent];
- NSString *fileURL = [fileFolder stringByAppendingPathComponent:fileName];
- BOOL flag = [[NSFileManager defaultManager] removeItemAtPath:fileURL error:nil];
- // BOOL flag = [[NSFileManager defaultManager] removeItemAtPath:dict[@"fullPath"] error:nil];
-
- // 删除plist的文件文件记录
- NSString *key = [[url lastPathComponent] stringByDeletingPathExtension];
- // NSDictionary *dict = SGDownloadList[key];
- [SGDownloadList removeObjectForKey:key];
- BOOL writeFlag = [SGDownloadList writeToFile:SGDownloadInfoPath atomically:YES];
-
- // 线程结束 释放信号量(信号量 - 1)
- dispatch_semaphore_signal(_semaphore);
- return (flag && writeFlag);
- }
- #pragma mark -
- + (BOOL)clearDisks {
- // 1.删除所有的文件下载信息关联表
- // 2.删除cache 下的download文件夹
- return [[NSFileManager defaultManager] removeItemAtPath:KFullDirector error:nil];
-
- }
- /** 取消所有当前下载的文件 清理内存缓存的数据 */
- + (BOOL)clearMemory {
- // 删除信息关联
- _downloadList = nil;
-
- return YES;
- }
- /** 取消所有当前下载的文件 删除磁盘所有的下载 清理内存缓存的数据 */
- + (BOOL)clearMemoryAndDisk {
- return ([self clearMemory] && [self clearDisks]);
- }
- @end
|