From c7f63e0468c0ca034986d8c72627f9a02040ba43 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 6 Sep 2021 22:12:57 -0500 Subject: add CheckVersionAsync --- .../API/Controllers/ApplicationController.cs | 29 ++++++++++ .../Application/Management/IManagementService.cs | 6 +++ .../Application/Management/ManagementService.cs | 63 ++++++++++++++++++---- 3 files changed, 89 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs index dd115786..850dfda9 100644 --- a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs +++ b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs @@ -19,6 +19,7 @@ namespace slskd.Management.API { using System; using System.Diagnostics; + using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; @@ -78,5 +79,33 @@ namespace slskd.Management.API return NoContent(); } + + /// + /// Gets the current application version. + /// + /// + [HttpGet("version")] + [Authorize] + public IActionResult GetVersion() + { + return Ok(Program.InformationalVersion); + } + + /// + /// Checks for updates. + /// + /// + [HttpGet("version/latest")] + [Authorize] + public async Task CheckVersion([FromQuery]bool forceCheck = false) + { + if (forceCheck) + { + await Management.CheckVersionAsync(); + } + + var state = Management.ApplicationState; + return Ok(new CheckVersionResponse() { UpdateAvailable = state.UpdateAvailable, LatestVersion = state.LatestVersion }); + } } } diff --git a/src/slskd/Application/Management/IManagementService.cs b/src/slskd/Application/Management/IManagementService.cs index 9a06d961..ba5ba69a 100644 --- a/src/slskd/Application/Management/IManagementService.cs +++ b/src/slskd/Application/Management/IManagementService.cs @@ -58,5 +58,11 @@ namespace slskd.Management /// /// The operation context. Task RescanSharesAsync(); + + /// + /// Gets the version of the latest application release. + /// + /// The operation context. + Task CheckVersionAsync(); } } \ No newline at end of file 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; /// @@ -32,25 +36,26 @@ namespace slskd.Management /// Initializes a new instance of the class. /// /// The options monitor used to derive application options. - /// The state monitor for application service state. + /// The state monitor for application service state. /// The Soulseek client. /// The shared file cache. + /// The HttpClientFactory to use. public ManagementService( IOptionsMonitor optionsMonitor, - IStateMonitor serviceStateMonitor, + IStateMonitor applicationStateMonitor, ISoulseekClient soulseekClient, - ISharedFileCache sharedFileCache) + ISharedFileCache sharedFileCache, + IHttpClientFactory httpClientFactory) { OptionsMonitor = optionsMonitor; - ServiceStateMonitor = serviceStateMonitor; + ApplicationStateMonitor = applicationStateMonitor; Client = soulseekClient; SharedFileCache = sharedFileCache; + HttpClientFactory = httpClientFactory; } - /// - /// Gets the current state of the slskd service. - /// - public ApplicationState ApplicationState => ServiceStateMonitor.CurrentValue; + private IHttpClientFactory HttpClientFactory { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); /// /// 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 OptionsMonitor { get; } - private IStateMonitor ServiceStateMonitor { get; } + private IStateMonitor ApplicationStateMonitor { get; } private ISharedFileCache SharedFileCache { get; } /// @@ -95,5 +100,45 @@ namespace slskd.Management /// /// The operation context. public Task RescanSharesAsync() => SharedFileCache.FillAsync(); + + /// + /// Gets the version of the latest application release. + /// + /// The operation context. + 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(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 -- cgit v1.2.3