mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 06:46:30 +00:00
128 lines
4.5 KiB
C#
128 lines
4.5 KiB
C#
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using Azaion.Common;
|
|
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 void SetControlsStatus(bool isLoading)
|
|
{
|
|
LoginBtn.IsEnabled = !isLoading;
|
|
TbEmail.IsEnabled = !isLoading;
|
|
TbPassword.IsEnabled = !isLoading;
|
|
LoginBtn.Cursor = isLoading ? Cursors.Wait : Cursors.Arrow;
|
|
Cursor = isLoading ? Cursors.Wait : Cursors.Arrow;
|
|
}
|
|
|
|
private async void LoginClick(object sender, RoutedEventArgs e)
|
|
{
|
|
var credentials = new ApiCredentials
|
|
{
|
|
Email = TbEmail.Text,
|
|
Password = TbPassword.Password
|
|
};
|
|
if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password))
|
|
return;
|
|
|
|
try
|
|
{
|
|
SetControlsStatus(isLoading: true);
|
|
await _azaionApi.Validate(credentials);
|
|
|
|
TbStatus.Foreground = Brushes.Black;
|
|
var localVersion = Constants.GetLocalVersion();
|
|
var installerVersion = await GetInstallerVer() ?? localVersion;
|
|
var credsEncrypted = Security.Encrypt(credentials);
|
|
|
|
if (installerVersion > localVersion)
|
|
{
|
|
TbStatus.Text = $"Updating from {localVersion} to {installerVersion}...";
|
|
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();
|
|
Process.Start(new ProcessStartInfo
|
|
{
|
|
FileName = "cmd.exe",
|
|
Arguments = $"/c updater.cmd {Process.GetCurrentProcess().Id} {installerName} {Constants.AZAION_SUITE_EXE} \"{credsEncrypted}\""
|
|
});
|
|
}
|
|
else
|
|
{
|
|
TbStatus.Text = "Your version is up to date!";
|
|
|
|
Process.Start(Constants.AZAION_SUITE_EXE, $"-c {credsEncrypted}");
|
|
await Task.Delay(800);
|
|
TbStatus.Text = "Loading...";
|
|
while (!Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Constants.EXTERNAL_INFERENCE_PATH)).Any())
|
|
await Task.Delay(500);
|
|
await Task.Delay(1500);
|
|
}
|
|
|
|
Close();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
_logger.LogError(exception, exception.Message);
|
|
TbStatus.Foreground = Brushes.Red;
|
|
TbStatus.Text = exception.Message;
|
|
SetControlsStatus(isLoading: false);
|
|
}
|
|
}
|
|
|
|
private async Task<Version?> GetInstallerVer()
|
|
{
|
|
TbStatus.Text = "Checking for the newer version...";
|
|
var installerDir = string.IsNullOrWhiteSpace(_dirConfig?.SuiteInstallerDirectory)
|
|
? ConstantsLoader.SUITE_FOLDER
|
|
: _dirConfig.SuiteInstallerDirectory;
|
|
var installerName = await _azaionApi.GetLastInstallerName(installerDir);
|
|
try
|
|
{
|
|
var match = Regex.Match(installerName, @"\d+(\.\d+)+");
|
|
if (!match.Success)
|
|
throw new Exception($"Can't find version in {installerName}");
|
|
return new Version(match.Value);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
TbStatus.Text = $"Exception during the check version {e.Message}";
|
|
_logger.LogError(e, e.Message);
|
|
await Task.Delay(1500);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|