Remotely/Server/Services/UpgradeService.cs
Jared Goodwin 3f8d63c2c0
Azure Pipelines, Docker, and embedded server data. (#543)
* Add Azure Pipelines yml.

* Remove Server Installer.

* Update Release Build.yml for Azure Pipelines

* Update Release Build.yml

* Update Release Build.yml for Azure Pipelines

* Update Release Build.yml for Azure Pipelines

* Update Release Build.yml for Azure Pipelines

* Update Release Build.yml for Azure Pipelines

* Update signtool.exe

* Update Release Build.yml

* Update Publish.ps1

* Update pipeline and Dockerfile.

* Move docker files.

* Update Dockerfile

* Update Dockerfile

* Update Dockerfile

* Update Dockerfile

* Move files.

* Update Dockerfile

* Update Dockerfile

* Create RewritableStream.

* Finish implementation of rewritable stream and embedded data searcher.

* Remove RelayCode.

* Get branding from default org if orgId is missing.

* Update README.md

* Remove AppConstants.ServerUrl.  Fix main module file path.

* Update submodule.

* Extract embedded data in Program.cs.

* Add logging.  Update submodule.

* Remove size block.  BinaryWriter prefixes size.

* Remove unused async

* Update Immense.RemoteControl

* Update Release Build.yml for Azure Pipelines

* Update Release Build.yml for Azure Pipelines

* Use UpgradeService for determining out of date clients.
2022-12-31 19:10:40 -08:00

80 lines
2.6 KiB
C#

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;
namespace Remotely.Server.Services
{
public interface IUpgradeService
{
Version GetCurrentVersion();
Task<bool> IsNewVersionAvailable();
}
public class UpgradeService : IUpgradeService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger<UpgradeService> _logger;
private Version _currentVersion;
public UpgradeService(IHttpClientFactory httpClientFactory, ILogger<UpgradeService> logger)
{
_httpClientFactory = httpClientFactory;
_logger = logger;
}
public Version GetCurrentVersion()
{
try
{
if (_currentVersion is not null)
{
return _currentVersion;
}
var fileVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo("Remotely_Server.dll").FileVersion;
if (Version.TryParse(fileVersion, out var result))
{
_currentVersion = result;
return _currentVersion;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while getting current version.");
}
_currentVersion = new(1, 0);
return _currentVersion;
}
public async Task<bool> IsNewVersionAvailable()
{
try
{
using var client = _httpClientFactory.CreateClient();
var response = await client.GetAsync("https://github.com/immense/Remotely/releases/latest");
var versionString = response.RequestMessage.RequestUri.ToString().Split("/").Last()[1..];
var remoteVersion = Version.Parse(versionString);
var filePath = Directory.GetFiles(Directory.GetCurrentDirectory(), "Remotely_Server.dll", SearchOption.AllDirectories).First();
var localVersion = Version.Parse(System.Diagnostics.FileVersionInfo.GetVersionInfo(filePath).FileVersion);
if (remoteVersion > localVersion)
{
return true;
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to retrieve latest version info.");
}
return false;
}
}
}