customDownloadOperation.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // customDownloadOperation.m
  3. // 双子星云手机
  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. @implementation customDownloadOperation
  11. - (instancetype)initWith:(NSString *)url session:(NSURLSession *)session{
  12. if (self = [super init]) {
  13. _url = url;
  14. // 初始化下载信息
  15. // 拼接后缀名
  16. NSString *urlFileName = [url lastPathComponent];
  17. NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  18. self.fileName = decodeUrlFileName;
  19. NSString* fileFullDir = [customDownloadCacheManager getFullDirector];
  20. // 创建文件储存路径
  21. if (![[NSFileManager defaultManager] fileExistsAtPath:fileFullDir]) {
  22. [[NSFileManager defaultManager] createDirectoryAtPath:fileFullDir withIntermediateDirectories:YES attributes:nil error:nil];
  23. }
  24. // 设置下载路径
  25. self.fullPath = [fileFullDir stringByAppendingPathComponent:self.fileName];
  26. _currentSize = [customDownloadCacheManager getFileSizeWithURL:url];
  27. // 偏好设置里面存储总数据
  28. //_totalSize = 0;
  29. _timeStamp = [iTools getNowTimeStamp];
  30. // 校验
  31. if (self.currentSize == self.totalSize && self.totalSize != 0) {
  32. return nil;
  33. }
  34. _downloadState = customDownloadStateDoing;
  35. _session = session;
  36. _dataTask = [_session custom_downloadDataTaskWithURLString:url startSize:_currentSize];
  37. }
  38. return _dataTask ? self : nil;
  39. }
  40. #pragma mark - setups
  41. #pragma mark - get download info
  42. // 构造回调信息
  43. - (NSDictionary *)downLoadInfoWithFinished:(BOOL)finished {
  44. return @{
  45. @"url" : self.url,
  46. @"fileName" : self.fileName,
  47. @"fullPath" : self.fullPath,
  48. @"currentSize" : @(self.currentSize),
  49. @"totalSize" : @(self.totalSize),
  50. @"fileType" : @(self.fileType),
  51. @"timeStamp" : @(self.timeStamp),
  52. @"downloadState" : @(self.downloadState),
  53. @"isFinished" : @(finished)
  54. };
  55. }
  56. @end