mirror of
https://github.com/netchx/netch.git
synced 2026-05-05 22:35:48 +08:00
Update Updater
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Netch.Models.GitHubRelease;
|
||||
using Netch.Utils;
|
||||
using static Netch.Updater.Updater;
|
||||
|
||||
namespace Netch.Controllers
|
||||
{
|
||||
@@ -24,9 +23,11 @@ namespace Netch.Controllers
|
||||
|
||||
public static readonly string Version = $"{AssemblyVersion}{(string.IsNullOrEmpty(Suffix) ? "" : $"-{Suffix}")}";
|
||||
|
||||
public static string? LatestVersionNumber;
|
||||
public static string? LatestVersionUrl;
|
||||
public static Release? LatestRelease;
|
||||
public static Release LatestRelease = null!;
|
||||
|
||||
public static string LatestVersionNumber => LatestRelease.tag_name;
|
||||
|
||||
public static string LatestVersionUrl => LatestRelease.html_url;
|
||||
|
||||
public static event EventHandler? NewVersionFound;
|
||||
|
||||
@@ -34,7 +35,7 @@ namespace Netch.Controllers
|
||||
|
||||
public static event EventHandler? NewVersionNotFound;
|
||||
|
||||
public static async void Check(bool isPreRelease)
|
||||
public static async Task Check(bool isPreRelease)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -43,10 +44,8 @@ namespace Netch.Controllers
|
||||
|
||||
var json = await WebUtil.DownloadStringAsync(WebUtil.CreateRequest(url));
|
||||
|
||||
var releases = JsonSerializer.Deserialize<List<Release>>(json);
|
||||
var releases = JsonSerializer.Deserialize<List<Release>>(json)!;
|
||||
LatestRelease = VersionUtil.GetLatestRelease(releases, isPreRelease);
|
||||
LatestVersionNumber = LatestRelease.tag_name;
|
||||
LatestVersionUrl = LatestRelease.html_url;
|
||||
Logging.Info($"Github 最新发布版本: {LatestRelease.tag_name}");
|
||||
if (VersionUtil.CompareVersion(LatestRelease.tag_name, Version) > 0)
|
||||
{
|
||||
@@ -70,47 +69,31 @@ namespace Netch.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task DownloadUpdate(DownloadProgressChangedEventHandler onDownloadProgressChanged)
|
||||
public static bool GetFileNameAndHashFromMarkdownForm(in string text, out string fileName, out string sha256, string? keyword = null)
|
||||
{
|
||||
using WebClient client = new();
|
||||
|
||||
var latestVersionDownloadUrl = LatestRelease!.assets[0].browser_download_url;
|
||||
var tagPage = await client.DownloadStringTaskAsync(LatestVersionUrl!);
|
||||
var match = Regex.Match(tagPage, @"<td .*>(?<sha256>.*)</td>", RegexOptions.Singleline);
|
||||
|
||||
// TODO Replace with regex get basename and sha256
|
||||
var fileName = Path.GetFileName(new Uri(latestVersionDownloadUrl).LocalPath);
|
||||
fileName = fileName.Insert(fileName.LastIndexOf('.'), LatestVersionNumber!);
|
||||
var fileFullPath = Path.Combine(Global.NetchDir, "data", fileName);
|
||||
|
||||
var sha256 = match.Groups["sha256"].Value;
|
||||
|
||||
if (File.Exists(fileFullPath))
|
||||
{
|
||||
if (Utils.Utils.SHA256CheckSum(fileFullPath) == sha256)
|
||||
{
|
||||
UpdateNetch(fileFullPath);
|
||||
return;
|
||||
}
|
||||
|
||||
File.Delete(fileFullPath);
|
||||
}
|
||||
|
||||
IEnumerable<Match> matches;
|
||||
try
|
||||
{
|
||||
client.DownloadProgressChanged += onDownloadProgressChanged;
|
||||
await client.DownloadFileTaskAsync(new Uri(latestVersionDownloadUrl), fileFullPath);
|
||||
client.DownloadProgressChanged -= onDownloadProgressChanged;
|
||||
matches = Regex.Matches(text, @"^\| (?<filename>.*) \| (?<sha256>.*) \|\r?$", RegexOptions.Multiline).Cast<Match>().Skip(2);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception(i18N.Translate("Download Update Failed", ": ") + e.Message);
|
||||
Logging.Error(e.ToString());
|
||||
throw new Exception(i18N.Translate("Find update filename and hash failed"));
|
||||
}
|
||||
|
||||
if (Utils.Utils.SHA256CheckSum(fileFullPath) != sha256)
|
||||
throw new Exception(i18N.Translate("The downloaded file has the wrong hash"));
|
||||
Match match = keyword == null ? matches.First() : matches.First(m => m.Groups["filename"].Value.Contains(keyword));
|
||||
|
||||
UpdateNetch(fileFullPath);
|
||||
if (match != null)
|
||||
{
|
||||
fileName = match.Groups["filename"].Value;
|
||||
sha256 = match.Groups["sha256"].Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
fileName = string.Empty;
|
||||
sha256 = string.Empty;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user