diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-09-06 22:12:57 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-09-06 22:12:57 -0500 |
| commit | c7f63e0468c0ca034986d8c72627f9a02040ba43 (patch) | |
| tree | 79f3a957c1d4eacdde5c025f8f44c24b966c3c4e /src | |
| parent | edf86bfb54d31c1a0b02e7d416806d260dd5b7f2 (diff) | |
| download | slskd-c7f63e0468c0ca034986d8c72627f9a02040ba43.tar.gz | |
add CheckVersionAsync
Diffstat (limited to 'src')
3 files changed, 89 insertions, 9 deletions
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(); } + + /// <summary> + /// Gets the current application version. + /// </summary> + /// <returns></returns> + [HttpGet("version")] + [Authorize] + public IActionResult GetVersion() + { + return Ok(Program.InformationalVersion); + } + + /// <summary> + /// Checks for updates. + /// </summary> + /// <returns></returns> + [HttpGet("version/latest")] + [Authorize] + public async Task<IActionResult> 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 /// </summary> /// <returns>The operation context.</returns> Task RescanSharesAsync(); + + /// <summary> + /// Gets the version of the latest application release. + /// </summary> + /// <returns>The operation context.</returns> + 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; /// <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 |
