1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- //
- // customDownloadOperation.m
- // 双子星云手机
- //
- // Created by xd h on 2024/7/1.
- //
- #import "customDownloadOperation.h"
- #import "customDownloadCacheManager.h"
- #import "NSURLSession+customDownloadTask.h"
- @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;
-
- 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;
- }
- #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
|