// // 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; } @end