customDownloadOperation.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. @end