diff --git a/Ink Canvas/App.xaml.cs b/Ink Canvas/App.xaml.cs index dc941a74..38888ffa 100644 --- a/Ink Canvas/App.xaml.cs +++ b/Ink Canvas/App.xaml.cs @@ -1148,6 +1148,17 @@ namespace Ink_Canvas LogHelper.WriteLogToFile($"启动IPC监听器时出错: {ex.Message}", LogHelper.LogType.Error); } + // 初始化上传帮助类 + try + { + LogHelper.WriteLogToFile("初始化上传帮助类"); + Helpers.UploadHelper.Initialize(); + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"初始化上传帮助类时出错: {ex.Message}", LogHelper.LogType.Error); + } + } private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e) diff --git a/Ink Canvas/Helpers/UploadHelper.cs b/Ink Canvas/Helpers/UploadHelper.cs new file mode 100644 index 00000000..cef66c61 --- /dev/null +++ b/Ink Canvas/Helpers/UploadHelper.cs @@ -0,0 +1,188 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Ink_Canvas.Helpers +{ + /// + /// 上传提供者接口 + /// + public interface IUploadProvider + { + /// + /// 提供者名称 + /// + string Name { get; } + + /// + /// 是否启用 + /// + bool IsEnabled { get; } + + /// + /// 上传文件 + /// + /// 文件路径 + /// 是否上传成功 + Task UploadAsync(string filePath); + } + + /// + /// Dlass上传提供者 + /// + public class DlassUploadProvider : IUploadProvider + { + /// + /// 提供者名称 + /// + public string Name => "Dlass"; + + /// + /// 是否启用 + /// + public bool IsEnabled => MainWindow.Settings?.Dlass?.IsAutoUploadNotes ?? false; + + /// + /// 上传文件 + /// + /// 文件路径 + /// 是否上传成功 + public async Task UploadAsync(string filePath) + { + return await DlassNoteUploader.UploadNoteFileAsync(filePath); + } + } + + + + /// + /// 上传帮助类 + /// + public static class UploadHelper + { + private static readonly List _providers = new List(); + private static bool _initialized; + private static readonly object s_sync = new object(); + + /// + /// 初始化上传帮助类 + /// + public static void Initialize() + { + lock (s_sync) + { + if (_initialized) + return; + + // 注册默认上传提供者 + RegisterProviderInternal(new DlassUploadProvider()); + + _initialized = true; + } + } + + /// + /// 注册上传提供者 + /// + /// 上传提供者 + public static void RegisterProvider(IUploadProvider provider) + { + if (provider == null) + return; + + lock (s_sync) + { + RegisterProviderInternal(provider); + } + } + + private static void RegisterProviderInternal(IUploadProvider provider) + { + if (provider != null) + { + bool providerExists = _providers.Any(p => p.GetType() == provider.GetType()); + if (!providerExists) + { + _providers.Add(provider); + } + } + } + + /// + /// 上传文件到所有启用的提供者 + /// + /// 文件路径 + /// 是否至少有一个提供者上传成功 + public static async Task UploadFileAsync(string filePath) + { + if (!_initialized) + { + Initialize(); + } + + List providersSnapshot; + lock (s_sync) + { + providersSnapshot = new List(_providers); + } + + bool anySuccess = false; + + foreach (var provider in providersSnapshot) + { + try + { + if (provider.IsEnabled) + { + bool success = await provider.UploadAsync(filePath); + if (success) + { + anySuccess = true; + } + } + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"使用 {provider.Name} 上传失败: {ex}", LogHelper.LogType.Error); + } + } + + return anySuccess; + } + + /// + /// 获取所有上传提供者 + /// + /// 上传提供者列表 + public static List GetProviders() + { + if (!_initialized) + { + Initialize(); + } + + lock (s_sync) + { + return new List(_providers); + } + } + + /// + /// 获取所有启用的上传提供者 + /// + /// 启用的上传提供者列表 + public static List GetEnabledProviders() + { + if (!_initialized) + { + Initialize(); + } + + lock (s_sync) + { + return _providers.FindAll(p => p.IsEnabled); + } + } + } +} diff --git a/Ink Canvas/MainWindow_cs/MW_Screenshot.cs b/Ink Canvas/MainWindow_cs/MW_Screenshot.cs index fe5b694f..b944299b 100644 --- a/Ink Canvas/MainWindow_cs/MW_Screenshot.cs +++ b/Ink Canvas/MainWindow_cs/MW_Screenshot.cs @@ -58,7 +58,7 @@ namespace Ink_Canvas var path = savePath; var hideNotification = isHideNotification; - _ = Task.Run(() => + _ = Task.Run(async () => { try { @@ -84,11 +84,8 @@ namespace Ink_Canvas Dispatcher.Invoke(() => ShowNotification($"截图成功保存至 {path}")); } - if (Settings?.Dlass?.AutoUploadDelayMinutes > 0) - { - Task.Delay(TimeSpan.FromMinutes(Settings.Dlass.AutoUploadDelayMinutes)).GetAwaiter().GetResult(); - Helpers.DlassNoteUploader.UploadNoteFileAsync(path).GetAwaiter().GetResult(); - } + // 使用上传帮助类上传到所有启用的服务 + await Helpers.UploadHelper.UploadFileAsync(path); } catch (Exception ex) { @@ -365,13 +362,8 @@ namespace Ink_Canvas { try { - var delayMinutes = Settings?.Dlass?.AutoUploadDelayMinutes ?? 0; - if (delayMinutes > 0) - { - await Task.Delay(TimeSpan.FromMinutes(delayMinutes)); - } - - await Helpers.DlassNoteUploader.UploadNoteFileAsync(savePath); + // 使用上传帮助类上传到所有启用的服务 + await Helpers.UploadHelper.UploadFileAsync(savePath); } catch (Exception) { diff --git a/Ink Canvas/Resources/Settings.cs b/Ink Canvas/Resources/Settings.cs index d0a55b01..34372fea 100644 --- a/Ink Canvas/Resources/Settings.cs +++ b/Ink Canvas/Resources/Settings.cs @@ -31,6 +31,7 @@ namespace Ink_Canvas public CameraSettings Camera { get; set; } = new CameraSettings(); [JsonProperty("dlass")] public DlassSettings Dlass { get; set; } = new DlassSettings(); + [JsonProperty("security")] public Security Security { get; set; } = new Security(); } @@ -851,7 +852,14 @@ namespace Ink_Canvas [JsonProperty("isAutoUploadNotes")] public bool IsAutoUploadNotes { get; set; } = false; + private int _autoUploadDelayMinutes = 0; [JsonProperty("autoUploadDelayMinutes")] - public int AutoUploadDelayMinutes { get; set; } = 0; + public int AutoUploadDelayMinutes + { + get { return _autoUploadDelayMinutes; } + set { _autoUploadDelayMinutes = Math.Max(0, value); } + } } + + }