From 8b0ffb1968bba1bcb932fa2b07b923d8a0d1e616 Mon Sep 17 00:00:00 2001 From: Juster Zhu Date: Thu, 11 Jan 2024 21:13:17 +0800 Subject: [PATCH] feature: ThrowExceptionUtility is used --- src/c#/GeneralUpdate.Api/Program.cs | 8 ++--- src/c#/GeneralUpdate.Client/MySample.cs | 4 +-- src/c#/GeneralUpdate.Client/Program.cs | 13 +++++++- .../GeneralUpdate.ClientCore.csproj | 1 + .../Bootstrap/AbstractBootstrap.cs | 30 ++++--------------- .../Exceptions/ThrowExceptionUtility.cs | 9 +++--- .../HashAlgorithms/HashAlgorithmBase.cs | 6 +++- .../Exceptions/.gitkeep | 0 .../Exceptions/CustomArgs/.gitkeep | 0 .../Exceptions/CustomException/.gitkeep | 0 .../GeneralUpdate.Differential.csproj | 10 +++++++ 11 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 src/c#/GeneralUpdate.Differential/Exceptions/.gitkeep create mode 100644 src/c#/GeneralUpdate.Differential/Exceptions/CustomArgs/.gitkeep create mode 100644 src/c#/GeneralUpdate.Differential/Exceptions/CustomException/.gitkeep diff --git a/src/c#/GeneralUpdate.Api/Program.cs b/src/c#/GeneralUpdate.Api/Program.cs index 35a851f2..6e8fa59b 100644 --- a/src/c#/GeneralUpdate.Api/Program.cs +++ b/src/c#/GeneralUpdate.Api/Program.cs @@ -36,7 +36,7 @@ app.MapGet("/versions/{clientType}/{clientVersion}/{clientAppKey}", (int clientType, string clientVersion, string clientAppKey, IUpdateService updateService) => { var versions = new List(); - var hash = "9bf414990a67e74f11752d03f49b15d8";//生成好的更新包文件的MD5码,因为返回给客户端的时候需要同这个来验证是否可用 + var hash = "415eed05eb310f480d1e4d15516fa00e484ddb9f416908b217f17b782ded2030";//生成好的更新包文件的MD5码,因为返回给客户端的时候需要同这个来验证是否可用 var pubTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); string version = null; if (clientType == AppType.ClientApp) @@ -48,10 +48,10 @@ else if (clientType == AppType.UpgradeApp) { //upgrad - //version = "0.0.0.0"; - version = "9.9.9.9"; //这里设置为9是让程序认为需要更新 + version = "0.0.0.0"; + //version = "9.9.9.9"; //这里设置为9是让程序认为需要更新 } - var url = $"http://192.168.50.203/update.zip";//更新包的下载地址 + var url = $"http://127.0.0.1/WpfClient_1_24.1.5.1218.zip";//更新包的下载地址 var name = "update"; versions.Add(new VersionDTO(hash, pubTime, version, url, name)); return updateService.Update(clientType, clientVersion, version, clientAppKey, GetAppSecretKey(), false, versions); diff --git a/src/c#/GeneralUpdate.Client/MySample.cs b/src/c#/GeneralUpdate.Client/MySample.cs index 64fc8ab7..0488921f 100644 --- a/src/c#/GeneralUpdate.Client/MySample.cs +++ b/src/c#/GeneralUpdate.Client/MySample.cs @@ -19,7 +19,7 @@ internal class MySample { #region 鎺ㄩ佸姛鑳 - private const string baseUrl = @"http://localhost:5051"; + private const string baseUrl = @"http://127.0.0.1:5001"; private const string hubName = "versionhub"; internal MySample() @@ -119,7 +119,7 @@ private Configinfo GetWindowsConfigInfo() //瀹㈡埛绔綋鍓嶇増鏈彿 config.ClientVersion = "1.1.1.1"; //瀹㈡埛绔被鍨嬶細1.涓荤▼搴忓鎴风 2.鏇存柊缁勪欢 - config.AppType = AppType.ClientApp; + config.AppType = AppType.UpgradeApp; //鎸囧畾搴旂敤瀵嗛挜锛岀敤浜庡尯鍒嗗鎴风搴旂敤 config.AppSecretKey = "B8A7FADD-386C-46B0-B283-C9F963420C7C"; //鏇存柊缁勪欢鏇存柊鍖呬笅杞藉湴鍧 diff --git a/src/c#/GeneralUpdate.Client/Program.cs b/src/c#/GeneralUpdate.Client/Program.cs index d2b50800..99480e10 100644 --- a/src/c#/GeneralUpdate.Client/Program.cs +++ b/src/c#/GeneralUpdate.Client/Program.cs @@ -1,4 +1,6 @@ -锘縩amespace GeneralUpdate.Client +锘縰sing GeneralUpdate.Core.HashAlgorithms; + +namespace GeneralUpdate.Client { internal class Program { @@ -6,6 +8,15 @@ static void Main(string[] args) { Task.Run(async() => { + //415eed05eb310f480d1e4d15516fa00e484ddb9f416908b217f17b782ded2030 + //var zip1 = @"D:\github_project\WpfClient\WebApi\UpdateFiles\WpfClient_1_24.1.5.1218.zip"; + //94bd3d806d39cd1b8813298ec0637c7f377658e766845a06cc50917306cb4ad9 + //var zip2 = @"D:\github_project\WpfClient\WebApi\UpdateFiles\WpfClient_1_24.1.5.1224.zip"; + + //var hashAlgorithm = new Sha256HashAlgorithm(); + //var hashSha256 = hashAlgorithm.ComputeHash(zip1); + //var hashSha2561 = hashAlgorithm.ComputeHash(zip2); + MySample sample = new MySample(); //await sample.TestDifferentialClean(); //await sample.TestDifferentialDirty(); diff --git a/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj b/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj index 2dc1fd22..523523f8 100644 --- a/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj +++ b/src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj @@ -127,6 +127,7 @@ + diff --git a/src/c#/GeneralUpdate.Core/Bootstrap/AbstractBootstrap.cs b/src/c#/GeneralUpdate.Core/Bootstrap/AbstractBootstrap.cs index 5a06c2b3..2062880e 100644 --- a/src/c#/GeneralUpdate.Core/Bootstrap/AbstractBootstrap.cs +++ b/src/c#/GeneralUpdate.Core/Bootstrap/AbstractBootstrap.cs @@ -166,35 +166,17 @@ public virtual T GetOption(UpdateOption option) #region Callback event. - public TBootstrap AddListenerMultiAllDownloadCompleted(Action callbackAction) - { - return AddListener(callbackAction); - } + public TBootstrap AddListenerMultiAllDownloadCompleted(Action callbackAction)=> AddListener(callbackAction); - public TBootstrap AddListenerMultiDownloadProgress(Action callbackAction) - { - return AddListener(callbackAction); - } + public TBootstrap AddListenerMultiDownloadProgress(Action callbackAction)=> AddListener(callbackAction); - public TBootstrap AddListenerMultiDownloadCompleted(Action callbackAction) - { - return AddListener(callbackAction); - } + public TBootstrap AddListenerMultiDownloadCompleted(Action callbackAction)=> AddListener(callbackAction); - public TBootstrap AddListenerMultiDownloadError(Action callbackAction) - { - return AddListener(callbackAction); - } + public TBootstrap AddListenerMultiDownloadError(Action callbackAction)=> AddListener(callbackAction); - public TBootstrap AddListenerMultiDownloadStatistics(Action callbackAction) - { - return AddListener(callbackAction); - } + public TBootstrap AddListenerMultiDownloadStatistics(Action callbackAction)=> AddListener(callbackAction); - public TBootstrap AddListenerException(Action callbackAction) - { - return AddListener(callbackAction); - } + public TBootstrap AddListenerException(Action callbackAction)=> AddListener(callbackAction); protected TBootstrap AddListener(Action callbackAction) where TArgs : EventArgs { diff --git a/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs b/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs index 95c7cdbb..088cd354 100644 --- a/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs +++ b/src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs @@ -1,5 +1,6 @@ 锘縰sing GeneralUpdate.Core.Exceptions.CustomArgs; using System; +using System.IO; namespace GeneralUpdate.Core.Exceptions { @@ -10,11 +11,9 @@ public static void ThrowGeneralUpdateException(ExceptionArgs args) #region Common - public static void ThrowIfNull(object obj) - { - if (obj == null) - Throw("Parameter cannot be null"); - } + public static void ThrowFileNotFound(string file) => Throw($"File cannot be accessed {file}!"); + + public static void ThrowIfNull()=> Throw("Parameter cannot be null"); /// /// Checks if an object is empty and throws an exception if it is diff --git a/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs b/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs index bd75ad69..09ed6b71 100644 --- a/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs +++ b/src/c#/GeneralUpdate.Core/HashAlgorithms/HashAlgorithmBase.cs @@ -1,4 +1,5 @@ -锘縰sing System.IO; +锘縰sing GeneralUpdate.Core.Exceptions; +using System.IO; using System.Security.Cryptography; using System.Text; @@ -8,6 +9,9 @@ public abstract class HashAlgorithmBase { public string ComputeHash(string fileName) { + if (!File.Exists(fileName)) + ThrowExceptionUtility.ThrowFileNotFound(fileName); + using (var hashAlgorithm = GetHashAlgorithm()) { using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) diff --git a/src/c#/GeneralUpdate.Differential/Exceptions/.gitkeep b/src/c#/GeneralUpdate.Differential/Exceptions/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/c#/GeneralUpdate.Differential/Exceptions/CustomArgs/.gitkeep b/src/c#/GeneralUpdate.Differential/Exceptions/CustomArgs/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/c#/GeneralUpdate.Differential/Exceptions/CustomException/.gitkeep b/src/c#/GeneralUpdate.Differential/Exceptions/CustomException/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj b/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj index f9a76f98..b3969018 100644 --- a/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj +++ b/src/c#/GeneralUpdate.Differential/GeneralUpdate.Differential.csproj @@ -16,6 +16,9 @@ + + + @@ -25,6 +28,13 @@ + + + + + + +