using System.Diagnostics; using System.IO; using System.Windows; using System.Windows.Input; using HeBianGu.Base.WpfBase; using HeBianGu.Service.Mvc; using Hopetry.Models; using Hopetry.Services; using Microsoft.WindowsAPICodePack.Dialogs; using SystemSetting = FileUploader.Models.SystemSetting; namespace Hopetry.ViewModel.Sync; [ViewModel("Sync")] public class SyncViewModel : MvcViewModelBase { public ICommand OpenDirCommand { get; set; } private readonly ISerializerService _serializerService; private readonly MinioService _minioService; private readonly SystemSetting _setting; public CustomCommand SyncData { get; set; } public CustomCommand ComboBox_SelectionChanged { get; set; } public CustomCommand OpenTargetDirCommand { get; set; } protected override void Init() { } public SyncViewModel(ISerializerService serializerService, MinioService minioService) { Console.WriteLine("SyncViewModel"); _minioService = minioService; _serializerService = serializerService; if (File.Exists("./settings.xml")) { _setting = _serializerService.Load("./settings.xml"); // 同步字段 TaskCount = _setting.TaskCount; SyncDir = _setting.SyncDir; } else { _setting = new SystemSetting(); } ComboBox_SelectionChanged = new CustomCommand(async () => await ComboBox_SelectionUpdate()); OpenDirCommand = new CustomCommand(async () => await ButtonBase_OnClick()); OpenTargetDirCommand = new CustomCommand(async () => await Button_OpenTargetDir()); SyncData = new CustomCommand(async () => await SyncDataQuick()); } private async Task ComboBox_SelectionUpdate() { _setting.TaskCount = TaskCount; _serializerService.Save("./settings.xml", _setting); } private async Task SyncDataQuick() { _minioService.MirrorAsync(_minioService._bucketName, SyncDir, _taskCount); } protected override void Loaded(string args) { Console.WriteLine("Loaded"); } private int _taskCount = 3; public int TaskCount { get => _taskCount; set { _taskCount = value; RaisePropertyChanged(); } } public string _syncDir; public string SyncDir { get => _syncDir; set { _syncDir = value; RaisePropertyChanged(); } } private async Task Button_OpenTargetDir() { // 验证目录是否存在 if (!Directory.Exists(SyncDir)) { MessageBox.Show("文件夹不存在"); return; } try { // 调用系统资源管理器打开目录 Process.Start(new ProcessStartInfo { FileName = "explorer.exe", Arguments = $"\"{SyncDir}\"", // 处理路径中的空格 UseShellExecute = true }); } catch (Exception ex) { MessageBox.Show($"打开失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } private async Task ButtonBase_OnClick() { CommonOpenFileDialog dialog = new CommonOpenFileDialog { IsFolderPicker = true }; if (dialog.ShowDialog() == CommonFileDialogResult.Ok) { var folderPath = dialog.FileName; // 获取选中的文件夹路径 //MessageBox.Show("folderPath " + folderPath); SyncDir = folderPath; _setting.SyncDir = folderPath; _serializerService.Save("./settings.xml", _setting); // 处理选中的文件夹路径 } } }