customDownloadCacheManager.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // customDownloadCacheManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/7/1.
  6. //
  7. #import "customDownloadCacheManager.h"
  8. #import "customDownloadManager.h"
  9. static NSMutableDictionary *_downloadList;
  10. @implementation customDownloadCacheManager
  11. #pragma mark-plist
  12. + (NSString *)getFullDirector {
  13. NSString *account = [customDownloadManager shareManager].uid;
  14. if (account.length != 0)
  15. {
  16. NSString *fileFolder = [HWDataManager documentPathForAccount:account fileFolder:KCustomDownloadDirector];
  17. return fileFolder;
  18. }else {
  19. HLog(@"创建下载plist文件失败!");
  20. return @"";
  21. }
  22. }
  23. /** 查询当前文件下载了多少 */
  24. + (int64_t)getFileSizeWithURL:(NSString *)url
  25. {
  26. NSString* fileFullDir = [customDownloadCacheManager getFullDirector];
  27. // 拼接后缀名
  28. NSString *urlFileName = [url lastPathComponent];
  29. NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  30. // 设置下载路径
  31. NSString*fullPath = [fileFullDir stringByAppendingPathComponent:decodeUrlFileName];
  32. // 获取下载进度
  33. NSDictionary *fileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil];
  34. // 获取已下载的长度
  35. return [fileInfo[NSFileSize] longLongValue];
  36. }
  37. /** 删除配置信息 */
  38. + (BOOL)deleteFileWithUrl:(NSString *)url {
  39. BOOL flag = NO;
  40. @synchronized (self) {
  41. // 删除缓存的文件
  42. NSString *fileFolder = [customDownloadCacheManager getFullDirector];
  43. NSString *urlFileName = [url lastPathComponent];
  44. NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  45. NSString *fileName = decodeUrlFileName;
  46. NSString *fileURL = [fileFolder stringByAppendingPathComponent:fileName];
  47. flag= [[NSFileManager defaultManager] removeItemAtPath:fileURL error:nil];
  48. }
  49. return (flag);
  50. }
  51. #pragma mark -
  52. + (BOOL)clearDisks {
  53. // 1.删除所有的文件下载信息关联表
  54. // 2.删除cache 下的download文件夹
  55. return [[NSFileManager defaultManager] removeItemAtPath:KCustomFullDirector error:nil];
  56. }
  57. /** 取消所有当前下载的文件 清理内存缓存的数据 */
  58. + (BOOL)clearMemory {
  59. // 删除信息关联
  60. _downloadList = nil;
  61. return YES;
  62. }
  63. /** 取消所有当前下载的文件 删除磁盘所有的下载 清理内存缓存的数据 */
  64. + (BOOL)clearMemoryAndDisk {
  65. return ([self clearMemory] && [self clearDisks]);
  66. }
  67. @end