From 8bc9b46f0f4bfae9db2c1952766aa831f8baee34 Mon Sep 17 00:00:00 2001 From: onezens Date: Sat, 15 Sep 2018 11:58:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=89=B9=E5=B0=8F=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8B=E8=BD=BD=E5=AE=8C=E6=88=90=E5=90=8E=E5=B0=BA?= =?UTF-8?q?=E5=AF=B8=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YCDownloadSession/YCDownloadItem.m | 12 +++++++----- YCDownloadSession/YCDownloadManager.m | 22 ++++++++++++++-------- YCDownloadSession/core/YCDownloadUtils.m | 1 + 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/YCDownloadSession/YCDownloadItem.m b/YCDownloadSession/YCDownloadItem.m index b49f3d6..43412f4 100644 --- a/YCDownloadSession/YCDownloadItem.m +++ b/YCDownloadSession/YCDownloadItem.m @@ -117,8 +117,8 @@ - (void)setFileExtensionWithTask:(YCDownloadTask *)task { return; } NSAssert([response isKindOfClass:[NSHTTPURLResponse class]], @"response can not nil & class must be NSHTTPURLResponse"); - NSString *extension = response.suggestedFilename.pathExtension; - if(!extension) extension = [[response.allHeaderFields valueForKey:@"Content-Type"] componentsSeparatedByString:@"/"].lastObject; + NSString *extension = [[response.allHeaderFields valueForKey:@"Content-Type"] componentsSeparatedByString:@"/"].lastObject; + if(extension.length==0) extension = response.suggestedFilename.pathExtension; _fileExtension = extension; } @@ -128,7 +128,7 @@ - (YCProgressHandler)progressHandler { if(weakSelf.downloadStatus == YCDownloadStatusWaiting){ [weakSelf downloadStatusChanged:YCDownloadStatusDownloading downloadTask:nil]; } - [weakSelf downloadProgress:task downloadedSize:(NSUInteger)progress.completedUnitCount fileSize:(NSUInteger)progress.totalUnitCount]; + [weakSelf downloadProgress:task downloadedSize:(NSUInteger)progress.completedUnitCount fileSize:(NSUInteger)(progress.totalUnitCount>0 ? progress.totalUnitCount : 0)]; }; } @@ -148,7 +148,9 @@ - (YCCompletionHandler)completionHandler { [weakSelf downloadStatusChanged:YCDownloadStatusFailed downloadTask:nil]; }else if([[NSFileManager defaultManager] moveItemAtPath:localPath toPath:self.savePath error:&saveError]){ NSAssert(self.fileExtension, @"file extension can not nil!"); - self->_downloadedSize = weakSelf.fileSize; + NSUInteger fileSize = [YCDownloadUtils fileSizeWithPath:weakSelf.savePath]; + self->_downloadedSize = fileSize; + self->_fileSize = fileSize; [weakSelf downloadStatusChanged:YCDownloadStatusFinished downloadTask:nil]; }else{ [weakSelf downloadStatusChanged:YCDownloadStatusFailed downloadTask:nil]; @@ -177,7 +179,7 @@ - (NSString *)saveDirectory { - (NSString *)saveName { NSString *saveName = self.fileId ? self.fileId : self.taskId; - return [saveName stringByAppendingPathExtension: self.fileExtension ? : @"data"]; + return [saveName stringByAppendingPathExtension: self.fileExtension.length>0 ? self.fileExtension : @"data"]; } - (NSString *)savePath { diff --git a/YCDownloadSession/YCDownloadManager.m b/YCDownloadSession/YCDownloadManager.m index 27f80c0..b0dacd2 100644 --- a/YCDownloadSession/YCDownloadManager.m +++ b/YCDownloadSession/YCDownloadManager.m @@ -44,6 +44,7 @@ + (void)mgrWithConfig:(YCDConfig *)config { } + (instancetype)manager { + NSAssert(_instance, @"please set config: [YCDownloadManager mgrWithConfig:config];"); return _instance; } @@ -212,16 +213,15 @@ - (BOOL)canResumeDownload { - (void)startDownloadWithItem:(YCDownloadItem *)item priority:(float)priority{ if(!item) return; YCDownloadItem *oldItem = [YCDownloadDB itemWithTaskId:item.taskId]; - if (oldItem.downloadStatus == YCDownloadStatusFinished) return; - item.downloadStatus = YCDownloadStatusWaiting; - item.uid = self.uid; - item.saveRootPath = self.config.saveRootPath; - item.fileType = item.fileType ? : @"video"; - if ([self downloadFinishedWithItem:item]) { + if (oldItem && [self downloadFinishedWithItem:oldItem]) { NSLog(@"[startDownloadWithItem] detect item finished!"); [self startNextDownload]; return; } + item.downloadStatus = YCDownloadStatusWaiting; + item.uid = self.uid; + item.saveRootPath = self.config.saveRootPath; + item.fileType = item.fileType ? : @"video"; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:item.downloadURL]]; YCDownloadTask *task = [[YCDownloader downloader] downloadWithRequest:request progress:item.progressHandler completion:item.completionHandler priority:priority]; item.taskId = task.taskId; @@ -320,8 +320,10 @@ - (void)stopDownloadWithItem:(YCDownloadItem *)item { - (void)pauseAllDownloadTask { [[YCDownloadDB fetchAllDownloadingItemWithUid:self.uid] enumerateObjectsUsingBlock:^(YCDownloadItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { - obj.noNeedStartNext = true; - [self pauseDownloadWithItem:obj]; + if (obj.downloadStatus == YCDownloadStatusWaiting || obj.downloadStatus == YCDownloadStatusDownloading) { + obj.noNeedStartNext = true; + [self pauseDownloadWithItem:obj]; + } }]; } @@ -367,4 +369,8 @@ -(void)dealloc { @implementation YCDConfig +- (NSUInteger)maxTaskCount { + return _maxTaskCount ? _maxTaskCount : 1; +} + @end diff --git a/YCDownloadSession/core/YCDownloadUtils.m b/YCDownloadSession/core/YCDownloadUtils.m index 91d747e..43fde12 100644 --- a/YCDownloadSession/core/YCDownloadUtils.m +++ b/YCDownloadSession/core/YCDownloadUtils.m @@ -281,6 +281,7 @@ + (void)getSqlWithKeys:(const char **)keys count:(int)count obj:(id)obj oldItem isEqual = false; }else{ NSAssert(value==nil && oValue==nil, @"cls err"); + isEqual = true; } if (!isEqual) { enumerateBlock(value ? type : [self getTypeWithValue:oValue], key, value, i);