// // mixDownloadCacheManager.m // 双子星云手机 // // Created by xd h on 2024/6/27. // #import "mixDownloadCacheManager.h" #import "mixDownloadManager.h" static NSMutableDictionary *_downloadList; static dispatch_semaphore_t _semaphore; @implementation mixDownloadCacheManager + (void)initialize { _semaphore = dispatch_semaphore_create(1); } #pragma mark-plist + (NSString *)getNewPlistPath { NSString *account = KMixDownloadUID; if (account.length != 0) { NSString *fileFolder = [HWDataManager documentPathForAccount:account fileFolder:KMixDownloadDirector]; HLog(@"下载账号:%@", fileFolder); return [fileFolder stringByAppendingPathComponent:@"downloadInfo.plist"];; }else { HLog(@" 创建下载plist文件失败!"); return @""; } } + (NSMutableDictionary *)getDownloadList { if (!_downloadList) { // 内存没有 _downloadList = [[NSDictionary dictionaryWithContentsOfFile:[self getNewPlistPath]] mutableCopy]; // 本地加载 if (!_downloadList) { // 本地没有,分配内存 _downloadList = [NSMutableDictionary dictionary]; } } return _downloadList; } + (NSString *)getFullDirector { NSString *account = KMixDownloadUID; if (account.length != 0) { NSString *fileFolder = [HWDataManager documentPathForAccount:account fileFolder:KMixDownloadDirector]; return fileFolder; }else { HLog(@"创建下载plist文件失败!"); return @""; } } /** 查询文件信息 */ + (NSMutableDictionary *)queryFileInfoWithUrl:(NSString *)url { // 本地查找 NSString *key = [[url lastPathComponent] stringByDeletingPathExtension]; NSMutableDictionary *dictM = [[[self getDownloadList] 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)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 = KMixDownloadUID; if (account.length == 0) { } 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 = [self getDownloadList]; [dictM setObject:dict forKey:key]; BOOL flag = [dictM writeToFile:[self getNewPlistPath] atomically:YES]; // 线程结束 (信号量 - 1) dispatch_semaphore_signal(_semaphore); return flag; } /** 删除配置信息 */ + (BOOL)deleteFileWithUrl:(NSString *)url { // 线程等待 分配信号量 (信号量 + 1) dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER); // 删除缓存的文件 NSString *fileFolder = [mixDownloadCacheManager getFullDirector]; NSString *urlFileName = [url lastPathComponent]; NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *fileName = decodeUrlFileName; 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]; [[self getDownloadList] removeObjectForKey:key]; BOOL writeFlag = [[self getDownloadList] writeToFile:[self getNewPlistPath] 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