From 4ece47620571f9f1aece0f68ab560770c2f77c0f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 6 Sep 2021 22:10:20 -0500 Subject: add CheckVersionResponse DTO --- src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs (limited to 'src/slskd/Application/Management/API') diff --git a/src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs b/src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs new file mode 100644 index 00000000..0e8e0097 --- /dev/null +++ b/src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs @@ -0,0 +1,8 @@ +namespace slskd.Management.API +{ + public class CheckVersionResponse + { + public bool? UpdateAvailable { get; init; } + public string LatestVersion { get; init; } + } +} -- cgit v1.2.3 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/slskd/Application/Management/API') 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 From d72995333092c7bc31528b23d770024034957137 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 8 Sep 2021 09:14:06 -0500 Subject: inject IApplication and implement version check --- .../Application/Management/API/Controllers/ApplicationController.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/slskd/Application/Management/API') diff --git a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs index 850dfda9..0ba8b655 100644 --- a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs +++ b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs @@ -34,12 +34,14 @@ namespace slskd.Management.API [Consumes("application/json")] public class ApplicationController : ControllerBase { - public ApplicationController(IManagementService managementService, IHostApplicationLifetime lifetime) + public ApplicationController(IManagementService managementService, IHostApplicationLifetime lifetime, IApplication application) { Management = managementService; Lifetime = lifetime; + Application = application; } + private IApplication Application { get; } private IManagementService Management { get; } private IHostApplicationLifetime Lifetime { get; } @@ -101,7 +103,7 @@ namespace slskd.Management.API { if (forceCheck) { - await Management.CheckVersionAsync(); + await Application.CheckVersionAsync(); } var state = Management.ApplicationState; -- cgit v1.2.3 From 25ad4f7c4b03636e7126af7f15d37ccf2bc08ced Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 9 Sep 2021 09:01:14 -0500 Subject: remove CheckVersionResponse --- src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs (limited to 'src/slskd/Application/Management/API') diff --git a/src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs b/src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs deleted file mode 100644 index 0e8e0097..00000000 --- a/src/slskd/Application/Management/API/DTO/CheckVersionResponse.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace slskd.Management.API -{ - public class CheckVersionResponse - { - public bool? UpdateAvailable { get; init; } - public string LatestVersion { get; init; } - } -} -- cgit v1.2.3 From eae8db1aecdae631edc0f2d370d3c704d49c58b9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 9 Sep 2021 09:02:24 -0500 Subject: return version state rather than DTO --- .../Application/Management/API/Controllers/ApplicationController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Application/Management/API') diff --git a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs index 0ba8b655..b3d43300 100644 --- a/src/slskd/Application/Management/API/Controllers/ApplicationController.cs +++ b/src/slskd/Application/Management/API/Controllers/ApplicationController.cs @@ -107,7 +107,7 @@ namespace slskd.Management.API } var state = Management.ApplicationState; - return Ok(new CheckVersionResponse() { UpdateAvailable = state.UpdateAvailable, LatestVersion = state.LatestVersion }); + return Ok(state.Version); } } } -- cgit v1.2.3