2025-04-12 13:23:12 +08:00
|
|
|
|
using System.Configuration;
|
|
|
|
|
|
using System.Windows;
|
2025-04-10 13:17:39 +08:00
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
using HeBianGu.Base.WpfBase;
|
|
|
|
|
|
using Hopetry.Models;
|
2025-04-12 13:23:12 +08:00
|
|
|
|
using Hopetry.Provider;
|
2025-04-10 13:17:39 +08:00
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Hopetry.Services;
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using Minio;
|
|
|
|
|
|
|
|
|
|
|
|
[SugarTable(TableName = "download_task")]
|
|
|
|
|
|
public class MinioDownloadTask : INotifyPropertyChanged
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly MinioService _minio;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
///
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private CancellationTokenSource _cts;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
///
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private ManualResetEventSlim _pauseEvent = new(true);
|
|
|
|
|
|
|
|
|
|
|
|
// 任务属性(绑定到UI)
|
2025-04-12 13:23:12 +08:00
|
|
|
|
[SugarColumn(ColumnName = "task_id", IsPrimaryKey = true, IsIdentity = true)]
|
|
|
|
|
|
public int TaskId { get; set; }
|
2025-04-10 13:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
[SugarColumn(ColumnName = "file_name")]
|
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
[SugarColumn(ColumnName = "bucket_name")]
|
|
|
|
|
|
public string Bucket { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
[SugarColumn(ColumnName = "object_key")]
|
|
|
|
|
|
public string ObjectKey { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
[SugarColumn(ColumnName = "total_size")]
|
|
|
|
|
|
public long TotalSize { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
[SugarColumn(ColumnName = "downloaded")]
|
|
|
|
|
|
public long Downloaded { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
[SugarColumn(ColumnName = "file_path")]
|
|
|
|
|
|
public string FilePath { get; set; }
|
|
|
|
|
|
|
2025-04-12 13:23:12 +08:00
|
|
|
|
[SugarColumn(ColumnName = "status")] public string Status { get; private set; } = "等待中";
|
2025-04-10 13:17:39 +08:00
|
|
|
|
|
2025-04-12 13:23:12 +08:00
|
|
|
|
[SugarColumn(IsIgnore = true)]
|
2025-04-10 13:17:39 +08:00
|
|
|
|
public string Progress
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return TotalSize == 0 ? "0%" : $"{(Downloaded * 100 / TotalSize):0.0}%"; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-12 13:23:12 +08:00
|
|
|
|
[SugarColumn(IsIgnore = true)] public ICommand PauseCommand { get; }
|
|
|
|
|
|
[SugarColumn(IsIgnore = true)] public ICommand CancelCommand { get; }
|
2025-04-10 13:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
public MinioDownloadTask()
|
|
|
|
|
|
{
|
|
|
|
|
|
PauseCommand = new CustomCommand(OnPause);
|
|
|
|
|
|
CancelCommand = new CustomCommand(OnCancel);
|
|
|
|
|
|
}
|
2025-04-12 13:23:12 +08:00
|
|
|
|
|
2025-04-10 13:17:39 +08:00
|
|
|
|
public MinioDownloadTask(MinioService minio, string bucket, string objectKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
_minio = minio;
|
|
|
|
|
|
Bucket = bucket;
|
|
|
|
|
|
ObjectKey = objectKey;
|
|
|
|
|
|
FileName = Path.GetFileName(objectKey);
|
2025-04-12 13:23:12 +08:00
|
|
|
|
FilePath = "f:\\dest";
|
2025-04-10 13:17:39 +08:00
|
|
|
|
PauseCommand = new CustomCommand(OnPause);
|
|
|
|
|
|
CancelCommand = new CustomCommand(OnCancel);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 下载
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="savePath"></param>
|
2025-04-12 13:23:12 +08:00
|
|
|
|
public async Task StartDownload()
|
2025-04-10 13:17:39 +08:00
|
|
|
|
{
|
|
|
|
|
|
_cts = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-04-12 13:23:12 +08:00
|
|
|
|
Status = "下载中";
|
|
|
|
|
|
var updateTask = new MinioDownloadTask
|
|
|
|
|
|
{
|
|
|
|
|
|
TaskId = TaskId,
|
|
|
|
|
|
Status = Status
|
|
|
|
|
|
};
|
2025-04-10 13:17:39 +08:00
|
|
|
|
|
2025-04-12 13:23:12 +08:00
|
|
|
|
using (var client = SqlSugarConfig.GetSqlSugarScope())
|
2025-04-10 13:17:39 +08:00
|
|
|
|
{
|
2025-04-12 13:23:12 +08:00
|
|
|
|
await client.Updateable(updateTask).IgnoreNullColumns().ExecuteCommandAsync();
|
2025-04-10 13:17:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-12 13:23:12 +08:00
|
|
|
|
Console.WriteLine($"{ObjectKey}文件下载中...");
|
|
|
|
|
|
updateTask.Status = "下载中";
|
|
|
|
|
|
await _minio.DownLoadObject(Bucket, ObjectKey, FilePath, "");
|
2025-04-10 13:17:39 +08:00
|
|
|
|
Status = "已完成";
|
2025-04-12 13:23:12 +08:00
|
|
|
|
updateTask.Status = Status;
|
|
|
|
|
|
using (var client = SqlSugarConfig.GetSqlSugarScope())
|
|
|
|
|
|
{
|
|
|
|
|
|
await client.Updateable(updateTask).IgnoreNullColumns().ExecuteCommandAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"文件{ObjectKey}下载完成");
|
2025-04-10 13:17:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
|
{
|
|
|
|
|
|
Status = "已取消";
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Status = $"错误:{ex.Message}";
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
_pauseEvent.Dispose();
|
|
|
|
|
|
OnPropertyChanged(nameof(Status));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnPause()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Status == "下载中")
|
|
|
|
|
|
{
|
|
|
|
|
|
_pauseEvent.Reset();
|
|
|
|
|
|
Status = "已暂停";
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (Status == "已暂停")
|
|
|
|
|
|
{
|
|
|
|
|
|
_pauseEvent.Set();
|
|
|
|
|
|
Status = "下载中";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OnPropertyChanged(nameof(Status));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnCancel()
|
|
|
|
|
|
{
|
|
|
|
|
|
_cts?.Cancel();
|
|
|
|
|
|
Status = "已取消";
|
|
|
|
|
|
OnPropertyChanged(nameof(Status));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
|
|
|
|
|
|
protected void OnPropertyChanged(string propertyName)
|
|
|
|
|
|
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
|
}
|