Skip to content

Commit 12b19cf

Browse files
committed
Merge Dev
2 parents cbfa3f3 + f714e1e commit 12b19cf

File tree

36 files changed

+577
-507
lines changed

36 files changed

+577
-507
lines changed

Flow.Launcher.Core/Updater.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Windows;
99
using JetBrains.Annotations;
1010
using Squirrel;
11-
using Newtonsoft.Json;
1211
using Flow.Launcher.Core.Resource;
1312
using Flow.Launcher.Plugin.SharedCommands;
1413
using Flow.Launcher.Infrastructure;
@@ -17,6 +16,7 @@
1716
using System.IO;
1817
using Flow.Launcher.Infrastructure.UserSettings;
1918
using Flow.Launcher.Plugin;
19+
using System.Text.Json.Serialization;
2020

2121
namespace Flow.Launcher.Core
2222
{
@@ -38,11 +38,11 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
3838
if (!silentUpdate)
3939
api.ShowMsg("Please wait...", "Checking for new update");
4040

41-
using var updateManager = await GitHubUpdateManager(GitHubRepository);
41+
using var updateManager = await GitHubUpdateManager(GitHubRepository).ConfigureAwait(false);
4242

4343

4444
// UpdateApp CheckForUpdate will return value only if the app is squirrel installed
45-
newUpdateInfo = await updateManager.CheckForUpdate().NonNull();
45+
newUpdateInfo = await updateManager.CheckForUpdate().NonNull().ConfigureAwait(false);
4646

4747
var newReleaseVersion = Version.Parse(newUpdateInfo.FutureReleaseEntry.Version.ToString());
4848
var currentVersion = Version.Parse(Constant.Version);
@@ -53,16 +53,15 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
5353
{
5454
if (!silentUpdate)
5555
MessageBox.Show("You already have the latest Flow Launcher version");
56-
updateManager.Dispose();
5756
return;
5857
}
5958

6059
if (!silentUpdate)
6160
api.ShowMsg("Update found", "Updating...");
6261

63-
await updateManager.DownloadReleases(newUpdateInfo.ReleasesToApply);
62+
await updateManager.DownloadReleases(newUpdateInfo.ReleasesToApply).ConfigureAwait(false);
6463

65-
await updateManager.ApplyReleases(newUpdateInfo);
64+
await updateManager.ApplyReleases(newUpdateInfo).ConfigureAwait(false);
6665

6766
if (DataLocation.PortableDataLocationInUse())
6867
{
@@ -74,7 +73,7 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
7473
}
7574
else
7675
{
77-
await updateManager.CreateUninstallerRegistryEntry();
76+
await updateManager.CreateUninstallerRegistryEntry().ConfigureAwait(false);
7877
}
7978

8079
var newVersionTips = NewVersinoTips(newReleaseVersion.ToString());
@@ -97,13 +96,13 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
9796
[UsedImplicitly]
9897
private class GithubRelease
9998
{
100-
[JsonProperty("prerelease")]
99+
[JsonPropertyName("prerelease")]
101100
public bool Prerelease { get; [UsedImplicitly] set; }
102101

103-
[JsonProperty("published_at")]
102+
[JsonPropertyName("published_at")]
104103
public DateTime PublishedAt { get; [UsedImplicitly] set; }
105104

106-
[JsonProperty("html_url")]
105+
[JsonPropertyName("html_url")]
107106
public string HtmlUrl { get; [UsedImplicitly] set; }
108107
}
109108

@@ -113,13 +112,13 @@ private async Task<UpdateManager> GitHubUpdateManager(string repository)
113112
var uri = new Uri(repository);
114113
var api = $"https://api.github.com/repos{uri.AbsolutePath}/releases";
115114

116-
var json = await Http.Get(api);
115+
var jsonStream = await Http.GetStreamAsync(api).ConfigureAwait(false);
117116

118-
var releases = JsonConvert.DeserializeObject<List<GithubRelease>>(json);
117+
var releases = await System.Text.Json.JsonSerializer.DeserializeAsync<List<GithubRelease>>(jsonStream).ConfigureAwait(false);
119118
var latest = releases.Where(r => !r.Prerelease).OrderByDescending(r => r.PublishedAt).First();
120119
var latestUrl = latest.HtmlUrl.Replace("/tag/", "/download/");
121120

122-
var client = new WebClient { Proxy = Http.WebProxy() };
121+
var client = new WebClient { Proxy = Http.WebProxy };
123122
var downloader = new FileDownloader(client);
124123

125124
var manager = new UpdateManager(latestUrl, urlDownloader: downloader);

Flow.Launcher.Infrastructure/Constant.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ public static class Constant
3535
public const string DefaultTheme = "Darker";
3636

3737
public const string Themes = "Themes";
38+
39+
public const string Website = "https://flow-launcher.github.io";
3840
}
3941
}

