CloudDownloadSession.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // CloudDownloadSession.m
  3. // zhuxun
  4. //
  5. // Created by winsoft on 17/6/29.
  6. //
  7. //
  8. #import "CloudDownloadSession.h"
  9. #import "HttpRequest.h"
  10. #import "CloudStorageDB.h"
  11. #define MaxTryTimes 3
  12. @interface CloudDownloadSession()
  13. @property (nonatomic , strong) NSMutableDictionary *successBlocksDict;
  14. @property (nonatomic , strong) NSMutableDictionary *failureBlocksDict;
  15. @property (nonatomic , strong) NSMutableDictionary *progressBlocksDict;
  16. @property (nonatomic , strong) WSDownloadSession *downloadSession;
  17. @property (nonatomic , copy) NSDate *lastRecordTime;
  18. /**
  19. * 失败重试次数
  20. */
  21. @property (nonatomic , assign) NSInteger failedAndTryTimes;
  22. @end
  23. @implementation CloudDownloadSession
  24. - (NSMutableDictionary *)successBlocksDict
  25. {
  26. if (!_successBlocksDict) {
  27. _successBlocksDict = [NSMutableDictionary dictionary];
  28. }
  29. return _successBlocksDict;
  30. }
  31. - (NSMutableDictionary *)failureBlocksDict
  32. {
  33. if (!_failureBlocksDict) {
  34. _failureBlocksDict = [NSMutableDictionary dictionary];
  35. }
  36. return _failureBlocksDict;
  37. }
  38. - (NSMutableDictionary *)progressBlocksDict
  39. {
  40. if (!_progressBlocksDict) {
  41. _progressBlocksDict = [NSMutableDictionary dictionary];
  42. }
  43. return _progressBlocksDict;
  44. }
  45. + (CloudDownloadSession *)startDownloadFileWithFileDownUpModel:(CSFileDownUpModel *)fileDownUpModel
  46. {
  47. CloudDownloadSession *downLoadSession = [[CloudDownloadSession alloc]initDowunLoadFileWithFileDownUpModel:fileDownUpModel];
  48. return downLoadSession;
  49. }
  50. - (instancetype)initDowunLoadFileWithFileDownUpModel:(CSFileDownUpModel *)fileDownUpModel
  51. {
  52. if (self = [super init]) {
  53. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  54. //查找数据库中md5不同,name,路径相同文件.
  55. [kCloudStorageDB getCSDownUpFileSameNameDifMd5WithMD5:fileDownUpModel.md5 name:fileDownUpModel.name localPath:fileDownUpModel.localRootPath downUpModel:DownUpModelDown resultBlock:^(NSInteger sameNameFileCount) {
  56. //修改存放路径
  57. if (sameNameFileCount) {
  58. #warning 2017-08-02 命名可以再优化
  59. fileDownUpModel.localRootPath = [NSString stringWithFormat:@"%@_%@",fileDownUpModel.md5,fileDownUpModel.localRootPath];
  60. //存储.
  61. [kCloudStorageDB updateCSDownUpFileLocalPathWithCSFileDownUpModel:fileDownUpModel newLocalPath:fileDownUpModel.localRootPath];
  62. }
  63. _fileDownUpModel = fileDownUpModel;
  64. [self startDownloadFileWithfileDownUpModel:fileDownUpModel];
  65. }];
  66. });
  67. }
  68. return self;
  69. }
  70. - (void)startDownloadFileWithfileDownUpModel:(CSFileDownUpModel *)fileDownUpModel
  71. {
  72. // __weak typeof(self)weakSelf = self;
  73. // return;
  74. [[HttpRequest sharedManager]cloudStorageGetFileDownloadURLWithMD5:fileDownUpModel.md5 complete:^(BOOL success, id data, NSError *error) {
  75. if (success && data) {
  76. NSString *url = data[@"url"];
  77. self.downloadSession = [WSDownloadSession oneDownloadSessionWithFileUrlString:url savePath:CloudStorageFullRouter localPath:fileDownUpModel.localRootPath fileMd5:fileDownUpModel.md5 UUID:fileDownUpModel.downUpUUID fileName:fileDownUpModel.name downloadedSize:fileDownUpModel.downUpSize fileSize:fileDownUpModel.size progressblock:nil
  78. successblock:nil failedBlock:nil];
  79. __weak typeof(self)weakSelf = self;
  80. self.downloadSession.downloadFailedBlock = ^(NSString* errMessage,NSString *msgUUID){
  81. if (weakSelf.failedAndTryTimes < MaxTryTimes) {
  82. [weakSelf startDownloadFileWithfileDownUpModel:weakSelf.fileDownUpModel];
  83. weakSelf.failedAndTryTimes ++;
  84. }else{
  85. weakSelf.fileDownUpModel.downUpStatus = DownUpStatusFailure;
  86. //存储
  87. [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakSelf.fileDownUpModel];
  88. int i = 0;
  89. for (DownloadFailedUIBlock fileBlock in weakSelf.failureBlocksDict.allValues) {
  90. NSString *key = weakSelf.failureBlocksDict.allKeys[i];
  91. if (fileBlock) {
  92. //一方面,所有的model都注册了,另一方面,有时候多个model公共一个task.
  93. fileBlock(errMessage,weakSelf.fileDownUpModel,key);
  94. }
  95. i++;
  96. }
  97. if (weakSelf.downloadFailedDBBlock) {
  98. weakSelf.downloadFailedDBBlock(errMessage,weakSelf.fileDownUpModel,weakSelf.fileDownUpModel.downUpUUID);
  99. }
  100. }
  101. };
  102. self.downloadSession.downloadProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress,NSString *router,NSString *msgUUID){
  103. weakSelf.failedAndTryTimes = 0;
  104. weakSelf.fileDownUpModel.downUpSize = receivedSize;
  105. NSDate *now = [NSDate date];
  106. if (!weakSelf.lastRecordTime || [now timeIntervalSinceDate:weakSelf.lastRecordTime] >= 1) {
  107. //存储
  108. [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakSelf.fileDownUpModel];
  109. weakSelf.lastRecordTime = now;
  110. }
  111. int i = 0;
  112. for (DownloadProgressUIBlock downloadProgressBlock in weakSelf.progressBlocksDict.allValues){
  113. NSString *key = weakSelf.progressBlocksDict.allKeys[i];
  114. //可将key传出.
  115. if (downloadProgressBlock) {
  116. downloadProgressBlock(receivedSize,expectedSize,progress,router,weakSelf.fileDownUpModel,key);
  117. }
  118. i++;
  119. }
  120. if(weakSelf.downloadProgressDBBlock)
  121. {
  122. weakSelf.downloadProgressDBBlock(receivedSize,expectedSize,progress,router,weakSelf.fileDownUpModel,weakSelf.fileDownUpModel.downUpUUID);
  123. }
  124. };
  125. self.downloadSession.downloadSuccessBlock = ^(NSInteger receivedSize, NSString *localFullFileRouter,NSString *msgUUID){
  126. weakSelf.failedAndTryTimes = 0;
  127. weakSelf.fileDownUpModel.downUpSize = receivedSize;
  128. weakSelf.fileDownUpModel.downUpStatus = DownUpStatusSuccess;
  129. weakSelf.fileDownUpModel.localRootPath = localFullFileRouter;
  130. //存储
  131. [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakSelf.fileDownUpModel];
  132. int i = 0;
  133. for (DownloadSuccessUIBlock succBlock in weakSelf.successBlocksDict.allValues) {
  134. NSString *key = weakSelf.successBlocksDict.allKeys[i];
  135. if (succBlock) {
  136. succBlock(receivedSize,localFullFileRouter,weakSelf.fileDownUpModel,key);
  137. }
  138. i++;
  139. }
  140. //隔一秒?,否则上层通知完成和下层通知新的开始cell冲突 crash.
  141. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  142. if (weakSelf.downloadSuccessDBBlock) {
  143. weakSelf.downloadSuccessDBBlock(receivedSize,localFullFileRouter,weakSelf.fileDownUpModel,weakSelf.fileDownUpModel.downUpUUID);
  144. }
  145. });
  146. [weakSelf.successBlocksDict removeAllObjects];
  147. [weakSelf.failureBlocksDict removeAllObjects];
  148. [weakSelf.progressBlocksDict removeAllObjects];
  149. weakSelf.successBlocksDict = nil;
  150. weakSelf.failureBlocksDict = nil;
  151. weakSelf.progressBlocksDict = nil;
  152. };
  153. }
  154. }];
  155. }
  156. - (void)giveBlockToUIWithUUID:(NSString *)uuid withProcessBlock:(DownloadProgressUIBlock)processBlock successBlock:(DownloadSuccessUIBlock)successBlock failBlock:(DownloadFailedUIBlock)failBlock;
  157. {
  158. [self.successBlocksDict setObject:successBlock forKey:uuid];
  159. [self.progressBlocksDict setObject:processBlock forKey:uuid];
  160. [self.failureBlocksDict setObject:failBlock forKey:uuid];
  161. }
  162. - (void)cancerDownLoad
  163. {
  164. [self.downloadSession cancer];
  165. _fileDownUpModel = nil;
  166. [self.successBlocksDict removeAllObjects];
  167. [self.failureBlocksDict removeAllObjects];
  168. [self.progressBlocksDict removeAllObjects];
  169. self.successBlocksDict = nil;
  170. self.failureBlocksDict = nil;
  171. self.progressBlocksDict = nil;
  172. self.downloadSuccessDBBlock = nil;
  173. self.downloadFailedDBBlock = nil;
  174. self.downloadProgressDBBlock = nil;
  175. #warning 2017-07-18,待cell 新增pause block和start block. ->giveBlockToUIWithUUID
  176. //待通知所有注册的ui block上层暂停.(反之恢复的话也需要.)
  177. }
  178. @end