customDownloadOperation.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // customDownloadOperation.m
  3. // Private-X
  4. //
  5. // Created by xd h on 2024/7/1.
  6. //
  7. #import "customDownloadOperation.h"
  8. #import "customDownloadCacheManager.h"
  9. #import "NSURLSession+customDownloadTask.h"
  10. static NSString *downloadOperationType;
  11. @interface customDownloadOperation ()
  12. //@property(nonatomic,copy)NSString *downloadType;
  13. @end
  14. @implementation customDownloadOperation
  15. - (instancetype)initWith:(NSString *)url session:(NSURLSession *)session{
  16. if (self = [super init]) {
  17. _url = url;
  18. // 初始化下载信息
  19. // 拼接后缀名
  20. NSString *urlFileName = [url lastPathComponent];
  21. NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  22. self.fileName = decodeUrlFileName;
  23. if(downloadOperationType && [downloadOperationType isEqualToString:@"audioPlayCache"]){
  24. NSString* fileFullDir = [[audioPlayListManager shareManager] getAudioCacheFullPathBy:self.fileName];
  25. // 设置下载路径
  26. self.fullPath = fileFullDir;
  27. downloadOperationType = @"";
  28. }
  29. else{
  30. NSString* fileFullDir = [customDownloadCacheManager getFullDirector];
  31. // 创建文件储存路径
  32. if (![[NSFileManager defaultManager] fileExistsAtPath:fileFullDir]) {
  33. [[NSFileManager defaultManager] createDirectoryAtPath:fileFullDir withIntermediateDirectories:YES attributes:nil error:nil];
  34. }
  35. // 设置下载路径
  36. self.fullPath = [fileFullDir stringByAppendingPathComponent:self.fileName];
  37. }
  38. _currentSize = [customDownloadCacheManager getFileSizeWithURL:url];
  39. // 偏好设置里面存储总数据
  40. //_totalSize = 0;
  41. _timeStamp = [iTools getNowTimeStamp];
  42. // 校验
  43. if (self.currentSize == self.totalSize && self.totalSize != 0) {
  44. return nil;
  45. }
  46. _downloadState = customDownloadStateDoing;
  47. _session = session;
  48. _dataTask = [_session custom_downloadDataTaskWithURLString:url startSize:_currentSize];
  49. }
  50. return _dataTask ? self : nil;
  51. }
  52. - (instancetype)initWith:(NSString *)url session:(NSURLSession *)session withType:(NSString*)downloadType
  53. {
  54. downloadOperationType = downloadType;
  55. return [[customDownloadOperation alloc] initWith:url session:session];
  56. }
  57. #pragma mark - setups
  58. #pragma mark - get download info
  59. // 构造回调信息
  60. - (NSDictionary *)downLoadInfoWithFinished:(BOOL)finished {
  61. return @{
  62. @"url" : self.url,
  63. @"fileName" : self.fileName,
  64. @"fullPath" : self.fullPath,
  65. @"currentSize" : @(self.currentSize),
  66. @"totalSize" : @(self.totalSize),
  67. @"fileType" : @(self.fileType),
  68. @"timeStamp" : @(self.timeStamp),
  69. @"downloadState" : @(self.downloadState),
  70. @"isFinished" : @(finished)
  71. };
  72. }
  73. @end