// // customDownloadOperation.m // Private-X // // Created by xd h on 2024/7/1. // #import "customDownloadOperation.h" #import "customDownloadCacheManager.h" #import "NSURLSession+customDownloadTask.h" static NSString *downloadOperationType; @interface customDownloadOperation () //@property(nonatomic,copy)NSString *downloadType; @end @implementation customDownloadOperation - (instancetype)initWith:(NSString *)url session:(NSURLSession *)session{ if (self = [super init]) { _url = url; // 初始化下载信息 // 拼接后缀名 NSString *urlFileName = [url lastPathComponent]; NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; self.fileName = decodeUrlFileName; if(downloadOperationType && [downloadOperationType isEqualToString:@"audioPlayCache"]){ NSString* fileFullDir = [[audioPlayListManager shareManager] getAudioCacheFullPathBy:self.fileName]; // 设置下载路径 self.fullPath = fileFullDir; downloadOperationType = @""; } else{ NSString* fileFullDir = [customDownloadCacheManager getFullDirector]; // 创建文件储存路径 if (![[NSFileManager defaultManager] fileExistsAtPath:fileFullDir]) { [[NSFileManager defaultManager] createDirectoryAtPath:fileFullDir withIntermediateDirectories:YES attributes:nil error:nil]; } // 设置下载路径 self.fullPath = [fileFullDir stringByAppendingPathComponent:self.fileName]; } _currentSize = [customDownloadCacheManager getFileSizeWithURL:url]; // 偏好设置里面存储总数据 //_totalSize = 0; _timeStamp = [iTools getNowTimeStamp]; // 校验 if (self.currentSize == self.totalSize && self.totalSize != 0) { return nil; } _downloadState = customDownloadStateDoing; _session = session; _dataTask = [_session custom_downloadDataTaskWithURLString:url startSize:_currentSize]; } return _dataTask ? self : nil; } - (instancetype)initWith:(NSString *)url session:(NSURLSession *)session withType:(NSString*)downloadType { downloadOperationType = downloadType; return [[customDownloadOperation alloc] initWith:url session:session]; } #pragma mark - setups #pragma mark - get download info // 构造回调信息 - (NSDictionary *)downLoadInfoWithFinished:(BOOL)finished { return @{ @"url" : self.url, @"fileName" : self.fileName, @"fullPath" : self.fullPath, @"currentSize" : @(self.currentSize), @"totalSize" : @(self.totalSize), @"fileType" : @(self.fileType), @"timeStamp" : @(self.timeStamp), @"downloadState" : @(self.downloadState), @"isFinished" : @(finished) }; } @end