EverythingPowerToys/Update/UpdateChecker.cs

95 lines
4.4 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using Community.PowerToys.Run.Plugin.Everything.Properties;
using NLog;
using Wox.Plugin.Logger;
2022-12-11 02:20:09 +08:00
namespace Community.PowerToys.Run.Plugin.Everything.Update
2023-01-07 02:20:22 +08:00
{
internal sealed class UpdateChecker
2022-12-11 02:20:09 +08:00
{
2024-04-18 17:04:25 +08:00
private readonly CompositeFormat updateAvailable = CompositeFormat.Parse(Resources.UpdateAvailable);
internal async Task Async(Version v, Settings s, UpdateSettings us, bool isArm)
2022-12-11 02:20:09 +08:00
{
string apiUrl = "https://api.github.com/repos/lin-ycv/EverythingPowerToys/releases/latest";
if (s.LoggingLevel <= LogLevel.Info) Log.Info("EPT: Checking Update...", GetType());
2022-12-11 02:20:09 +08:00
try
{
using HttpClient httpClient = new();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0");
HttpResponseMessage response = await httpClient.GetAsync(apiUrl);
if (s.LoggingLevel <= LogLevel.Debug) Log.Info($"EPT: Response: {response.StatusCode}", GetType());
if (response.IsSuccessStatusCode)
2022-12-11 02:20:09 +08:00
{
using JsonDocument jsonDocument = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
JsonElement root = jsonDocument.RootElement;
Version latest = Version.TryParse(root.GetProperty("tag_name").GetString().AsSpan(1), out var vNumber)
? vNumber
: Version.Parse(root.GetProperty("tag_name").GetString());
if (s.LoggingLevel <= LogLevel.Debug) Log.Info($"EPT:\n\tLastest: {latest}\n\tSkip: {us.Skip}", GetType());
if (latest > v && latest != us.Skip)
2022-12-11 02:20:09 +08:00
{
MessageBoxResult mbox = MessageBox.Show(string.Format(CultureInfo.InvariantCulture, updateAvailable, v, latest), "EPT: Updater", MessageBoxButton.YesNoCancel);
if (mbox == MessageBoxResult.Yes && root.TryGetProperty("assets", out JsonElement assets))
2022-12-11 02:20:09 +08:00
{
string[] nameUrl = [string.Empty, string.Empty];
foreach (JsonElement asset in assets.EnumerateArray())
{
if (asset.TryGetProperty("browser_download_url", out JsonElement downUrl) && downUrl.ToString().EndsWith(isArm ? "ARM64.exe" : "x64.exe", StringComparison.OrdinalIgnoreCase))
{
nameUrl[0] = asset.GetProperty("name").ToString();
nameUrl[1] = downUrl.ToString();
}
}
if (nameUrl[0].Length > 0)
{
byte[] fileContent = await httpClient.GetByteArrayAsync(nameUrl[1]);
string fileName = Path.Combine(Path.GetTempPath(), nameUrl[0]);
File.WriteAllBytes(fileName, fileContent);
Process.Start(fileName);
2025-02-01 15:16:53 +08:00
foreach (Process pt in Process.GetProcessesByName("PowerToys"))
pt.Kill();
}
else
{
ProcessStartInfo p = new("https://github.com/lin-ycv/EverythingPowerToys/releases/latest")
{
UseShellExecute = true,
Verb = "Open",
};
Process.Start(p);
}
}
else if (mbox == MessageBoxResult.No)
{
us.Skip = latest;
}
2023-11-01 08:52:14 +08:00
}
2022-12-11 02:20:09 +08:00
}
}
2024-05-25 21:11:41 +08:00
catch (Exception e)
{
2025-02-01 15:16:53 +08:00
if (s.LoggingLevel <= LogLevel.Info)
Log.Exception($"EPT: Unable to check for update", e, GetType());
2024-05-25 21:11:41 +08:00
}
2025-02-01 15:16:53 +08:00
if (s.LoggingLevel <= LogLevel.Info)
Log.Info("EPT: Checking Update...Done", GetType());
2022-12-11 02:20:09 +08:00
}
}
}