TYDownLoadModel.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // TYDownloadModel.m
  3. // TYDownloadManagerDemo
  4. //
  5. // Created by tany on 16/6/1.
  6. // Copyright © 2016年 tany. All rights reserved.
  7. //
  8. #import "TYDownloadModel.h"
  9. @interface TYDownloadProgress ()
  10. // 续传大小
  11. @property (nonatomic, assign) int64_t resumeBytesWritten;
  12. // 这次写入的数量
  13. @property (nonatomic, assign) int64_t bytesWritten;
  14. // 已下载的数量
  15. @property (nonatomic, assign) int64_t totalBytesWritten;
  16. // 文件的总大小
  17. @property (nonatomic, assign) int64_t totalBytesExpectedToWrite;
  18. // 下载进度
  19. @property (nonatomic, assign) float progress;
  20. // 下载速度
  21. @property (nonatomic, assign) float speed;
  22. // 下载剩余时间
  23. @property (nonatomic, assign) int remainingTime;
  24. @end
  25. @interface TYDownloadModel ()
  26. // >>>>>>>>>>>>>>>>>>>>>>>>>> download info
  27. // 下载地址
  28. @property (nonatomic, strong) NSString *downloadURL;
  29. // 文件名 默认nil 则为下载URL中的文件名
  30. @property (nonatomic, strong) NSString *fileName;
  31. // 缓存文件目录 默认nil 则为manger缓存目录
  32. @property (nonatomic, strong) NSString *downloadDirectory;
  33. // >>>>>>>>>>>>>>>>>>>>>>>>>> task info
  34. // 下载状态
  35. @property (nonatomic, assign) TYDownloadState state;
  36. // 下载任务
  37. @property (nonatomic, strong) NSURLSessionTask *task;
  38. // 文件流
  39. @property (nonatomic, strong) NSOutputStream *stream;
  40. // 下载文件路径,下载完成后有值,把它移动到你的目录
  41. @property (nonatomic, strong) NSString *filePath;
  42. // 下载时间
  43. @property (nonatomic, strong) NSDate *downloadDate;
  44. // 断点续传需要设置这个数据
  45. @property (nonatomic, strong) NSData *resumeData;
  46. // 手动取消当做暂停
  47. @property (nonatomic, assign) BOOL manualCancle;
  48. @end
  49. @implementation TYDownloadModel
  50. - (instancetype)init
  51. {
  52. if (self = [super init]) {
  53. _progress = [[TYDownloadProgress alloc]init];
  54. }
  55. return self;
  56. }
  57. - (instancetype)initWithURLString:(NSString *)URLString
  58. {
  59. return [self initWithURLString:URLString filePath:nil];
  60. }
  61. - (instancetype)initWithURLString:(NSString *)URLString filePath:(NSString *)filePath
  62. {
  63. if (self = [self init]) {
  64. _downloadURL = URLString;
  65. NSString*urlFileName = filePath.lastPathComponent;
  66. NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  67. _fileName = decodeUrlFileName;
  68. // if ([_downloadURL.lastPathComponent containsString:@"?"]) {
  69. // NSArray *array = [_downloadURL.lastPathComponent componentsSeparatedByString:@"?"];
  70. // _fileName = array.firstObject;
  71. // }else {
  72. // _fileName = _downloadURL.lastPathComponent;
  73. // }
  74. _downloadDirectory = filePath.stringByDeletingLastPathComponent;
  75. _filePath = filePath;
  76. }
  77. return self;
  78. }
  79. -(NSString *)fileName
  80. {
  81. if (!_fileName) {
  82. _fileName = _downloadURL.lastPathComponent;
  83. // if ([_downloadURL.lastPathComponent containsString:@"?"]) {
  84. // NSArray *array = [_downloadURL.lastPathComponent componentsSeparatedByString:@"?"];
  85. // _fileName = array.firstObject;
  86. // }else {
  87. // _fileName = _downloadURL.lastPathComponent;
  88. // }
  89. }
  90. return _fileName;
  91. }
  92. - (NSString *)downloadDirectory
  93. {
  94. if (!_downloadDirectory) {
  95. _downloadDirectory = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"TYDownloadCache"];
  96. }
  97. return _downloadDirectory;
  98. }
  99. - (NSString *)filePath
  100. {
  101. if (!_filePath) {
  102. _filePath = [self.downloadDirectory stringByAppendingPathComponent:self.fileName];
  103. }
  104. return _filePath;
  105. }
  106. @end
  107. @implementation TYDownloadProgress
  108. @end