Files
annotations/Azaion.LoaderUI/Login.xaml.cs
T
2025-06-14 16:08:32 +03:00

99 lines
3.2 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Azaion.LoaderUI;
public partial class Login
{
private readonly IAzaionApi _azaionApi;
private readonly ILogger<Login> _logger;
private readonly DirectoriesConfig? _dirConfig;
public Login(IAzaionApi azaionApi, IOptions<DirectoriesConfig> directoriesConfig, ILogger<Login> logger)
{
_azaionApi = azaionApi;
_logger = logger;
_dirConfig = directoriesConfig.Value;
InitializeComponent();
}
private async void LoginClick(object sender, RoutedEventArgs e)
{
var creds = new ApiCredentials(TbEmail.Text, TbPassword.Password);
if (!creds.IsValid())
return;
LoginBtn.Cursor = Cursors.Wait;
Cursor = Cursors.Wait;
_azaionApi.Login(creds);
var installerVersion = await GetInstallerVer();
var localVersion = GetLocalVer();
if (installerVersion > localVersion)
{
TbStatus.Text = $"Updating from {localVersion} to {installerVersion}...";
await DownloadAndRunInstaller();
TbStatus.Text = $"Installed {installerVersion}!";
}
else
TbStatus.Text = $"Your version is up to date!";
Process.Start(Constants.AZAION_SUITE_EXE, $"-e {creds.Email} -p {creds.Password}");
Close();
}
private async Task DownloadAndRunInstaller()
{
var (installerName, stream) = await _azaionApi.DownloadInstaller(_dirConfig?.SuiteInstallerDirectory ?? "");
var localFileStream = new FileStream(installerName, FileMode.Create, FileAccess.Write);
await stream.CopyToAsync(localFileStream);
localFileStream.Close();
stream.Close();
var processInfo = new ProcessStartInfo(installerName)
{
UseShellExecute = true,
Arguments = "/VERYSILENT"
};
var process = Process.Start(processInfo);
await process!.WaitForExitAsync();
}
private async Task<Version> GetInstallerVer()
{
TbStatus.Text = "Checking for the newer version...";
var installerName = await _azaionApi.GetLastInstallerName(_dirConfig?.SuiteInstallerDirectory ?? Constants.SUITE_FOLDER);
var version = installerName
.Replace("AzaionSuite.Iterative.", "")
.Replace(".exe", "");
return new Version(version);
}
private Version GetLocalVer()
{
var localFileInfo = FileVersionInfo.GetVersionInfo(Constants.AZAION_SUITE_EXE);
if (string.IsNullOrWhiteSpace(localFileInfo.ProductVersion))
throw new Exception($"Can't find {Constants.AZAION_SUITE_EXE} and its version");
return new Version(localFileInfo.FileVersion!);
}
private void CloseClick(object sender, RoutedEventArgs e) => Close();
private void MainMouseMove(object sender, MouseEventArgs e)
{
if (e.OriginalSource is Button || e.OriginalSource is TextBox)
return;
if (e.LeftButton == MouseButtonState.Pressed)
DragMove();
}
}