Flow.Launcher.Infrastructure/Http/Http.cs

Lines changed: 90 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,72 +6,127 @@
66
using JetBrains.Annotations;
77
using Flow.Launcher.Infrastructure.Logger;
88
using Flow.Launcher.Infrastructure.UserSettings;
9+
using System;
10+
using System.ComponentModel;
911

1012
namespace Flow.Launcher.Infrastructure.Http
1113
{
1214
public static class Http
1315
{
1416
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
1517

18+
private static HttpClient client;
19+
20+
private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler()
21+
{
22+
UseProxy = true,
23+
Proxy = WebProxy
24+
};
25+
1626
static Http()
1727
{
1828
// need to be added so it would work on a win10 machine
1929
ServicePointManager.Expect100Continue = true;
2030
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls
2131
| SecurityProtocolType.Tls11
2232
| SecurityProtocolType.Tls12;
33+
34+
client = new HttpClient(socketsHttpHandler, false);
35+
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
2336
}
2437

25-
public static HttpProxy Proxy { private get; set; }
26-
public static IWebProxy WebProxy()
38+
private static HttpProxy proxy;
39+
40+
public static HttpProxy Proxy
2741
{
28-
if (Proxy != null && Proxy.Enabled && !string.IsNullOrEmpty(Proxy.Server))
42+
private get { return proxy; }
43+
set
2944
{
30-
if (string.IsNullOrEmpty(Proxy.UserName) || string.IsNullOrEmpty(Proxy.Password))
31-
{
32-
var webProxy = new WebProxy(Proxy.Server, Proxy.Port);
33-
return webProxy;
34-
}
35-
else
45+
proxy = value;
46+
proxy.PropertyChanged += UpdateProxy;
47+
}
48+
}
49+
50+
public static WebProxy WebProxy { get; } = new WebProxy();
51+
52+
/// <summary>
53+
/// Update the Address of the Proxy to modify the client Proxy
54+
/// </summary>
55+
public static void UpdateProxy(ProxyProperty property)
56+
{
57+
(WebProxy.Address, WebProxy.Credentials) = property switch
58+
{
59+
ProxyProperty.Enabled => Proxy.Enabled switch
3660
{
37-
var webProxy = new WebProxy(Proxy.Server, Proxy.Port)
61+
true => Proxy.UserName switch
3862
{
39-
Credentials = new NetworkCredential(Proxy.UserName, Proxy.Password)
40-
};
41-
return webProxy;
42-
}
63+
var userName when !string.IsNullOrEmpty(userName) =>
64+
(new Uri($"http://{Proxy.Server}:{Proxy.Port}"), null),
65+
_ => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"),
66+
new NetworkCredential(Proxy.UserName, Proxy.Password))
67+
},
68+
false => (null, null)
69+
},
70+
ProxyProperty.Server => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials),
71+
ProxyProperty.Port => (new Uri($"http://{Proxy.Server}:{Proxy.Port}"), WebProxy.Credentials),
72+
ProxyProperty.UserName => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)),
73+
ProxyProperty.Password => (WebProxy.Address, new NetworkCredential(Proxy.UserName, Proxy.Password)),
74+
_ => throw new ArgumentOutOfRangeException()
75+
};
76+
}
77+
78+
public static async Task Download([NotNull] string url, [NotNull] string filePath)
79+
{
80+
using var response = await client.GetAsync(url);
81+
if (response.StatusCode == HttpStatusCode.OK)
82+
{
83+
await using var fileStream = new FileStream(filePath, FileMode.CreateNew);
84+
await response.Content.CopyToAsync(fileStream);
4385
}
4486
else
4587
{
46-
return WebRequest.GetSystemWebProxy();
88+
throw new HttpRequestException($"Error code <{response.StatusCode}> returned from <{url}>");
4789
}
4890
}
4991

50-
public static void Download([NotNull] string url, [NotNull] string filePath)
92+
/// <summary>
93+
/// Asynchrously get the result as string from url.
94+
/// When supposing the result is long and large, try using GetStreamAsync to avoid reading as string
95+
/// </summary>
96+
/// <param name="url"></param>
97+
/// <returns></returns>
98+
public static Task<string> GetAsync([NotNull] string url)
5199
{
52-
var client = new WebClient { Proxy = WebProxy() };
53-
client.Headers.Add("user-agent", UserAgent);
54-
client.DownloadFile(url, filePath);
100+
Log.Debug($"|Http.Get|Url <{url}>");
101+
return GetAsync(new Uri(url.Replace("#", "%23")));
55102
}
56103

