using HeBianGu.Base.WpfBase; using HeBianGu.Service.Mvc; using Hopetry.Services; using Microsoft.Win32; using Minio.DataModel.Args; using Minio.DataModel; using System; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Input; using System.Windows.Threading; using Minio; using Microsoft.Extensions.Configuration; using Hopetry.ViewModel.Send; using System.IO; using System.Security.AccessControl; using System.Diagnostics; namespace HeBianGu.App.Disk { [ViewModel("Loyout")] internal class LoyoutViewModel : MvcViewModelBase { private string _path; /// 说明 public string Path { get { return _path; } set { _path = value; RaisePropertyChanged("Path"); } } private string _nearPath; /// 说明 public string NearPath { get { return _nearPath; } set { _nearPath = value; RaisePropertyChanged("NearPath"); } } private string _sharePath; /// 说明 public string SharePath { get { return _sharePath; } set { _sharePath = value; RaisePropertyChanged("SharePath"); } } protected override void Init() { Path = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); NearPath = Environment.GetFolderPath(Environment.SpecialFolder.Recent); SharePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // LinkActions.Add(new LinkAction() { Action = "Near", Controller = "Loyout", DisplayName = "最近使用", Logo = "\xe6f3" }); LinkActions.Add(new LinkAction() { Action = "Explorer", Controller = "Loyout", DisplayName = "全部文件", Logo = "" }); // LinkActions.Add(new LinkAction() { Action = "Image", Controller = "Loyout", DisplayName = " 图片", Logo = "" }); // LinkActions.Add(new LinkAction() { Action = "Video", Controller = "Loyout", DisplayName = " 视频", Logo = "" }); // LinkActions.Add(new LinkAction() { Action = "Document", Controller = "Loyout", DisplayName = " 文档", Logo = "" }); // LinkActions.Add(new LinkAction() { Action = "Music", Controller = "Loyout", DisplayName = " 音乐", Logo = "" }); // LinkActions.Add(new LinkAction() { Action = "Explorer", Controller = "Loyout", DisplayName = " 种子", Logo = "" }); //LinkActions.Add(new LinkAction() { Action = "Recent", Controller = "Loyout", DisplayName = " 其他", Logo = "" }); // LinkActions.Add(new LinkAction() { Action = "Space", Controller = "Loyout", DisplayName = "隐藏空间", Logo = "\xe613" }); //LinkActions.Add(new LinkAction() { Action = "Share", Controller = "Loyout", DisplayName = "我的分享", Logo = "\xe764" }); // LinkActions.Add(new LinkAction() { Action = "Near", Controller = "Loyout", DisplayName = "回收站", Logo = "\xe618" }); Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => { SelectLink = LinkActions[0]; })); } protected override void Loaded(string args) { } #region 文件上传 private SendViewModel _sendViewModel; private IConfiguration config; public ICommand UploadCommand { get; } public LoyoutViewModel(SendViewModel sendViewModel, IConfiguration _config) { _sendViewModel = sendViewModel; config = _config; UploadCommand = new AsyncRelayCommand(async () => await UploadFile()); } private SemaphoreSlim _semaphore = new SemaphoreSlim(5); private async Task UploadFile() { _sendViewModel.UpLoadItems.Clear(); var openFileDialog = new Microsoft.Win32.OpenFileDialog(); openFileDialog.Multiselect = true; // 可选择多个文件 openFileDialog.Filter = "All Files (*.*)|*.*"; //openFileDialog.Multiselect = false; if (openFileDialog.ShowDialog() == true) { foreach (string filePath in openFileDialog.FileNames) { UpLoadItems ut = new UpLoadItems(); ut.Value = System.IO.Path.GetFileName(filePath); ut.Value3 = "等待上传"; ut.Value5 = filePath; FileInfo fileInfo = new FileInfo(filePath); ut.Double1 = fileInfo.Length; ut.Double2 = 0.0; if (fileInfo.Exists) { if (fileInfo.Length < 1024 * 1024) { ut.Value1 = "0KB/" + Math.Round((double)(fileInfo.Length / 1024), 2).ToString() + "KB"; } else { ut.Value1 = "0MB/" + Math.Round((double)(fileInfo.Length / (1024 * 1024)), 2).ToString() + "MB"; } } _sendViewModel.UpLoadItems.Add(ut); } //更新总进度 var timer = new System.Timers.Timer(1000); // 每秒更新一次 timer.Elapsed += (sender, e) => { double currentBytes = _sendViewModel.UpLoadItems.Sum(r=>r.Double2); double totalBytes = _sendViewModel.UpLoadItems.Sum(r => r.Double1); double speed = Math.Round((currentBytes / totalBytes)*100,0); // KB/s _sendViewModel.Progress = speed; }; timer.Start(); try { // 创建所有上传任务 var uploadTasks = _sendViewModel.UpLoadItems .Select(async item => { await _semaphore.WaitAsync(); // 等待信号量 try { await UploadFileToMinIOWithProgress(item); } finally { _semaphore.Release(); // 释放信号量 } }) .ToList(); // 等待所有上传任务完成 await Task.WhenAll(uploadTasks); } finally { //更新总进度100% _sendViewModel.Progress = 100; // 关机 if (MySetting.Instance.IsOn) { Shutdown(); } // 停止计时器 timer.Stop(); } } } private async Task UploadFileToMinIOWithProgress(UpLoadItems ut) { // 配置 MinIO 客户端 IMinioClient client = new Minio.MinioClient() .WithEndpoint("123.132.248.154:9107") // MinIO 服务器地址 .WithCredentials("KcJPKzOsKfVq20EA4Lyh", "HY7K5EkYpRUphEdINZmAq34tsZD3PMLFTnL85y4N") // 访问密钥和密钥 .Build(); try { string bucketName = "test"; string objectName = System.IO.Path.GetFileName(ut.Value5); //var aa = _sendViewModel.UpLoadItems.Where(r => r.Value == objectName).FirstOrDefault(); //判断桶是否存在 var beArgs = new BucketExistsArgs().WithBucket(bucketName); bool found = await client.BucketExistsAsync(beArgs).ConfigureAwait(false); if (!found) { var mbArgs = new MakeBucketArgs() .WithBucket(bucketName); await client.MakeBucketAsync(mbArgs).ConfigureAwait(false); } var progress = new Progress(progressReport => { // 确保在 UI 线程上更新 Progress Application.Current.Dispatcher.Invoke(() => { //_sendViewModel.Progress = progressReport.Percentage; ut.Int1 = progressReport.Percentage; int slashIndex = ut.Value1.IndexOf('/'); long trans = progressReport.TotalBytesTransferred; ut.Double2 = trans; if (trans < 1024 * 1024) { ut.Value1 = Math.Round((double)(trans / 1024), 0).ToString() + "KB" + ut.Value1.Substring(slashIndex); } else { ut.Value1 = Math.Round((double)(trans / (1024 * 1024)), 2).ToString() + "MB" + ut.Value1.Substring(slashIndex); } ut.Value3 = "上传中..."; }); }); PutObjectArgs putObjectArgs = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithFileName(ut.Value5) //.WithContentType("application/octet-stream") .WithProgress(progress); // 上传文件并提供进度反馈 await client.PutObjectAsync(putObjectArgs); ut.Value3 = "已完成"; //MessageBox.Show("文件上传成功!"); } catch (Exception ex) { MessageBox.Show($"上传失败: {ex.Message}"); } } // 关机方法 private void Shutdown() { try { // 使用shutdown命令关机 Process.Start("shutdown", "/s /t 0"); } catch (System.ComponentModel.Win32Exception ex) { MessageBox.Show($"关机失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } #endregion } internal class DataFileViewModel : ObservableSourceViewModel { protected override void Init() { base.Init(); } } }