customDownloadCacheManager.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // customDownloadCacheManager.m
  3. // Private-X
  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. // 创建文件储存路径
  18. if (![[NSFileManager defaultManager] fileExistsAtPath:fileFolder]) {
  19. [[NSFileManager defaultManager] createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:nil];
  20. }
  21. return fileFolder;
  22. }else {
  23. HLog(@"创建下载plist文件失败!");
  24. return @"";
  25. }
  26. }
  27. /** 上传的fullPath 是不能用的 要改新的*/
  28. + (NSString *)getFullPathByOldFullPath:(NSString*)fullPath
  29. {
  30. NSArray *pathArr= [fullPath componentsSeparatedByString:@"/"];
  31. NSString * fileName= @"";
  32. if(pathArr && pathArr.count >0){
  33. fileName = pathArr.lastObject;
  34. }
  35. else{
  36. return @"";
  37. }
  38. NSString* fileFullDir = [customDownloadCacheManager getFullDirector];
  39. NSString* curFullPath = [fileFullDir stringByAppendingPathComponent:fileName];
  40. return curFullPath;
  41. }
  42. /** 查询当前文件下载了多少 */
  43. + (int64_t)getFileSizeWithURL:(NSString *)url
  44. {
  45. NSString* fileFullDir = [customDownloadCacheManager getFullDirector];
  46. // 拼接后缀名
  47. NSString *urlFileName = [url lastPathComponent];
  48. NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  49. // 设置下载路径
  50. NSString*fullPath = [fileFullDir stringByAppendingPathComponent:decodeUrlFileName];
  51. // 获取下载进度
  52. NSDictionary *fileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil];
  53. // 获取已下载的长度
  54. return [fileInfo[NSFileSize] longLongValue];
  55. }
  56. /** 删除配置信息 */
  57. + (BOOL)deleteFileWithUrl:(NSString *)url {
  58. BOOL flag = NO;
  59. @synchronized (self) {
  60. // 删除缓存的文件
  61. NSString *fileFolder = [customDownloadCacheManager getFullDirector];
  62. NSString *urlFileName = [url lastPathComponent];
  63. NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  64. NSString *fileName = decodeUrlFileName;
  65. NSString *fileURL = [fileFolder stringByAppendingPathComponent:fileName];
  66. flag= [[NSFileManager defaultManager] removeItemAtPath:fileURL error:nil];
  67. }
  68. return (flag);
  69. }
  70. #pragma mark -
  71. + (BOOL)clearDisks {
  72. // 1.删除所有的文件下载信息关联表
  73. // 2.删除cache 下的download文件夹
  74. return [[NSFileManager defaultManager] removeItemAtPath:KCustomFullDirector error:nil];
  75. }
  76. /** 取消所有当前下载的文件 清理内存缓存的数据 */
  77. + (BOOL)clearMemory {
  78. // 删除信息关联
  79. _downloadList = nil;
  80. return YES;
  81. }
  82. /** 取消所有当前下载的文件 删除磁盘所有的下载 清理内存缓存的数据 */
  83. + (BOOL)clearMemoryAndDisk {
  84. return ([self clearMemory] && [self clearDisks]);
  85. }
  86. @end