feat(Upload):解耦Dlass上传并使用UploadHelper接管 (#380)
* feat(Upload):解耦Dlass笔记上传 Signed-off-by: doudou0720 <98651603+doudou0720@users.noreply.github.com> * fix(上传): 修复多线程环境下的上传提供者管理问题 添加线程同步锁确保上传提供者列表的线程安全 修改AutoUploadDelayMinutes属性确保最小值为0 优化提供者注册逻辑避免重复注册 Signed-off-by: doudou0720 <98651603+doudou0720@users.noreply.github.com> --------- Signed-off-by: doudou0720 <98651603+doudou0720@users.noreply.github.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ink_Canvas.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// 上传提供者接口
|
||||
/// </summary>
|
||||
public interface IUploadProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 提供者名称
|
||||
/// </summary>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
bool IsEnabled { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="filePath">文件路径</param>
|
||||
/// <returns>是否上传成功</returns>
|
||||
Task<bool> UploadAsync(string filePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dlass上传提供者
|
||||
/// </summary>
|
||||
public class DlassUploadProvider : IUploadProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// 提供者名称
|
||||
/// </summary>
|
||||
public string Name => "Dlass";
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用
|
||||
/// </summary>
|
||||
public bool IsEnabled => MainWindow.Settings?.Dlass?.IsAutoUploadNotes ?? false;
|
||||
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="filePath">文件路径</param>
|
||||
/// <returns>是否上传成功</returns>
|
||||
public async Task<bool> UploadAsync(string filePath)
|
||||
{
|
||||
return await DlassNoteUploader.UploadNoteFileAsync(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 上传帮助类
|
||||
/// </summary>
|
||||
public static class UploadHelper
|
||||
{
|
||||
private static readonly List<IUploadProvider> _providers = new List<IUploadProvider>();
|
||||
private static bool _initialized;
|
||||
private static readonly object s_sync = new object();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化上传帮助类
|
||||
/// </summary>
|
||||
public static void Initialize()
|
||||
{
|
||||
lock (s_sync)
|
||||
{
|
||||
if (_initialized)
|
||||
return;
|
||||
|
||||
// 注册默认上传提供者
|
||||
RegisterProviderInternal(new DlassUploadProvider());
|
||||
|
||||
_initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册上传提供者
|
||||
/// </summary>
|
||||
/// <param name="provider">上传提供者</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 上传文件到所有启用的提供者
|
||||
/// </summary>
|
||||
/// <param name="filePath">文件路径</param>
|
||||
/// <returns>是否至少有一个提供者上传成功</returns>
|
||||
public static async Task<bool> UploadFileAsync(string filePath)
|
||||
{
|
||||
if (!_initialized)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
List<IUploadProvider> providersSnapshot;
|
||||
lock (s_sync)
|
||||
{
|
||||
providersSnapshot = new List<IUploadProvider>(_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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有上传提供者
|
||||
/// </summary>
|
||||
/// <returns>上传提供者列表</returns>
|
||||
public static List<IUploadProvider> GetProviders()
|
||||
{
|
||||
if (!_initialized)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
lock (s_sync)
|
||||
{
|
||||
return new List<IUploadProvider>(_providers);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有启用的上传提供者
|
||||
/// </summary>
|
||||
/// <returns>启用的上传提供者列表</returns>
|
||||
public static List<IUploadProvider> GetEnabledProviders()
|
||||
{
|
||||
if (!_initialized)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
lock (s_sync)
|
||||
{
|
||||
return _providers.FindAll(p => p.IsEnabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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); }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user