Skip to content

Commit

Permalink
feature: ThrowExceptionUtility is used
Browse files Browse the repository at this point in the history
  • Loading branch information
JusterZhu committed Jan 11, 2024
1 parent df60351 commit 8b0ffb1
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/c#/GeneralUpdate.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
app.MapGet("/versions/{clientType}/{clientVersion}/{clientAppKey}", (int clientType, string clientVersion, string clientAppKey, IUpdateService updateService) =>
{
var versions = new List<VersionDTO>();
var hash = "9bf414990a67e74f11752d03f49b15d8";//生成好的更新包文件的MD5码,因为返回给客户端的时候需要同这个来验证是否可用
var hash = "415eed05eb310f480d1e4d15516fa00e484ddb9f416908b217f17b782ded2030";//生成好的更新包文件的MD5码,因为返回给客户端的时候需要同这个来验证是否可用
var pubTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
string version = null;
if (clientType == AppType.ClientApp)
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/c#/GeneralUpdate.Client/MySample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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";
//更新组件更新包下载地址
Expand Down
13 changes: 12 additions & 1 deletion src/c#/GeneralUpdate.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
namespace GeneralUpdate.Client
using GeneralUpdate.Core.HashAlgorithms;

namespace GeneralUpdate.Client
{
internal class Program
{
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomArgs\UnZipExceptionArgs.cs" Link="Exceptions\CustomArgs\UnZipExceptionArgs.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomArgs\UpdateExceptionArgs.cs" Link="Exceptions\CustomArgs\UpdateExceptionArgs.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomException\GeneralUpdateException.cs" Link="Exceptions\CustomException\GeneralUpdateException.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\ThrowExceptionUtility.cs" Link="Exceptions\ThrowExceptionUtility.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\HashAlgorithmBase.cs" Link="HashAlgorithms\HashAlgorithmBase.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\Md5HashAlgorithm.cs" Link="HashAlgorithms\Md5HashAlgorithm.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\Sha1HashAlgorithm.cs" Link="HashAlgorithms\Sha1HashAlgorithm.cs" />
Expand Down
30 changes: 6 additions & 24 deletions src/c#/GeneralUpdate.Core/Bootstrap/AbstractBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,35 +166,17 @@ public virtual T GetOption<T>(UpdateOption<T> option)

#region Callback event.

public TBootstrap AddListenerMultiAllDownloadCompleted(Action<object, MultiAllDownloadCompletedEventArgs> callbackAction)
{
return AddListener(callbackAction);
}
public TBootstrap AddListenerMultiAllDownloadCompleted(Action<object, MultiAllDownloadCompletedEventArgs> callbackAction)=> AddListener(callbackAction);

public TBootstrap AddListenerMultiDownloadProgress(Action<object, MultiDownloadProgressChangedEventArgs> callbackAction)
{
return AddListener(callbackAction);
}
public TBootstrap AddListenerMultiDownloadProgress(Action<object, MultiDownloadProgressChangedEventArgs> callbackAction)=> AddListener(callbackAction);

public TBootstrap AddListenerMultiDownloadCompleted(Action<object, MultiDownloadCompletedEventArgs> callbackAction)
{
return AddListener(callbackAction);
}
public TBootstrap AddListenerMultiDownloadCompleted(Action<object, MultiDownloadCompletedEventArgs> callbackAction)=> AddListener(callbackAction);

public TBootstrap AddListenerMultiDownloadError(Action<object, MultiDownloadErrorEventArgs> callbackAction)
{
return AddListener(callbackAction);
}
public TBootstrap AddListenerMultiDownloadError(Action<object, MultiDownloadErrorEventArgs> callbackAction)=> AddListener(callbackAction);

public TBootstrap AddListenerMultiDownloadStatistics(Action<object, MultiDownloadStatisticsEventArgs> callbackAction)
{
return AddListener(callbackAction);
}
public TBootstrap AddListenerMultiDownloadStatistics(Action<object, MultiDownloadStatisticsEventArgs> callbackAction)=> AddListener(callbackAction);

public TBootstrap AddListenerException(Action<object, ExceptionEventArgs> callbackAction)
{
return AddListener(callbackAction);
}
public TBootstrap AddListenerException(Action<object, ExceptionEventArgs> callbackAction)=> AddListener(callbackAction);

protected TBootstrap AddListener<TArgs>(Action<object, TArgs> callbackAction) where TArgs : EventArgs
{
Expand Down
9 changes: 4 additions & 5 deletions src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GeneralUpdate.Core.Exceptions.CustomArgs;
using System;
using System.IO;

namespace GeneralUpdate.Core.Exceptions
{
Expand All @@ -10,11 +11,9 @@ public static void ThrowGeneralUpdateException(ExceptionArgs args)

#region Common

public static void ThrowIfNull(object obj)
{
if (obj == null)
Throw<ArgumentException>("Parameter cannot be null");
}
public static void ThrowFileNotFound(string file) => Throw<FileNotFoundException>($"File cannot be accessed {file}!");

public static void ThrowIfNull()=> Throw<ArgumentException>("Parameter cannot be null");

/// <summary>
/// Checks if an object is empty and throws an exception if it is
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using GeneralUpdate.Core.Exceptions;
using System.IO;
using System.Security.Cryptography;
using System.Text;

Expand All @@ -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))
Expand Down
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<ItemGroup>
<None Remove="CustomAwaiter\.gitkeep" />
<None Remove="Domain\PO\.gitkeep" />
<None Remove="Exceptions\.gitkeep" />
<None Remove="Exceptions\CustomArgs\.gitkeep" />
<None Remove="Exceptions\CustomException\.gitkeep" />
<None Remove="GeneralUpdate.ico" />
<None Remove="WillMessage\.gitkeep" />
</ItemGroup>
Expand All @@ -25,6 +28,13 @@
<Compile Include="..\GeneralUpdate.Core\CustomAwaiter\IAwaiter.cs" Link="CustomAwaiter\IAwaiter.cs" />
<Compile Include="..\GeneralUpdate.Core\CustomAwaiter\ICriticalAwaiter.cs" Link="CustomAwaiter\ICriticalAwaiter.cs" />
<Compile Include="..\GeneralUpdate.Core\Domain\PO\WillMessagePO.cs" Link="Domain\PO\WillMessagePO.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomArgs\ExceptionArgs.cs" Link="Exceptions\CustomArgs\ExceptionArgs.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomArgs\HttpExceptionArgs.cs" Link="Exceptions\CustomArgs\HttpExceptionArgs.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomArgs\PatchDirtyExceptionArgs.cs" Link="Exceptions\CustomArgs\PatchDirtyExceptionArgs.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomArgs\UnZipExceptionArgs.cs" Link="Exceptions\CustomArgs\UnZipExceptionArgs.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomArgs\UpdateExceptionArgs.cs" Link="Exceptions\CustomArgs\UpdateExceptionArgs.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\CustomException\GeneralUpdateException.cs" Link="Exceptions\CustomException\GeneralUpdateException.cs" />
<Compile Include="..\GeneralUpdate.Core\Exceptions\ThrowExceptionUtility.cs" Link="Exceptions\ThrowExceptionUtility.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\HashAlgorithmBase.cs" Link="HashAlgorithms\HashAlgorithmBase.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\Md5HashAlgorithm.cs" Link="HashAlgorithms\Md5HashAlgorithm.cs" />
<Compile Include="..\GeneralUpdate.Core\HashAlgorithms\Sha1HashAlgorithm.cs" Link="HashAlgorithms\Sha1HashAlgorithm.cs" />
Expand Down

0 comments on commit 8b0ffb1

Please sign in to comment.