fix keyboard catch, fix loading

refactoring
This commit is contained in:
Alex Bezdieniezhnykh
2024-11-25 13:03:46 +02:00
parent b61bed3b51
commit 30b9b9aa80
18 changed files with 88 additions and 117 deletions
+1
View File
@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
+12
View File
@@ -0,0 +1,12 @@
using CommandLine;
namespace Azaion.Common.DTO;
public class ApiCredentials
{
[Option('e', "email", Required = true, HelpText = "The email for authorization.")]
public string Email { get; set; } = null!;
[Option('p', "password", Required = true, HelpText = "The password for authorization.")]
public string Password { get; set; } = null!;
}
+1
View File
@@ -5,4 +5,5 @@ public interface IAzaionModule
string Name { get; }
string SvgIcon { get; }
Type MainWindowType { get; }
WindowEnum WindowEnum { get; }
}
@@ -1,20 +0,0 @@
using System.Windows;
using Azaion.Common.DTO;
namespace Azaion.Common.Extensions;
public static class WindowExtensions
{
public static WindowEnum GetParentWindow(this FrameworkElement? element)
{
while (element != null && element is not Window)
{
element = element.Parent as FrameworkElement;
}
if (element is not Window)
return WindowEnum.None;
return WindowEnum.Annotator;
}
}
+6 -6
View File
@@ -17,13 +17,13 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
private SecureString Password { get; set; } = new();
private string JwtToken { get; set; } = null!;
public void Login(string email, string password)
public void EnterCredentials(ApiCredentials credentials)
{
if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
if (string.IsNullOrWhiteSpace(credentials.Email) || string.IsNullOrWhiteSpace(credentials.Password))
throw new Exception("Email or password is empty!");
Email = email;
Password = password.ToSecureString();
Email = credentials.Email;
Password = credentials.Password.ToSecureString();
}
public async Task<Stream> GetResource(string fileName, string password, HardwareInfo hardware)
@@ -38,7 +38,7 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
private async Task<string> Authorize()
{
if (string.IsNullOrEmpty(Email) || Password.Length == 0)
throw new Exception("Email or password is empty! Please do Login first!");
throw new Exception("Email or password is empty! Please do EnterCredentials first!");
var payload = new
{
@@ -50,7 +50,7 @@ public class AzaionApiClient(HttpClient httpClient) : IDisposable
new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, JSON_MEDIA));
if (!response.IsSuccessStatusCode)
throw new Exception($"Login failed: {response.StatusCode}");
throw new Exception($"EnterCredentials failed: {response.StatusCode}");
var responseData = await response.Content.ReadAsStringAsync();
+28 -3
View File
@@ -1,20 +1,45 @@
using System.IO;
using System.Reflection;
using Azaion.Common.DTO;
namespace Azaion.Common.Services;
public interface IResourceLoader
{
Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default);
Assembly LoadAssembly(string asmName);
}
public class ResourceLoader(string email, string password, AzaionApiClient api, IHardwareService hardwareService) : IResourceLoader
public class ResourceLoader(AzaionApiClient api, ApiCredentials credentials) : IResourceLoader
{
private static readonly List<string> EncryptedResources =
[
"Azaion.Annotator",
"Azaion.Dataset"
];
public Assembly LoadAssembly(string resourceName)
{
var assemblyName = resourceName.Split(',').First();
if (EncryptedResources.Contains(assemblyName))
{
var stream = Load(assemblyName).GetAwaiter().GetResult();
return Assembly.Load(stream.ToArray());
}
var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.GetName().Name == assemblyName);
return loadedAssembly;
}
public async Task<MemoryStream> Load(string fileName, CancellationToken cancellationToken = default)
{
var hardwareService = new HardwareService();
var hardwareInfo = await hardwareService.GetHardware();
var encryptedStream = await api.GetResource(fileName, password, hardwareInfo);
var encryptedStream = await api.GetResource(fileName, credentials.Password, hardwareInfo);
var key = Security.MakeEncryptionKey(email, password, hardwareInfo.Hash);
var key = Security.MakeEncryptionKey(credentials.Email, credentials.Password, hardwareInfo.Hash);
var stream = new MemoryStream();
await encryptedStream.DecryptTo(stream, key, cancellationToken);
return stream;