MCDownloadReceipt.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // MCDownloadReceipt.m
  3. // MCDownloadManager
  4. //
  5. // Created by M.C on 17/4/6. (QQ:714080794 Gmail:chaoma0609@gmail.com)
  6. // Copyright © 2017年 qikeyun. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #import "MCDownloadReceipt.h"
  27. #import <CommonCrypto/CommonDigest.h>
  28. extern NSString * cacheFolder();
  29. static unsigned long long fileSizeForPath(NSString *path) {
  30. signed long long fileSize = 0;
  31. NSFileManager *fileManager = [NSFileManager defaultManager];
  32. if ([fileManager fileExistsAtPath:path]) {
  33. NSError *error = nil;
  34. NSDictionary *fileDict = [fileManager attributesOfItemAtPath:path error:&error];
  35. if (!error && fileDict) {
  36. fileSize = [fileDict fileSize];
  37. }
  38. }
  39. return fileSize;
  40. }
  41. static NSString * getMD5String(NSString *str) {
  42. if (str == nil) return nil;
  43. const char *cstring = str.UTF8String;
  44. unsigned char bytes[CC_MD5_DIGEST_LENGTH];
  45. CC_MD5(cstring, (CC_LONG)strlen(cstring), bytes);
  46. NSMutableString *md5String = [NSMutableString string];
  47. for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
  48. [md5String appendFormat:@"%02x", bytes[i]];
  49. }
  50. return md5String;
  51. }
  52. @interface MCDownloadReceipt()
  53. @property (nonatomic, assign) MCDownloadState state;
  54. @property (nonatomic, copy) NSString *url;
  55. @property (nonatomic, copy) NSString *filePath;
  56. @property (nonatomic, copy) NSString *filename;
  57. @property (nonatomic, copy) NSString *truename;
  58. @property (nonatomic, strong) NSProgress *progress;
  59. @property (assign, nonatomic) long long totalBytesWritten;
  60. @end
  61. @implementation MCDownloadReceipt
  62. - (NSString *)filePath {
  63. NSString *path = [cacheFolder() stringByAppendingPathComponent:self.filename];
  64. if (![path isEqualToString:_filePath] ) {
  65. if (_filePath && ![[NSFileManager defaultManager] fileExistsAtPath:_filePath]) {
  66. NSString *dir = [_filePath stringByDeletingLastPathComponent];
  67. [[NSFileManager defaultManager] createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
  68. }
  69. _filePath = path;
  70. }
  71. return _filePath;
  72. }
  73. - (NSString *)filename {
  74. if (_filename == nil) {
  75. NSString *pathExtension = self.url.pathExtension;
  76. if (pathExtension.length) {
  77. _filename = [NSString stringWithFormat:@"%@.%@", getMD5String(self.url), pathExtension];
  78. } else {
  79. _filename = getMD5String(self.url);
  80. }
  81. }
  82. return _filename;
  83. }
  84. - (NSString *)truename {
  85. if (_truename == nil) {
  86. _truename = self.url.lastPathComponent;
  87. }
  88. return _truename;
  89. }
  90. - (NSProgress *)progress {
  91. if (_progress == nil) {
  92. _progress = [[NSProgress alloc] initWithParent:nil userInfo:nil];
  93. }
  94. @try {
  95. _progress.totalUnitCount = self.totalBytesExpectedToWrite;
  96. _progress.completedUnitCount = self.totalBytesWritten;
  97. } @catch (NSException *exception) {
  98. }
  99. return _progress;
  100. }
  101. - (long long)totalBytesWritten {
  102. return fileSizeForPath(self.filePath);
  103. }
  104. - (instancetype)initWithURL:(NSString *)url {
  105. if (self = [self init]) {
  106. self.url = url;
  107. }
  108. return self;
  109. }
  110. #pragma mark - NSCoding
  111. - (void)encodeWithCoder:(NSCoder *)aCoder
  112. {
  113. [aCoder encodeObject:self.url forKey:NSStringFromSelector(@selector(url))];
  114. [aCoder encodeObject:self.filePath forKey:NSStringFromSelector(@selector(filePath))];
  115. [aCoder encodeObject:@(self.state) forKey:NSStringFromSelector(@selector(state))];
  116. [aCoder encodeObject:self.filename forKey:NSStringFromSelector(@selector(filename))];
  117. [aCoder encodeObject:@(self.totalBytesWritten) forKey:NSStringFromSelector(@selector(totalBytesWritten))];
  118. [aCoder encodeObject:@(self.totalBytesExpectedToWrite) forKey:NSStringFromSelector(@selector(totalBytesExpectedToWrite))];
  119. }
  120. - (id)initWithCoder:(NSCoder *)aDecoder
  121. {
  122. self = [super init];
  123. if (self) {
  124. self.url = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(url))];
  125. self.filePath = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(filePath))];
  126. self.state = [[aDecoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(state))] unsignedIntegerValue];
  127. self.filename = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(filename))];
  128. self.totalBytesWritten = [[aDecoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(totalBytesWritten))] unsignedIntegerValue];
  129. self.totalBytesExpectedToWrite = [[aDecoder decodeObjectOfClass:[NSNumber class] forKey:NSStringFromSelector(@selector(totalBytesExpectedToWrite))] unsignedIntegerValue];
  130. }
  131. return self;
  132. }
  133. - (instancetype)initWithURLString:(NSString *)URLString
  134. downloadOperationCancelToken:(id)downloadOperationCancelToken
  135. downloaderProgressBlock:(MCDownloaderProgressBlock)downloaderProgressBlock
  136. downloaderCompletedBlock:(MCDownloaderCompletedBlock)downloaderCompletedBlock {
  137. if (self = [self init]) {
  138. self.url = URLString;
  139. self.totalBytesExpectedToWrite = 0;
  140. self.downloadOperationCancelToken = downloadOperationCancelToken;
  141. self.downloaderProgressBlock = downloaderProgressBlock;
  142. self.downloaderCompletedBlock = downloaderCompletedBlock;
  143. }
  144. return self;
  145. }
  146. - (void)setTotalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite {
  147. _totalBytesExpectedToWrite = totalBytesExpectedToWrite;
  148. }
  149. - (void)setState:(MCDownloadState)state {
  150. _state = state;
  151. }
  152. - (void)setDownloadOperationCancelToken:(id)downloadOperationCancelToken {
  153. _downloadOperationCancelToken = downloadOperationCancelToken;
  154. }
  155. - (void)setDownloaderProgressBlock:(MCDownloaderProgressBlock)downloaderProgressBlock {
  156. _downloaderProgressBlock = downloaderProgressBlock;
  157. }
  158. - (void)setDownloaderCompletedBlock:(MCDownloaderCompletedBlock)downloaderCompletedBlock {
  159. _downloaderCompletedBlock = downloaderCompletedBlock;
  160. }
  161. - (void)setSpeed:(NSString *)speed {
  162. _speed = speed;
  163. }
  164. @end