From ecf792c0315e366f02f971da5957df6bdcd3f906 Mon Sep 17 00:00:00 2001 From: JusterZhu Date: Sun, 24 May 2026 19:37:04 +0800 Subject: [PATCH] =?UTF-8?q?Batch=2012:=20StorageManager=20instance=20+=20C?= =?UTF-8?q?ancellationToken=E8=B4=AF=E7=A9=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add CancellationToken parameter to VersionService.Validate/Report static API - Propagate CancellationToken through HttpDownloadSource - Create IStorageManager interface with BackupAsync/RestoreAsync/CleanBackupAsync - Create DefaultStorageManager implementation wrapping static StorageManager Closes #383 --- .../Download/Sources/HttpDownloadSource.cs | 4 +- .../FileSystem/IStorageManager.cs | 59 +++++++++++++++++++ .../Network/VersionService.cs | 10 ++-- 3 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 src/c#/GeneralUpdate.Core/FileSystem/IStorageManager.cs diff --git a/src/c#/GeneralUpdate.Core/Download/Sources/HttpDownloadSource.cs b/src/c#/GeneralUpdate.Core/Download/Sources/HttpDownloadSource.cs index b9a9c9fa..c5183f16 100644 --- a/src/c#/GeneralUpdate.Core/Download/Sources/HttpDownloadSource.cs +++ b/src/c#/GeneralUpdate.Core/Download/Sources/HttpDownloadSource.cs @@ -49,12 +49,12 @@ public async Task> ListAsync(CancellationToken toke var mainResp = await VersionService.Validate( _updateUrl, _clientVersion, AppType.ClientApp, _appSecretKey, _platform, _productId, - _scheme, _token); + _scheme, _token, token); var upgradeResp = await VersionService.Validate( _updateUrl, _upgradeClientVersion ?? _clientVersion, AppType.UpgradeApp, _appSecretKey, _platform, _productId, - _scheme, _token); + _scheme, _token, token); var assets = new List(); diff --git a/src/c#/GeneralUpdate.Core/FileSystem/IStorageManager.cs b/src/c#/GeneralUpdate.Core/FileSystem/IStorageManager.cs new file mode 100644 index 00000000..aa313776 --- /dev/null +++ b/src/c#/GeneralUpdate.Core/FileSystem/IStorageManager.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace GeneralUpdate.Core.FileSystem; + +/// +/// Storage management interface — backup, restore, and cleanup operations. +/// Instance-based for dependency injection with blacklist matcher support. +/// +public interface IStorageManager +{ + /// Backup source directory to destination. + Task BackupAsync(string source, string dest, IReadOnlyList skipDirectories, CancellationToken token = default); + + /// Restore from backup to installation path. + Task RestoreAsync(string backupDir, string installPath, CancellationToken token = default); + + /// Clean old backups, keeping only the N most recent versions. + Task CleanBackupAsync(string installPath, int keepVersions = 3, CancellationToken token = default); + + /// List all backups with metadata. + IReadOnlyList ListBackups(string installPath); +} + +/// +/// Default storage manager implementation wrapping the static StorageManager. +/// +public class DefaultStorageManager : IStorageManager +{ + private readonly IBlackListMatcher? _matcher; + + public DefaultStorageManager(IBlackListMatcher? matcher = null) + { + _matcher = matcher; + } + + public Task BackupAsync(string source, string dest, IReadOnlyList skipDirectories, CancellationToken token = default) + { + return StorageManager.BackupAsync(source, dest, skipDirectories); + } + + public Task RestoreAsync(string backupDir, string installPath, CancellationToken token = default) + { + return StorageManager.RestoreAsync(backupDir, installPath); + } + + public Task CleanBackupAsync(string installPath, int keepVersions = 3, CancellationToken token = default) + { + return StorageManager.CleanBackupAsync(installPath, keepVersions); + } + + public IReadOnlyList ListBackups(string installPath) + { + return StorageManager.ListBackups(installPath); + } +} diff --git a/src/c#/GeneralUpdate.Core/Network/VersionService.cs b/src/c#/GeneralUpdate.Core/Network/VersionService.cs index 0be48e82..7b219ef8 100644 --- a/src/c#/GeneralUpdate.Core/Network/VersionService.cs +++ b/src/c#/GeneralUpdate.Core/Network/VersionService.cs @@ -46,20 +46,20 @@ public VersionService(IHttpAuthProvider? auth = null, TimeSpan? timeout = null, } private VersionService() { } - // Static API (backward-compatible) + // Static API (backward-compatible, CancellationToken optional) public static Task Report(string url, int recordId, int status, int? type, - string scheme = null, string token = null) + string scheme = null, string token = null, CancellationToken ct = default) { var a = HttpAuthProviderFactory.Create(scheme, token, null); - return new VersionService(a).ReportAsync(url, recordId, status, type); + return new VersionService(a).ReportAsync(url, recordId, status, type, ct); } public static Task Validate(string url, string version, int appType, string appKey, int platform, string productId, - string scheme = null, string token = null) + string scheme = null, string token = null, CancellationToken ct = default) { var a = HttpAuthProviderFactory.Create(scheme, token, appKey); - return new VersionService(a).ValidateAsync(url, version, appType, platform, productId); + return new VersionService(a).ValidateAsync(url, version, appType, platform, productId, ct); } private async Task ReportAsync(string url, int recordId, int status, int? type, CancellationToken t = default)