Skip to content

Commit

Permalink
解决特小文件下载完成后尺寸异常的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
onezens committed Sep 15, 2018
1 parent 216221f commit 8bc9b46
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
12 changes: 7 additions & 5 deletions YCDownloadSession/YCDownloadItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)];
};
}

Expand All @@ -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];
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 14 additions & 8 deletions YCDownloadSession/YCDownloadManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ + (void)mgrWithConfig:(YCDConfig *)config {
}

+ (instancetype)manager {
NSAssert(_instance, @"please set config: [YCDownloadManager mgrWithConfig:config];");
return _instance;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
}];
}

Expand Down Expand Up @@ -367,4 +369,8 @@ -(void)dealloc {

@implementation YCDConfig

- (NSUInteger)maxTaskCount {
return _maxTaskCount ? _maxTaskCount : 1;
}

@end
1 change: 1 addition & 0 deletions YCDownloadSession/core/YCDownloadUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8bc9b46

Please sign in to comment.