From 6f42bcfa4f5db94140b18c544f0228ad06e2840c Mon Sep 17 00:00:00 2001 From: bao-qian Date: Tue, 7 Mar 2017 19:23:42 +0000 Subject: [PATCH] Use own update manager, so we can pass proxy #1266 --- Wox.Core/Updater.cs | 44 ++++++++++++++++++++----- Wox.Infrastructure/Wox.cs | 2 +- Wox/SettingWindow.xaml.cs | 2 +- Wox/ViewModel/SettingWindowViewModel.cs | 4 +-- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/Wox.Core/Updater.cs b/Wox.Core/Updater.cs index 85bc5ce388..42c460e0b2 100644 --- a/Wox.Core/Updater.cs +++ b/Wox.Core/Updater.cs @@ -1,10 +1,13 @@ using System; +using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Net.Sockets; +using System.Linq; using System.Threading.Tasks; using System.Windows; using Squirrel; +using Newtonsoft.Json; using Wox.Core.Resource; using Wox.Infrastructure; using Wox.Infrastructure.Http; @@ -16,14 +19,9 @@ namespace Wox.Core { public static async Task UpdateApp() { - - var c = new WebClient { Proxy = Http.WebProxy() }; - var d = new FileDownloader(c); - try { - const string url = Constant.Github; - using (var m = await UpdateManager.GitHubUpdateManager(url, urlDownloader: d)) + using (var m = await GitHubUpdateManager(Constant.Repository)) { // UpdateApp CheckForUpdate will return value only if the app is squirrel installed var e = await m.CheckForUpdate().NonNull(); @@ -45,7 +43,6 @@ namespace Wox.Core catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException) { Log.Exception("|Updater.UpdateApp|Network error", e); - } catch (Exception e) { @@ -68,5 +65,36 @@ namespace Wox.Core return tips; } + class GithubRelease + { + [JsonProperty("prerelease")] + public bool Prerelease { get; set; } + + [JsonProperty("published_at")] + public DateTime PublishedAt { get; set; } + + [JsonProperty("html_url")] + public string HtmlUrl { get; set; } + } + + /// https://github.com/Squirrel/Squirrel.Windows/blob/master/src/Squirrel/UpdateManager.Factory.cs + private static async Task GitHubUpdateManager(string repository) + { + var uri = new Uri(repository); + var api = $"https://api.github.com/{uri.AbsolutePath}/releases"; + + var json = await Http.Get(api); + + var releases = JsonConvert.DeserializeObject>(json); + var latest = releases.Where(r => r.Prerelease).OrderByDescending(r => r.PublishedAt).First(); + var latestUrl = latest.HtmlUrl.Replace("/tag/", "/download/"); + + var client = new WebClient { Proxy = Http.WebProxy() }; + var downloader = new FileDownloader(client); + + var manager = new UpdateManager(latestUrl, urlDownloader: downloader); + + return manager; + } } -} +} \ No newline at end of file diff --git a/Wox.Infrastructure/Wox.cs b/Wox.Infrastructure/Wox.cs index 4acce08458..b5abd88f98 100644 --- a/Wox.Infrastructure/Wox.cs +++ b/Wox.Infrastructure/Wox.cs @@ -16,7 +16,7 @@ namespace Wox.Infrastructure public static readonly string DataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Wox); public static readonly string PluginsDirectory = Path.Combine(DataDirectory, Plugins); public static readonly string PreinstalledDirectory = Path.Combine(ProgramDirectory, Plugins); - public const string Github = "https://github.com/Wox-launcher/Wox"; + public const string Repository = "https://github.com/Wox-launcher/Wox"; public const string Issue = "https://github.com/Wox-launcher/Wox/issues/new"; public static readonly string Version = FileVersionInfo.GetVersionInfo(Assembly.Location).ProductVersion; diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index d5bf9bb5ae..fccfb97948 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -272,7 +272,7 @@ namespace Wox return; } - HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Infrastructure.Constant.Github); + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Infrastructure.Constant.Repository); if (string.IsNullOrEmpty(_settings.Proxy.UserName) || string.IsNullOrEmpty(_settings.Proxy.Password)) { request.Proxy = new WebProxy(_settings.Proxy.Server, _settings.Proxy.Port); diff --git a/Wox/ViewModel/SettingWindowViewModel.cs b/Wox/ViewModel/SettingWindowViewModel.cs index 53d1c595a3..bcb05cc444 100644 --- a/Wox/ViewModel/SettingWindowViewModel.cs +++ b/Wox/ViewModel/SettingWindowViewModel.cs @@ -206,7 +206,7 @@ namespace Wox.ViewModel }, new Result { - Title = $"Open Source: {Constant.Github}", + Title = $"Open Source: {Constant.Repository}", SubTitle = "Please star it!" } }; @@ -316,7 +316,7 @@ namespace Wox.ViewModel #region about - public static string Github => Constant.Github; + public static string Github => Constant.Repository; public static string ReleaseNotes => @"https://github.com/Wox-launcher/Wox/releases/latest"; public static string Version => Constant.Version; public string ActivatedTimes => string.Format(_translater.GetTranslation("about_activate_times"), Settings.ActivateTimes);