添加下载进度

dev2.0
陈伟 2025-04-15 10:29:41 +08:00
parent 7f4e7f7a06
commit 9f07f7edaf
4 changed files with 91 additions and 10 deletions

View File

@ -1,5 +1,6 @@
using System.Configuration;
using System.Windows;
using System.Windows.Forms.VisualStyles;
using System.Windows.Input;
using HeBianGu.Base.WpfBase;
using Hopetry.Models;
@ -67,10 +68,25 @@ public class MinioDownloadTask : INotifyPropertyChanged
[SugarColumn(ColumnName = "file_etag")]
public string FileETag { get; set; }
private string _progress;
[SugarColumn(IsIgnore = true)]
public string Progress
{
get { return TotalSize == 0 ? "0%" : $"{(Downloaded * 100 / TotalSize):0.0}%"; }
get
{
if (string.IsNullOrEmpty(_progress))
{
_progress = TotalSize == 0 ? "0%" : $"{(Downloaded * 100 / TotalSize):0.0}%";
}
return _progress;
}
set
{
_progress = value;
OnPropertyChanged(nameof(Progress));
}
}
[SugarColumn(IsIgnore = true)] public ICommand PauseCommand { get; }
@ -108,11 +124,17 @@ public class MinioDownloadTask : INotifyPropertyChanged
try
{
var stat = await _minio.GetObjectMetadata(Bucket, ObjectKey);
// 获取对象信息
TotalSize = stat.Size;
FileETag = stat.ETag;
Status = "下载中";
var updateTask = new MinioDownloadTask
{
TaskId = TaskId,
Status = Status
Status = Status,
TotalSize = TotalSize,
FileETag = FileETag
};
using (var client = SqlSugarConfig.GetSqlSugarScope())
@ -122,12 +144,22 @@ public class MinioDownloadTask : INotifyPropertyChanged
Console.WriteLine($"id {TaskId} path: {FilePath} key: {ObjectKey}文件下载中...");
updateTask.Status = "下载中";
TotalSize = 5L;
var stat = await _minio.GetObjectMetadata(Bucket, ObjectKey);
// 获取对象信息
TotalSize = stat.Size;
FileETag = stat.ETag;
await _minio.DownLoadObject(Bucket, ObjectKey, FilePath, "");
await _minio.DownLoadObjectWithCallBack(Bucket, ObjectKey, FilePath, FileETag,
(downloaded, total) =>
{
var progress = (double)downloaded / total * 100;
Downloaded = downloaded;
using (var client = SqlSugarConfig.GetSqlSugarScope())
{
updateTask.Downloaded = downloaded;
client.Updateable(updateTask).IgnoreNullColumns().ExecuteCommandAsync();
}
Progress = $"{progress:f2}%";
Console.WriteLine($"文件 {FileName} 进度 {Progress}");
});
//await _minio.DownLoadObject(Bucket, ObjectKey, FilePath, "");
Status = "已完成";
updateTask.Status = Status;
updateTask.FinishedTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

View File

@ -398,5 +398,54 @@ namespace Hopetry.Services
#endregion
}
public async Task DownLoadObjectWithCallBack(string bucketName, string objectKey, string filePath,
string fileETag, Action<long, long> action)
{
long totalBytes = 0;
var index = objectKey.LastIndexOf("/", StringComparison.Ordinal);
if (index > 0)
{
var dir = Path.Combine(filePath, objectKey.Substring(0, index));
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
var args = new StatObjectArgs()
.WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName)
.WithObject(objectKey);
var stat = await _minioClient.StatObjectAsync(args);
totalBytes = stat.Size;
var localPath = Path.Combine(filePath, objectKey.Replace('/', Path.DirectorySeparatorChar));
GetObjectArgs getObjectArgs = new GetObjectArgs()
.WithBucket(string.IsNullOrEmpty(bucketName) ? _bucketName : bucketName)
.WithObject(objectKey)
.WithCallbackStream((stream) =>
{
long bytesRead = 0;
byte[] buffer = new byte[64 * 1024]; // 64KB 缓冲区
int read;
using (var fileStream = File.Create(localPath))
{
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
bytesRead += read;
// 触发进度回调
action?.Invoke(bytesRead, totalBytes);
}
}
});
await _minioClient.GetObjectAsync(getObjectArgs);
/*if (VerifyETag(localPath, objectETag))
{
// todo 先忽略处理
}*/
Console.WriteLine($"{objectKey} Download complete");
}
}
}

View File

@ -84,7 +84,7 @@
Height="15"
CornerRadius="2"
Maximum="100"
Value="11" />
Value="{Binding Progress}" />
<TextBlock Grid.Row="1"
Grid.Column="3"

BIN
minio.db

Binary file not shown.