RQDownloadModel.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // RQDownloadModel.m
  3. // TEST
  4. //
  5. // Created by 张嵘 on 2018/10/22.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQDownloadModel.h"
  9. #import "RQDownloadHeader.h"
  10. @implementation RQDownloadModel
  11. - (void)encodeWithCoder:(NSCoder *)aCoder {
  12. [aCoder encodeObject:self.imageUrl forKey:@"imageUrl"];
  13. [aCoder encodeObject:self.title forKey:@"title"];
  14. [aCoder encodeObject:self.content forKey:@"content"];
  15. [aCoder encodeObject:self.urlString forKey:@"urlString"];
  16. [aCoder encodeObject:self.downloadDesc forKey:@"downloadDesc"];
  17. [aCoder encodeObject:self.fileName forKey:@"fileName"];
  18. [aCoder encodeObject:self.fileFormat forKey:@"fileFormat"];
  19. [aCoder encodeObject:self.destinationPath forKey:@"destinationPath"];
  20. [aCoder encodeObject:[NSNumber numberWithFloat:self.progress] forKey:@"progress"];
  21. [aCoder encodeObject:[NSNumber numberWithInteger:self.status] forKey:@"status"];
  22. [aCoder encodeObject:self.statusText forKey:@"statusText"];
  23. [aCoder encodeObject:self.completeTime forKey:@"completeTime"];
  24. [aCoder encodeObject:[NSNumber numberWithBool:self.isLast] forKey:@"isLast"];
  25. [aCoder encodeObject:[NSNumber numberWithInteger:self.fileTotalSize] forKey:@"fileTotalSize"];
  26. [aCoder encodeObject:[NSNumber numberWithInteger:self.fileDownloadSize] forKey:@"fileDownloadSize"];
  27. [aCoder encodeObject:[NSNumber numberWithBool:self.isFinished] forKey:@"isFinished"];
  28. }
  29. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  30. if (self = [super init]) {
  31. self.imageUrl = [aDecoder decodeObjectForKey:@"imageUrl"];
  32. self.title = [aDecoder decodeObjectForKey:@"title"];
  33. self.content = [aDecoder decodeObjectForKey:@"content"];
  34. self.urlString = [aDecoder decodeObjectForKey:@"urlString"];
  35. self.downloadDesc = [aDecoder decodeObjectForKey:@"downloadDesc"];
  36. self.fileName = [aDecoder decodeObjectForKey:@"fileName"];
  37. self.fileFormat = [aDecoder decodeObjectForKey:@"fileFormat"];
  38. self.destinationPath = [aDecoder decodeObjectForKey:@"destinationPath"];
  39. self.progress = [[aDecoder decodeObjectForKey:@"progress"] floatValue];
  40. self.status = [[aDecoder decodeObjectForKey:@"status"] integerValue];
  41. self.statusText = [aDecoder decodeObjectForKey:@"statusText"];
  42. self.completeTime = [aDecoder decodeObjectForKey:@"completeTime"];
  43. self.isLast = [[aDecoder decodeObjectForKey:@"isLast"] boolValue];
  44. self.fileTotalSize = [[aDecoder decodeObjectForKey:@"fileTotalSize"] integerValue];
  45. self.fileDownloadSize = [[aDecoder decodeObjectForKey:@"fileDownloadSize"] integerValue];
  46. self.isFinished = [[aDecoder decodeObjectForKey:@"isFinished"] boolValue];
  47. }
  48. return self;
  49. }
  50. - (NSString *)destinationPath{
  51. _destinationPath = [[RQCachesDirectory stringByAppendingString:self.fileName] stringByAppendingString:self.fileFormat];
  52. return _destinationPath;
  53. }
  54. - (NSString *)fileName{
  55. if (!_fileName) {
  56. NSTimeInterval timeInterval = [[NSDate date]timeIntervalSince1970];
  57. //解决多个任务同时开始时 文件重名问题
  58. NSString *timeStr = [NSString stringWithFormat:@"%.6f",timeInterval];
  59. timeStr = [timeStr stringByReplacingOccurrencesOfString:@"." withString:@"_"];
  60. _fileName = [NSString stringWithFormat:@"%@",timeStr];
  61. }
  62. return _fileName;
  63. }
  64. - (NSString *)fileFormat{
  65. if (!_fileFormat && _urlString) {
  66. NSArray *urlArr = [_urlString componentsSeparatedByString:@"."];
  67. if (urlArr && urlArr.count>1) {
  68. self.fileFormat = [@"." stringByAppendingString:[urlArr lastObject]];
  69. }
  70. }
  71. return _fileFormat;
  72. }
  73. - (void)setProgress:(CGFloat)progress{
  74. if (_progress != progress) {
  75. _progress = progress;
  76. }
  77. if ([kRQDownloadManager enableProgressLog]) {
  78. NSLog(@"%@%@==%@==%.1f%%",self.fileName,self.fileFormat,self.statusText,progress*100*1.0);
  79. }
  80. if (self.progressChanged) {
  81. self.progressChanged(self);
  82. }
  83. }
  84. - (void)setStatus:(RQDownloadStatus)status{
  85. if (_status != status) {
  86. _status = status;
  87. [self setStatusTextWith:_status];
  88. if (self.statusChanged) {
  89. self.statusChanged(self);
  90. }
  91. }
  92. }
  93. - (void)setUrlString:(NSString *)urlString{
  94. _urlString = urlString;
  95. NSArray *urlArr = [_urlString componentsSeparatedByString:@"."];
  96. if (urlArr && urlArr.count>1) {
  97. self.fileFormat = [@"." stringByAppendingString:[urlArr lastObject]];
  98. }
  99. }
  100. - (void)setCompleteTime:(NSString *)completeTime{
  101. NSDateFormatter *fomatter = [[NSDateFormatter alloc]init];
  102. _completeTime = [fomatter stringFromDate:[NSDate date]];
  103. }
  104. - (void)setStatusTextWith:(RQDownloadStatus)status{
  105. _status = status;
  106. switch (status) {
  107. case RQDownloadStatus_Running: {
  108. self.statusText = @"正在下载";
  109. break;
  110. }
  111. case RQDownloadStatus_Suspended: {
  112. self.statusText = @"暂停下载";
  113. break;
  114. }
  115. case RQDownloadStatus_Failed: {
  116. self.statusText = @"下载失败";
  117. break;
  118. }
  119. case RQDownloadStatus_Cancel: {
  120. self.statusText = @"取消下载";
  121. break;
  122. }
  123. case RQDownloadStatus_Waiting: {
  124. self.statusText = @"等待下载";
  125. break;
  126. }
  127. case RQDownloadStatus_Completed: {
  128. self.statusText = @"下载完成";
  129. break;
  130. }
  131. default: {
  132. break;
  133. }
  134. }
  135. NSLog(@"%@%@==%@",self.fileName,self.fileFormat,self.statusText);
  136. }
  137. + (NSArray *)mj_ignoredCodingPropertyNames{
  138. return @[@"statusChanged",@"progressChanged",@"stream",@"operation"];
  139. }
  140. - (NSInteger)fileDownloadSize{
  141. // 获取文件下载长度
  142. NSInteger fileDownloadSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.destinationPath error:nil][NSFileSize] integerValue];
  143. _fileDownloadSize = fileDownloadSize;
  144. return _fileDownloadSize;
  145. }
  146. - (NSOutputStream *)stream{
  147. if (!_stream) {
  148. _stream = [NSOutputStream outputStreamToFileAtPath:self.destinationPath append:YES];
  149. }
  150. return _stream;
  151. }
  152. - (BOOL)isFinished{
  153. return (self.fileTotalSize == self.fileDownloadSize) && (self.fileTotalSize != 0);
  154. }
  155. @end