| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- //
- // CloudDownloadSession.m
- // zhuxun
- //
- // Created by winsoft on 17/6/29.
- //
- //
- #import "CloudDownloadSession.h"
- #import "HttpRequest.h"
- #import "CloudStorageDB.h"
- #define MaxTryTimes 3
- @interface CloudDownloadSession()
- @property (nonatomic , strong) NSMutableDictionary *successBlocksDict;
- @property (nonatomic , strong) NSMutableDictionary *failureBlocksDict;
- @property (nonatomic , strong) NSMutableDictionary *progressBlocksDict;
- @property (nonatomic , strong) WSDownloadSession *downloadSession;
- @property (nonatomic , copy) NSDate *lastRecordTime;
- /**
- * 失败重试次数
- */
- @property (nonatomic , assign) NSInteger failedAndTryTimes;
- @end
- @implementation CloudDownloadSession
- - (NSMutableDictionary *)successBlocksDict
- {
- if (!_successBlocksDict) {
- _successBlocksDict = [NSMutableDictionary dictionary];
- }
-
- return _successBlocksDict;
- }
- - (NSMutableDictionary *)failureBlocksDict
- {
- if (!_failureBlocksDict) {
- _failureBlocksDict = [NSMutableDictionary dictionary];
- }
-
- return _failureBlocksDict;
- }
- - (NSMutableDictionary *)progressBlocksDict
- {
- if (!_progressBlocksDict) {
- _progressBlocksDict = [NSMutableDictionary dictionary];
- }
-
- return _progressBlocksDict;
- }
- + (CloudDownloadSession *)startDownloadFileWithFileDownUpModel:(CSFileDownUpModel *)fileDownUpModel
- {
- CloudDownloadSession *downLoadSession = [[CloudDownloadSession alloc]initDowunLoadFileWithFileDownUpModel:fileDownUpModel];
-
- return downLoadSession;
- }
- - (instancetype)initDowunLoadFileWithFileDownUpModel:(CSFileDownUpModel *)fileDownUpModel
- {
- if (self = [super init]) {
-
-
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
-
- //查找数据库中md5不同,name,路径相同文件.
- [kCloudStorageDB getCSDownUpFileSameNameDifMd5WithMD5:fileDownUpModel.md5 name:fileDownUpModel.name localPath:fileDownUpModel.localRootPath downUpModel:DownUpModelDown resultBlock:^(NSInteger sameNameFileCount) {
-
- //修改存放路径
- if (sameNameFileCount) {
- #warning 2017-08-02 命名可以再优化
- fileDownUpModel.localRootPath = [NSString stringWithFormat:@"%@_%@",fileDownUpModel.md5,fileDownUpModel.localRootPath];
- //存储.
- [kCloudStorageDB updateCSDownUpFileLocalPathWithCSFileDownUpModel:fileDownUpModel newLocalPath:fileDownUpModel.localRootPath];
-
-
- }
-
- _fileDownUpModel = fileDownUpModel;
-
- [self startDownloadFileWithfileDownUpModel:fileDownUpModel];
-
-
-
- }];
-
-
- });
-
-
-
- }
-
- return self;
- }
- - (void)startDownloadFileWithfileDownUpModel:(CSFileDownUpModel *)fileDownUpModel
- {
- // __weak typeof(self)weakSelf = self;
-
- // return;
-
- [[HttpRequest sharedManager]cloudStorageGetFileDownloadURLWithMD5:fileDownUpModel.md5 complete:^(BOOL success, id data, NSError *error) {
-
-
- if (success && data) {
-
- NSString *url = data[@"url"];
-
-
- 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
- successblock:nil failedBlock:nil];
- __weak typeof(self)weakSelf = self;
- self.downloadSession.downloadFailedBlock = ^(NSString* errMessage,NSString *msgUUID){
-
- if (weakSelf.failedAndTryTimes < MaxTryTimes) {
- [weakSelf startDownloadFileWithfileDownUpModel:weakSelf.fileDownUpModel];
- weakSelf.failedAndTryTimes ++;
- }else{
-
- weakSelf.fileDownUpModel.downUpStatus = DownUpStatusFailure;
-
- //存储
- [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakSelf.fileDownUpModel];
-
- int i = 0;
- for (DownloadFailedUIBlock fileBlock in weakSelf.failureBlocksDict.allValues) {
-
- NSString *key = weakSelf.failureBlocksDict.allKeys[i];
- if (fileBlock) {
- //一方面,所有的model都注册了,另一方面,有时候多个model公共一个task.
- fileBlock(errMessage,weakSelf.fileDownUpModel,key);
- }
- i++;
- }
-
- if (weakSelf.downloadFailedDBBlock) {
- weakSelf.downloadFailedDBBlock(errMessage,weakSelf.fileDownUpModel,weakSelf.fileDownUpModel.downUpUUID);
- }
- }
-
-
- };
-
- self.downloadSession.downloadProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, CGFloat progress,NSString *router,NSString *msgUUID){
-
- weakSelf.failedAndTryTimes = 0;
-
- weakSelf.fileDownUpModel.downUpSize = receivedSize;
-
-
- NSDate *now = [NSDate date];
-
-
- if (!weakSelf.lastRecordTime || [now timeIntervalSinceDate:weakSelf.lastRecordTime] >= 1) {
-
- //存储
- [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakSelf.fileDownUpModel];
-
- weakSelf.lastRecordTime = now;
-
-
- }
- int i = 0;
- for (DownloadProgressUIBlock downloadProgressBlock in weakSelf.progressBlocksDict.allValues){
-
- NSString *key = weakSelf.progressBlocksDict.allKeys[i];
- //可将key传出.
- if (downloadProgressBlock) {
- downloadProgressBlock(receivedSize,expectedSize,progress,router,weakSelf.fileDownUpModel,key);
- }
- i++;
- }
-
- if(weakSelf.downloadProgressDBBlock)
- {
- weakSelf.downloadProgressDBBlock(receivedSize,expectedSize,progress,router,weakSelf.fileDownUpModel,weakSelf.fileDownUpModel.downUpUUID);
- }
-
-
-
- };
-
- self.downloadSession.downloadSuccessBlock = ^(NSInteger receivedSize, NSString *localFullFileRouter,NSString *msgUUID){
-
- weakSelf.failedAndTryTimes = 0;
-
- weakSelf.fileDownUpModel.downUpSize = receivedSize;
- weakSelf.fileDownUpModel.downUpStatus = DownUpStatusSuccess;
- weakSelf.fileDownUpModel.localRootPath = localFullFileRouter;
- //存储
- [kCloudStorageDB updateCSDownUpFileStatusWithCSFileDownUpModel:weakSelf.fileDownUpModel];
-
- int i = 0;
- for (DownloadSuccessUIBlock succBlock in weakSelf.successBlocksDict.allValues) {
-
- NSString *key = weakSelf.successBlocksDict.allKeys[i];
- if (succBlock) {
- succBlock(receivedSize,localFullFileRouter,weakSelf.fileDownUpModel,key);
- }
-
- i++;
- }
- //隔一秒?,否则上层通知完成和下层通知新的开始cell冲突 crash.
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- if (weakSelf.downloadSuccessDBBlock) {
- weakSelf.downloadSuccessDBBlock(receivedSize,localFullFileRouter,weakSelf.fileDownUpModel,weakSelf.fileDownUpModel.downUpUUID);
- }
- });
-
-
- [weakSelf.successBlocksDict removeAllObjects];
- [weakSelf.failureBlocksDict removeAllObjects];
- [weakSelf.progressBlocksDict removeAllObjects];
-
- weakSelf.successBlocksDict = nil;
- weakSelf.failureBlocksDict = nil;
- weakSelf.progressBlocksDict = nil;
- };
-
- }
-
-
-
-
- }];
-
- }
- - (void)giveBlockToUIWithUUID:(NSString *)uuid withProcessBlock:(DownloadProgressUIBlock)processBlock successBlock:(DownloadSuccessUIBlock)successBlock failBlock:(DownloadFailedUIBlock)failBlock;
- {
- [self.successBlocksDict setObject:successBlock forKey:uuid];
- [self.progressBlocksDict setObject:processBlock forKey:uuid];
- [self.failureBlocksDict setObject:failBlock forKey:uuid];
-
- }
- - (void)cancerDownLoad
- {
- [self.downloadSession cancer];
-
- _fileDownUpModel = nil;
-
- [self.successBlocksDict removeAllObjects];
- [self.failureBlocksDict removeAllObjects];
- [self.progressBlocksDict removeAllObjects];
-
- self.successBlocksDict = nil;
- self.failureBlocksDict = nil;
- self.progressBlocksDict = nil;
-
- self.downloadSuccessDBBlock = nil;
- self.downloadFailedDBBlock = nil;
- self.downloadProgressDBBlock = nil;
- #warning 2017-07-18,待cell 新增pause block和start block. ->giveBlockToUIWithUUID
- //待通知所有注册的ui block上层暂停.(反之恢复的话也需要.)
- }
- @end
|