添加下载进度
parent
7f4e7f7a06
commit
9f07f7edaf
|
|
@ -1,5 +1,6 @@
|
||||||
using System.Configuration;
|
using System.Configuration;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
|
using System.Windows.Forms.VisualStyles;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
using HeBianGu.Base.WpfBase;
|
using HeBianGu.Base.WpfBase;
|
||||||
using Hopetry.Models;
|
using Hopetry.Models;
|
||||||
|
|
@ -66,11 +67,26 @@ public class MinioDownloadTask : INotifyPropertyChanged
|
||||||
|
|
||||||
[SugarColumn(ColumnName = "file_etag")]
|
[SugarColumn(ColumnName = "file_etag")]
|
||||||
public string FileETag { get; set; }
|
public string FileETag { get; set; }
|
||||||
|
|
||||||
|
private string _progress;
|
||||||
|
|
||||||
[SugarColumn(IsIgnore = true)]
|
[SugarColumn(IsIgnore = true)]
|
||||||
public string Progress
|
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; }
|
[SugarColumn(IsIgnore = true)] public ICommand PauseCommand { get; }
|
||||||
|
|
@ -108,11 +124,17 @@ public class MinioDownloadTask : INotifyPropertyChanged
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var stat = await _minio.GetObjectMetadata(Bucket, ObjectKey);
|
||||||
|
// 获取对象信息
|
||||||
|
TotalSize = stat.Size;
|
||||||
|
FileETag = stat.ETag;
|
||||||
Status = "下载中";
|
Status = "下载中";
|
||||||
var updateTask = new MinioDownloadTask
|
var updateTask = new MinioDownloadTask
|
||||||
{
|
{
|
||||||
TaskId = TaskId,
|
TaskId = TaskId,
|
||||||
Status = Status
|
Status = Status,
|
||||||
|
TotalSize = TotalSize,
|
||||||
|
FileETag = FileETag
|
||||||
};
|
};
|
||||||
|
|
||||||
using (var client = SqlSugarConfig.GetSqlSugarScope())
|
using (var client = SqlSugarConfig.GetSqlSugarScope())
|
||||||
|
|
@ -122,12 +144,22 @@ public class MinioDownloadTask : INotifyPropertyChanged
|
||||||
|
|
||||||
Console.WriteLine($"id {TaskId} path: {FilePath} key: {ObjectKey}文件下载中...");
|
Console.WriteLine($"id {TaskId} path: {FilePath} key: {ObjectKey}文件下载中...");
|
||||||
updateTask.Status = "下载中";
|
updateTask.Status = "下载中";
|
||||||
TotalSize = 5L;
|
|
||||||
var stat = await _minio.GetObjectMetadata(Bucket, ObjectKey);
|
await _minio.DownLoadObjectWithCallBack(Bucket, ObjectKey, FilePath, FileETag,
|
||||||
// 获取对象信息
|
(downloaded, total) =>
|
||||||
TotalSize = stat.Size;
|
{
|
||||||
FileETag = stat.ETag;
|
var progress = (double)downloaded / total * 100;
|
||||||
await _minio.DownLoadObject(Bucket, ObjectKey, FilePath, "");
|
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 = "已完成";
|
Status = "已完成";
|
||||||
updateTask.Status = Status;
|
updateTask.Status = Status;
|
||||||
updateTask.FinishedTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
updateTask.FinishedTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
|
||||||
|
|
@ -398,5 +398,54 @@ namespace Hopetry.Services
|
||||||
|
|
||||||
#endregion
|
#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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +84,7 @@
|
||||||
Height="15"
|
Height="15"
|
||||||
CornerRadius="2"
|
CornerRadius="2"
|
||||||
Maximum="100"
|
Maximum="100"
|
||||||
Value="11" />
|
Value="{Binding Progress}" />
|
||||||
|
|
||||||
<TextBlock Grid.Row="1"
|
<TextBlock Grid.Row="1"
|
||||||
Grid.Column="3"
|
Grid.Column="3"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue