Refactor Updater exception and release UpdaterChecker Event's EventHandler

Fix didn't Remove broken update file
This commit is contained in:
ChsBuffer
2021-03-06 02:56:41 +08:00
parent 1ff9d1ec9d
commit 10ba299f4d
2 changed files with 91 additions and 59 deletions

View File

@@ -384,19 +384,25 @@ namespace Netch.Forms
{
void OnNewVersionNotFound(object o, EventArgs args)
{
UpdateChecker.NewVersionNotFound -= OnNewVersionNotFound;
NotifyTip(i18N.Translate("Already latest version"));
}
void OnNewVersionFoundFailed(object o, EventArgs args)
{
UpdateChecker.NewVersionFoundFailed -= OnNewVersionFoundFailed;
NotifyTip(i18N.Translate("New version found failed"), info: false);
}
UpdateChecker.NewVersionNotFound += OnNewVersionNotFound;
UpdateChecker.NewVersionFoundFailed += OnNewVersionFoundFailed;
CheckUpdate();
try
{
UpdateChecker.NewVersionNotFound += OnNewVersionNotFound;
UpdateChecker.NewVersionFoundFailed += OnNewVersionFoundFailed;
CheckUpdate();
}
finally
{
UpdateChecker.NewVersionNotFound -= OnNewVersionNotFound;
UpdateChecker.NewVersionFoundFailed -= OnNewVersionFoundFailed;
}
});
}
@@ -570,6 +576,47 @@ namespace Netch.Forms
Utils.Utils.Open($"https://github.com/{UpdateChecker.Owner}/{UpdateChecker.Repo}/releases");
}
private async void NewVersionLabel_Click(object sender, EventArgs e)
{
if (ModifierKeys == Keys.Control || !UpdateChecker.LatestRelease!.assets.Any())
{
Utils.Utils.Open(UpdateChecker.LatestVersionUrl!);
return;
}
if (MessageBoxX.Show(i18N.Translate("Download and install now?"), confirm: true) != DialogResult.OK)
return;
NotifyTip(i18N.Translate("Start downloading new version"));
NewVersionLabel.Enabled = false;
NewVersionLabel.Text = "...";
try
{
await Task.Run(() =>
{
Updater.Updater.DownloadAndUpdate(Path.Combine(Global.NetchDir, "data"),
Global.NetchDir,
(_, args) => BeginInvoke(new Action(() => NewVersionLabel.Text = $"{args.ProgressPercentage}%")));
});
}
catch (Exception exception)
{
if (exception is not MessageException)
{
Logging.Error($"更新失败: {exception}");
Utils.Utils.Open(Logging.LogFile);
}
NotifyTip(exception.Message, info: false);
}
finally
{
NewVersionLabel.Visible = false;
NewVersionLabel.Enabled = true;
}
}
private void AboutToolStripButton_Click(object sender, EventArgs e)
{
Hide();
@@ -1392,43 +1439,22 @@ namespace Netch.Forms
private void CheckUpdate()
{
UpdateChecker.NewVersionFound += (_, _) =>
{
NotifyTip($"{i18N.Translate(@"New version available", ": ")}{UpdateChecker.LatestVersionNumber}");
NewVersionLabel.Visible = true;
};
UpdateChecker.Check(Global.Settings.CheckBetaUpdate).Wait();
}
private async void NewVersionLabel_Click(object sender, EventArgs e)
{
if (ModifierKeys == Keys.Control || !UpdateChecker.LatestRelease!.assets.Any())
{
Utils.Utils.Open(UpdateChecker.LatestVersionUrl!);
return;
}
if (MessageBoxX.Show(i18N.Translate("Download and install now?"), confirm: true) != DialogResult.OK)
return;
NotifyTip(i18N.Translate("Start downloading new version"));
NewVersionLabel.Enabled = false;
NewVersionLabel.Text = "...";
try
{
void OnDownloadProgressChanged(object o1, DownloadProgressChangedEventArgs args)
{
BeginInvoke(new Action(() => { NewVersionLabel.Text = $"{args.ProgressPercentage}%"; }));
}
await Updater.Updater.DownloadAndUpdate(Path.Combine(Global.NetchDir, "data"), Global.NetchDir, OnDownloadProgressChanged);
UpdateChecker.NewVersionFound += OnUpdateCheckerOnNewVersionFound;
UpdateChecker.Check(Global.Settings.CheckBetaUpdate).Wait();
}
catch (Exception exception)
finally
{
Logging.Error(exception.Message);
NotifyTip(exception.Message);
UpdateChecker.NewVersionFound -= OnUpdateCheckerOnNewVersionFound;
}
void OnUpdateCheckerOnNewVersionFound(object o, EventArgs eventArgs)
{
NotifyTip($"{i18N.Translate(@"New version available", ": ")}{UpdateChecker.LatestVersionNumber}");
NewVersionLabel.Text = i18N.Translate("New version available");
NewVersionLabel.Enabled = true;
NewVersionLabel.Visible = true;
}
}

View File

@@ -5,7 +5,6 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Netch.Controllers;
using Netch.Properties;
using Netch.Utils;
@@ -121,40 +120,47 @@ namespace Netch.Updater
}
}
public static async Task DownloadAndUpdate(string downloadPath,
public static void DownloadAndUpdate(string downloadPath,
string targetPath,
DownloadProgressChangedEventHandler onDownloadProgressChanged)
DownloadProgressChangedEventHandler onDownloadProgressChanged,
string? keyword = null)
{
var keyword = (string?) null;
if (!UpdateChecker.GetFileNameAndHashFromMarkdownForm(UpdateChecker.LatestRelease.body, out var fileName, out var sha256, keyword))
throw new Exception(i18N.Translate("parse release note failed"));
throw new MessageException(i18N.Translate("parse release note failed"));
var fileFullPath = Path.Combine(downloadPath, fileName);
var updater = new Updater(fileFullPath, targetPath);
if (!(File.Exists(fileFullPath) && Utils.Utils.SHA256CheckSum(fileFullPath) == sha256))
if (File.Exists(fileFullPath))
{
using WebClient client = new();
try
if (Utils.Utils.SHA256CheckSum(fileFullPath) == sha256)
{
client.DownloadProgressChanged += onDownloadProgressChanged;
await client.DownloadFileTaskAsync(new Uri(UpdateChecker.LatestRelease.assets[0].browser_download_url), fileFullPath);
}
catch (Exception e)
{
throw new Exception(i18N.Translate("Download Update Failed", ": ") + e.Message);
}
finally
{
client.DownloadProgressChanged -= onDownloadProgressChanged;
updater.ApplyUpdate();
return;
}
if (Utils.Utils.SHA256CheckSum(fileFullPath) != sha256)
throw new Exception(i18N.Translate("The downloaded file has the wrong hash"));
File.Delete(fileFullPath);
}
DownloadUpdate(onDownloadProgressChanged, fileFullPath, sha256);
updater.ApplyUpdate();
}
private static void DownloadUpdate(DownloadProgressChangedEventHandler onDownloadProgressChanged, string fileFullPath, string sha256)
{
using WebClient client = new();
try
{
client.DownloadProgressChanged += onDownloadProgressChanged;
client.DownloadFile(new Uri(UpdateChecker.LatestRelease.assets[0].browser_download_url), fileFullPath);
}
finally
{
client.DownloadProgressChanged -= onDownloadProgressChanged;
}
if (Utils.Utils.SHA256CheckSum(fileFullPath) != sha256)
throw new MessageException(i18N.Translate("The downloaded file has the wrong hash"));
}
}
}