57-
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
104+
public static async Task<string> GetAsync([NotNull] Uri url)
58105
{
59106
Log.Debug($"|Http.Get|Url <{url}>");
60-
var request = WebRequest.CreateHttp(url);
61-
request.Method = "GET";
62-
request.Timeout = 1000;
63-
request.Proxy = WebProxy();
64-
request.UserAgent = UserAgent;
65-
var response = await request.GetResponseAsync() as HttpWebResponse;
66-
response = response.NonNull();
67-
var stream = response.GetResponseStream().NonNull();
107+
using var response = await client.GetAsync(url);
108+
var content = await response.Content.ReadAsStringAsync();
109+
if (response.StatusCode == HttpStatusCode.OK)
110+
{
111+
return content;
112+
}
113+
else
114+
{
115+
throw new HttpRequestException(
116+
$"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
117+
}
118+
}
68119

69-
using var reader = new StreamReader(stream, Encoding.GetEncoding(encoding));
70-
var content = await reader.ReadToEndAsync();
71-
if (response.StatusCode != HttpStatusCode.OK)
72-
throw new HttpRequestException($"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
73-
74-
return content;
120+
/// <summary>
121+
/// Asynchrously get the result as stream from url.
122+
/// </summary>
123+
/// <param name="url"></param>
124+
/// <returns></returns>
125+
public static async Task<Stream> GetStreamAsync([NotNull] string url)
126+
{
127+
Log.Debug($"|Http.Get|Url <{url}>");
128+
var response = await client.GetAsync(url);
129+
return await response.Content.ReadAsStreamAsync();
75130
}
76131
}
77-
}
132+
}
Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,80 @@
1-
namespace Flow.Launcher.Infrastructure.UserSettings
1+
using System.ComponentModel;
2+
3+
namespace Flow.Launcher.Infrastructure.UserSettings
24
{
5+
public enum ProxyProperty
6+
{
7+
Enabled,
8+
Server,
9+
Port,
10+
UserName,
11+
Password
12+
}
13+
314
public class HttpProxy
415
{
5-
public bool Enabled { get; set; } = false;
6-
public string Server { get; set; }
7-
public int Port { get; set; }
8-
public string UserName { get; set; }
9-
public string Password { get; set; }
16+
private bool _enabled = false;
17+
private string _server;
18+
private int _port;
19+
private string _userName;
20+
private string _password;
21+
22+
public bool Enabled
23+
{
24+
get => _enabled;
25+
set
26+
{
27+
_enabled = value;
28+
OnPropertyChanged(ProxyProperty.Enabled);
29+
}
30+
}
31+
32+
public string Server
33+
{
34+
get => _server;
35+
set
36+
{
37+
_server = value;
38+
OnPropertyChanged(ProxyProperty.Server);
39+
}
40+
}
41+
42+
public int Port
43+
{
44+
get => _port;
45+
set
46+
{
47+
_port = value;
48+
OnPropertyChanged(ProxyProperty.Port);
49+
}
50+
}
51+
52+
public string UserName
53+
{
54+
get => _userName;
55+
set
56+
{
57+
_userName = value;
58+
OnPropertyChanged(ProxyProperty.UserName);
59+
}
60+
}
61+
62+
public string Password
63+
{
64+
get => _password;
65+
set
66+
{
67+
_password = value;
68+
OnPropertyChanged(ProxyProperty.Password);
69+
}
70+
}
71+
72+
public delegate void ProxyPropertyChangedHandler(ProxyProperty property);
73+
public event ProxyPropertyChangedHandler PropertyChanged;
74+
75+
private void OnPropertyChanged(ProxyProperty property)
76+
{
77+
PropertyChanged?.Invoke(property);
78+
}
1079
}
1180
}

Flow.Launcher.Plugin/Flow.Launcher.Plugin.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
</PropertyGroup>
1515

1616
<PropertyGroup>
17-
<Version>1.3.0</Version>
18-
<PackageVersion>1.3.0</PackageVersion>
19-
<AssemblyVersion>1.3.0</AssemblyVersion>
20-
<FileVersion>1.3.0</FileVersion>
17+
<Version>1.3.1</Version>
18+
<PackageVersion>1.3.1</PackageVersion>
19+
<AssemblyVersion>1.3.1</AssemblyVersion>
20+
<FileVersion>1.3.1</FileVersion>
2121
<PackageId>Flow.Launcher.Plugin</PackageId>
2222
<Authors>Flow-Launcher</Authors>
2323
<PackageLicenseExpression>MIT</PackageLicenseExpression>

0 commit comments

Comments
 (0)