Files
annotations/Azaion.LoaderUI/Login.xaml.cs
T
2025-06-13 23:06:48 +03:00

85 lines
2.5 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Extensions.Options;
namespace Azaion.LoaderUI;
public partial class Login
{
private readonly IAzaionApi _azaionApi;
private readonly DirectoriesConfig _dirConfig;
public Login(IAzaionApi azaionApi, IOptions<DirectoriesConfig> directoriesConfig)
{
_azaionApi = azaionApi;
_dirConfig = directoriesConfig.Value;
InitializeComponent();
}
private 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);
if (GetInstallerVer() > GetLocalVer())
DownloadAndRunInstaller();
Process.Start(Constants.AZAION_SUITE_EXE, $"-e {creds.Email} -p {creds.Password}");
Close();
}
private void DownloadAndRunInstaller()
{
var (installerName, stream) = _azaionApi.DownloadInstaller(_dirConfig.SuiteInstallerDirectory);
var localFileStream = new FileStream(installerName, FileMode.Create, FileAccess.Write);
stream.CopyTo(localFileStream);
localFileStream.Close();
stream.Close();
var processInfo = new ProcessStartInfo(installerName)
{
UseShellExecute = true,
Verb = "runas"
};
Process.Start(processInfo);
}
private Version GetInstallerVer()
{
var installerName = _azaionApi.GetLastInstallerName(_dirConfig.SuiteInstallerDirectory);
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();
}
}