aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Application/Management/ManagementService.cs
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-09-06 22:12:57 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-09-06 22:12:57 -0500
commitc7f63e0468c0ca034986d8c72627f9a02040ba43 (patch)
tree79f3a957c1d4eacdde5c025f8f44c24b966c3c4e /src/slskd/Application/Management/ManagementService.cs
parentedf86bfb54d31c1a0b02e7d416806d260dd5b7f2 (diff)
downloadslskd-c7f63e0468c0ca034986d8c72627f9a02040ba43.tar.gz
add CheckVersionAsync
Diffstat (limited to 'src/slskd/Application/Management/ManagementService.cs')
-rw-r--r--src/slskd/Application/Management/ManagementService.cs63
1 files changed, 54 insertions, 9 deletions
diff --git a/src/slskd/Application/Management/ManagementService.cs b/src/slskd/Application/Management/ManagementService.cs
index 3d7bd442..07767e20 100644
--- a/src/slskd/Application/Management/ManagementService.cs
+++ b/src/slskd/Application/Management/ManagementService.cs
@@ -20,7 +20,11 @@ using Microsoft.Extensions.Options;
namespace slskd.Management
{
using System;
+ using System.Net.Http;
+ using System.Net.Http.Json;
+ using System.Text.Json;
using System.Threading.Tasks;
+ using Serilog;
using Soulseek;
/// <summary>
@@ -32,25 +36,26 @@ namespace slskd.Management
/// Initializes a new instance of the <see cref="ManagementService"/> class.
/// </summary>
/// <param name="optionsMonitor">The options monitor used to derive application options.</param>
- /// <param name="serviceStateMonitor">The state monitor for application service state.</param>
+ /// <param name="applicationStateMonitor">The state monitor for application service state.</param>
/// <param name="soulseekClient">The Soulseek client.</param>
/// <param name="sharedFileCache">The shared file cache.</param>
+ /// <param name="httpClientFactory">The HttpClientFactory to use.</param>
public ManagementService(
IOptionsMonitor<Options> optionsMonitor,
- IStateMonitor<ApplicationState> serviceStateMonitor,
+ IStateMonitor<ApplicationState> applicationStateMonitor,
ISoulseekClient soulseekClient,
- ISharedFileCache sharedFileCache)
+ ISharedFileCache sharedFileCache,
+ IHttpClientFactory httpClientFactory)
{
OptionsMonitor = optionsMonitor;
- ServiceStateMonitor = serviceStateMonitor;
+ ApplicationStateMonitor = applicationStateMonitor;
Client = soulseekClient;
SharedFileCache = sharedFileCache;
+ HttpClientFactory = httpClientFactory;
}
- /// <summary>
- /// Gets the current state of the slskd service.
- /// </summary>
- public ApplicationState ApplicationState => ServiceStateMonitor.CurrentValue;
+ private IHttpClientFactory HttpClientFactory { get; }
+ private ILogger Log { get; } = Serilog.Log.ForContext<ManagementService>();
/// <summary>
/// Gets the current state of the connection to the Soulseek server.
@@ -72,7 +77,7 @@ namespace slskd.Management
private ISoulseekClient Client { get; }
private Options Options => OptionsMonitor.CurrentValue;
private IOptionsMonitor<Options> OptionsMonitor { get; }
- private IStateMonitor<ApplicationState> ServiceStateMonitor { get; }
+ private IStateMonitor<ApplicationState> ApplicationStateMonitor { get; }
private ISharedFileCache SharedFileCache { get; }
/// <summary>
@@ -95,5 +100,45 @@ namespace slskd.Management
/// </summary>
/// <returns>The operation context.</returns>
public Task RescanSharesAsync() => SharedFileCache.FillAsync();
+
+ /// <summary>
+ /// Gets the version of the latest application release.
+ /// </summary>
+ /// <returns>The operation context.</returns>
+ public async Task CheckVersionAsync()
+ {
+ if (Program.InformationalVersion.EndsWith("65534"))
+ {
+ Log.Information("Skipping version check for Canary build; check for updates manually.");
+ }
+
+ try
+ {
+ using var http = HttpClientFactory.CreateClient();
+ http.DefaultRequestHeaders.UserAgent.TryParseAdd(Program.AppName + Program.Version);
+
+ Log.Information("Checking {LatestReleaseUrl} for latest version", Program.RepositoryAPILatestReleaseUrl);
+
+ var response = await http.GetFromJsonAsync<JsonDocument>(Program.RepositoryAPILatestReleaseUrl);
+ var latestVersion = Version.Parse(response.RootElement.GetProperty("tag_name").GetString());
+ var currentVersion = Version.Parse(Program.InformationalVersion);
+
+ if (latestVersion > currentVersion)
+ {
+ ApplicationStateMonitor.SetValue(state => state with { LatestVersion = latestVersion.ToString(), UpdateAvailable = true });
+ Log.Information("A new version is available! {CurrentVersion} -> {LatestVersion}", currentVersion, latestVersion);
+ }
+ else
+ {
+ Log.Information("Version {Version} is up to date.", currentVersion);
+ }
+
+ }
+ catch (Exception ex)
+ {
+ Log.Warning("Failed to check version: {Message}", ex.Message);
+ throw;
+ }
+ }
}
} \ No newline at end of file