Skip to content

Commit 204d728

Browse files
authored
修复断点续传功能 (#1050)
1 parent 1cba9b0 commit 204d728

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

BBDown/BBDownDownloadUtil.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ public class DownloadConfig
2626
private static async Task RangeDownloadToTmpAsync(int id, string url, string tmpName, long fromPosition, long? toPosition, Action<int, long, long> onProgress, bool failOnRangeNotSupported = false)
2727
{
2828
DateTimeOffset? lastTime = File.Exists(tmpName) ? new FileInfo(tmpName).LastWriteTimeUtc : null;
29-
using var fileStream = new FileStream(tmpName, FileMode.Create);
29+
using var fileStream = new FileStream(tmpName, FileMode.OpenOrCreate);
3030
fileStream.Seek(0, SeekOrigin.End);
31+
if (toPosition > 0 && fileStream.Position == toPosition - fromPosition + 1)
32+
{
33+
// 已下载完成 直接汇报进度并跳过下载
34+
onProgress(id, fileStream.Position, fileStream.Position);
35+
return;
36+
}
3137
var downloadedBytes = fromPosition + fileStream.Position;
3238

3339
using var httpRequestMessage = new HttpRequestMessage();

BBDown/Program.Methods.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,6 @@ private static async Task DownloadTrackAsync(string url, string destPath, Downlo
503503
{
504504
if (downloadConfig.MultiThread && !url.Contains("-cmcc-"))
505505
{
506-
// 下载前先清理残片
507-
foreach (var file in new DirectoryInfo(Path.GetDirectoryName(destPath)!).EnumerateFiles("*.?clip")) file.Delete();
508506
await MultiThreadDownloadFileAsync(url, destPath, downloadConfig);
509507
Log($"合并{(video ? "视频" : "音频")}分片...");
510508
CombineMultipleFilesIntoSingleFile(GetFiles(Path.GetDirectoryName(destPath)!, $".{(video ? "v" : "a")}clip"), destPath);

BBDown/ProgressBar.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,9 @@ private void TimerHandler(object? state)
9393
if (disposed) return;
9494

9595
int progressBlockCount = (int)(currentProgress * blockCount);
96-
int percent = (int)(currentProgress * 100);
97-
string text = string.Format(" [{0}{1}] {2,3}% {3}{4}",
98-
new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount),
99-
percent,
96+
double percent = currentProgress * 100;
97+
string text = string.Format(" [{0}{1}] {2,3:0.00}% {3}{4}",
98+
new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount), percent,
10099
animation[animationIndex++ % animation.Length],
101100
speedString);
102101
UpdateText(text);

0 commit comments

Comments
 (0)