From 29e41d821f53d43a012aa2cd9687370d28ba1a99 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sat, 29 Jun 2024 19:39:34 +0100 Subject: [PATCH] write thumbnail to wallet store --- Wallet.Avalonia/App.axaml.cs | 17 +++++---- Wallet/CreateWalletHandler.cs | 35 ++++++------------ Wallet/CreateWalletViewModel.cs | 8 ++--- Wallet/IWalletFactory.cs | 13 +++---- Wallet/IWalletHostFactory.cs | 8 +++++ ...orageFactory.cs => IWalletStoreFactory.cs} | 2 +- Wallet/OpenWalletHandler.cs | 2 +- Wallet/ProfileImage.cs | 3 ++ Wallet/ProfileImageHandler.cs | 22 ------------ Wallet/ReadProfileImageHandler.cs | 23 ++++++++++++ ...izer.cs => WalletCollectionInitializer.cs} | 2 +- Wallet/WalletFactory.cs | 36 ++++++++++++++----- Wallet/WalletHostFactory.cs | 18 ++++++++++ ...torageFactory.cs => WalletStoreFactory.cs} | 4 +-- 14 files changed, 117 insertions(+), 76 deletions(-) create mode 100644 Wallet/IWalletHostFactory.cs rename Wallet/{IWalletStorageFactory.cs => IWalletStoreFactory.cs} (66%) delete mode 100644 Wallet/ProfileImageHandler.cs create mode 100644 Wallet/ReadProfileImageHandler.cs rename Wallet/{WalletInitializer.cs => WalletCollectionInitializer.cs} (90%) create mode 100644 Wallet/WalletHostFactory.cs rename Wallet/{WalletStorageFactory.cs => WalletStoreFactory.cs} (91%) diff --git a/Wallet.Avalonia/App.axaml.cs b/Wallet.Avalonia/App.axaml.cs index 4c4ca0b..f87e394 100644 --- a/Wallet.Avalonia/App.axaml.cs +++ b/Wallet.Avalonia/App.axaml.cs @@ -74,6 +74,8 @@ public partial class App : Application services.AddTransient(_ => provider.GetServices>()); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -82,7 +84,7 @@ public partial class App : Application services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(provider => { @@ -105,7 +107,8 @@ public partial class App : Application } }); - services.AddHandler(); + services.AddHandler(); + services.AddHandler(); services.AddHandler(); services.AddHandler(); @@ -212,13 +215,13 @@ public partial class App : Application }); })!); - services.AddTransient(); - - services.AddHandler(); - services.AddHandler(); + services.AddTransient(); services.AddSingleton(); - services.AddInitializer(); + services.AddInitializer(); + + services.AddHandler(); + services.AddHandler(); services.AddTemplate("Main"); services.AddHandler(); diff --git a/Wallet/CreateWalletHandler.cs b/Wallet/CreateWalletHandler.cs index 7ee5955..d1f825a 100644 --- a/Wallet/CreateWalletHandler.cs +++ b/Wallet/CreateWalletHandler.cs @@ -1,47 +1,34 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using System.Text; using Toolkit.Foundation; namespace Wallet; -public class CreateWalletHandler(IWalletFactory componentFactory, +public class CreateWalletHandler(IWalletHostFactory componentFactory, IPublisher publisher) : - IHandler>, bool> + IHandler>, bool> { - public async Task Handle(CreateEventArgs> args, + public async Task Handle(CreateEventArgs> args, CancellationToken cancellationToken) { - if (args.Sender is Wallet <(string, string)> Wallet) + if (args.Sender is Wallet <(string, string, IImageDescriptor?)> Wallet) { - if (Wallet.Value is (string name, string password) && + if (Wallet.Value is (string name, string password, + IImageDescriptor thumbnail) && name is { Length: > 0 } && password is { Length: > 0 }) { if (componentFactory.Create(name) is IComponentHost host) { - ISecurityKeyFactory keyFactory = host.Services.GetRequiredService(); - IDecoratorService secureKeyStore = host.Services.GetRequiredService>(); - IWalletStorageFactory WalletStorageFactory = host.Services.GetRequiredService(); - - if (keyFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key) + IWalletFactory factory = host.Services.GetRequiredService(); + if (await factory.Create(name, password, thumbnail)) { - secureKeyStore.Set(key); + host.Start(); + publisher.Publish(Activated.As(new Wallet(host))); - if (await WalletStorageFactory.Create(name, key)) - { - IWritableConfiguration configuration = - host.Services.GetRequiredService>(); - - configuration.Write(args => args.Key = $"{Convert.ToBase64String(key.Salt)}:{Convert.ToBase64String(key.EncryptedKey)}:{Convert.ToBase64String(key.DecryptedKey)}"); - host.Start(); - - publisher.Publish(Activated.As(new Wallet(host))); - return true; - } + return true; } } - } } diff --git a/Wallet/CreateWalletViewModel.cs b/Wallet/CreateWalletViewModel.cs index f1977ec..3169502 100644 --- a/Wallet/CreateWalletViewModel.cs +++ b/Wallet/CreateWalletViewModel.cs @@ -54,16 +54,16 @@ public partial class CreateWalletViewModel : { using (await new ActivityLock(this)) { - IsConfirmed = await Mediator.Handle>, - bool>(Create.As(new Wallet<(string, string)>((Name, Password)))); + IsConfirmed = await Mediator.Handle>, + bool>(Create.As(new Wallet<(string, string, IImageDescriptor?)>((Name, Password, ImageDescriptor)))); return IsConfirmed; } } [RelayCommand] - public async Task Import() => ImageDescriptor = await Mediator.Handle, - IImageDescriptor>(Request.As()); + public async Task Import() => ImageDescriptor = await Mediator.Handle, + IImageDescriptor>(Read.As()); protected override void OnPropertyChanged(PropertyChangedEventArgs args) { diff --git a/Wallet/IWalletFactory.cs b/Wallet/IWalletFactory.cs index 61486ec..0ee1d2b 100644 --- a/Wallet/IWalletFactory.cs +++ b/Wallet/IWalletFactory.cs @@ -1,9 +1,10 @@ using Toolkit.Foundation; -namespace Wallet +namespace Wallet; + +public interface IWalletFactory { - public interface IWalletFactory - { - IComponentHost? Create(string key); - } -} \ No newline at end of file + Task Create(string name, + string password, + IImageDescriptor thumbnail); +} diff --git a/Wallet/IWalletHostFactory.cs b/Wallet/IWalletHostFactory.cs new file mode 100644 index 0000000..f5a2906 --- /dev/null +++ b/Wallet/IWalletHostFactory.cs @@ -0,0 +1,8 @@ +using Toolkit.Foundation; + +namespace Wallet; + +public interface IWalletHostFactory +{ + IComponentHost? Create(string key); +} \ No newline at end of file diff --git a/Wallet/IWalletStorageFactory.cs b/Wallet/IWalletStoreFactory.cs similarity index 66% rename from Wallet/IWalletStorageFactory.cs rename to Wallet/IWalletStoreFactory.cs index 7309d38..a5915ef 100644 --- a/Wallet/IWalletStorageFactory.cs +++ b/Wallet/IWalletStoreFactory.cs @@ -1,6 +1,6 @@ namespace Wallet; -public interface IWalletStorageFactory +public interface IWalletStoreFactory { Task Create(string name, SecurityKey key); } \ No newline at end of file diff --git a/Wallet/OpenWalletHandler.cs b/Wallet/OpenWalletHandler.cs index d597c57..68aa50f 100644 --- a/Wallet/OpenWalletHandler.cs +++ b/Wallet/OpenWalletHandler.cs @@ -5,7 +5,7 @@ namespace Wallet; public class OpenWalletHandler(IConfigurationDescriptor descriptor, ISecurityKeyFactory securityKeyFactory, - IWalletStorageFactory WalletStorageFactory) : + IWalletStoreFactory WalletStorageFactory) : IHandler>, bool> { public async Task Handle(ActivateEventArgs> args, diff --git a/Wallet/ProfileImage.cs b/Wallet/ProfileImage.cs index 44633e9..3787b87 100644 --- a/Wallet/ProfileImage.cs +++ b/Wallet/ProfileImage.cs @@ -1,3 +1,6 @@ namespace Wallet; public record ProfileImage; + +public record ProfileImage(TValue Value); + diff --git a/Wallet/ProfileImageHandler.cs b/Wallet/ProfileImageHandler.cs deleted file mode 100644 index ddade58..0000000 --- a/Wallet/ProfileImageHandler.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Toolkit.Foundation; - -namespace Wallet; - -public class ProfileImageHandler(IFileProvider fileProvider, - IImageProvider imageProvider) : - IHandler, IImageDescriptor?> -{ - public async Task Handle(RequestEventArgs args, - CancellationToken cancellationToken) - { - if (await fileProvider.SelectFiles(new FileFilter("Image files", ["jpg", "jpeg", "png"])) is { Count: 1 } files) - { - if (files.FirstOrDefault() is string file) - { - return await imageProvider.Get(file, 200, 200, true); - } - } - - return default; - } -} diff --git a/Wallet/ReadProfileImageHandler.cs b/Wallet/ReadProfileImageHandler.cs new file mode 100644 index 0000000..b8d39ef --- /dev/null +++ b/Wallet/ReadProfileImageHandler.cs @@ -0,0 +1,23 @@ +using Toolkit.Foundation; + +namespace Wallet; + +public class ReadProfileImageHandler(IFileProvider fileProvider, + IImageReader imageReader) : + IHandler, IImageDescriptor?> +{ + public async Task Handle(ReadEventArgs args, + CancellationToken cancellationToken) + { + if (await fileProvider.SelectFiles(new FileFilter("Image files", ["jpg", "jpeg", "png"])) is { Count: 1 } files) + { + if (files.FirstOrDefault() is string file) + { + await using FileStream stream = File.OpenRead(file); + return await imageReader.Get(stream, 200, 200, true); + } + } + + return default; + } +} diff --git a/Wallet/WalletInitializer.cs b/Wallet/WalletCollectionInitializer.cs similarity index 90% rename from Wallet/WalletInitializer.cs rename to Wallet/WalletCollectionInitializer.cs index e33324d..8b93ea7 100644 --- a/Wallet/WalletInitializer.cs +++ b/Wallet/WalletCollectionInitializer.cs @@ -3,7 +3,7 @@ using Toolkit.Foundation; namespace Wallet; -public class WalletInitializer(IHostEnvironment environment, +public class WalletCollectionInitializer(IHostEnvironment environment, IComponentFactory componentFactory, IWalletHostCollection Wallets) : IInitialization diff --git a/Wallet/WalletFactory.cs b/Wallet/WalletFactory.cs index 0211388..b2d1c63 100644 --- a/Wallet/WalletFactory.cs +++ b/Wallet/WalletFactory.cs @@ -1,18 +1,38 @@ -using Toolkit.Foundation; +using Microsoft.Extensions.Hosting; +using System.Text; +using Toolkit.Foundation; namespace Wallet; -public class WalletFactory(IComponentFactory componentFactory) : +public class WalletFactory(ISecurityKeyFactory securityKeyFactory, + IDecoratorService secureKeyStore, + IWalletStoreFactory walletStoreFactory, + IWritableConfiguration configuration, + IHostEnvironment environment, + IImageWriter imageWriter) : IWalletFactory { - public IComponentHost? Create(string key) + public async Task Create(string name, + string password, + IImageDescriptor thumbnail) { - if (componentFactory.Create($"Wallet:{key}", - new WalletConfiguration()) is IComponentHost host) + if (securityKeyFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key) { - return host; + secureKeyStore.Set(key); + + if (await walletStoreFactory.Create(name, key)) + { + configuration.Write(args => args.Key = $"{Convert.ToBase64String(key.Salt)}:" + + $"{Convert.ToBase64String(key.EncryptedKey)}:{Convert.ToBase64String(key.DecryptedKey)}"); + + string file = Path.Combine(environment.ContentRootPath, "Thumbnail.png"); + using FileStream stream = File.OpenWrite(file); + + imageWriter.Write(thumbnail, stream); + return true; + } } - return default; + return false; } -} \ No newline at end of file +} diff --git a/Wallet/WalletHostFactory.cs b/Wallet/WalletHostFactory.cs new file mode 100644 index 0000000..e6ee0a9 --- /dev/null +++ b/Wallet/WalletHostFactory.cs @@ -0,0 +1,18 @@ +using Toolkit.Foundation; + +namespace Wallet; + +public class WalletHostFactory(IComponentFactory componentFactory) : + IWalletHostFactory +{ + public IComponentHost? Create(string key) + { + if (componentFactory.Create($"Wallet:{key}", + new WalletConfiguration()) is IComponentHost host) + { + return host; + } + + return default; + } +} \ No newline at end of file diff --git a/Wallet/WalletStorageFactory.cs b/Wallet/WalletStoreFactory.cs similarity index 91% rename from Wallet/WalletStorageFactory.cs rename to Wallet/WalletStoreFactory.cs index 0859e51..14852d3 100644 --- a/Wallet/WalletStorageFactory.cs +++ b/Wallet/WalletStoreFactory.cs @@ -6,10 +6,10 @@ using Toolkit.Foundation; namespace Wallet; -public class WalletStorageFactory(IDecoratorService connection, +public class WalletStoreFactory(IDecoratorService connection, IHostEnvironment environment, IServiceProvider provider) : - IWalletStorageFactory + IWalletStoreFactory { public async Task Create(string name, SecurityKey key)