write thumbnail to wallet store

This commit is contained in:
TheXamlGuy
2024-06-29 19:39:34 +01:00
parent c1713b1372
commit 29e41d821f
14 changed files with 117 additions and 76 deletions
+10 -7
View File
@@ -74,6 +74,8 @@ public partial class App : Application
services.AddTransient(_ => services.AddTransient(_ =>
provider.GetServices<IConfigurationDescriptor<ItemConfiguration>>()); provider.GetServices<IConfigurationDescriptor<ItemConfiguration>>());
services.AddTransient<IWalletFactory, WalletFactory>();
services.AddTransient<IKeyGenerator, KeyGenerator>(); services.AddTransient<IKeyGenerator, KeyGenerator>();
services.AddTransient<IEncryptor, AesEncryptor>(); services.AddTransient<IEncryptor, AesEncryptor>();
services.AddTransient<IDecryptor, AesDecryptor>(); services.AddTransient<IDecryptor, AesDecryptor>();
@@ -82,7 +84,7 @@ public partial class App : Application
services.AddTransient<IKeyDeriver, KeyDeriver>(); services.AddTransient<IKeyDeriver, KeyDeriver>();
services.AddTransient<ISecurityKeyFactory, SecurityKeyFactory>(); services.AddTransient<ISecurityKeyFactory, SecurityKeyFactory>();
services.AddTransient<IWalletStorageFactory, WalletStorageFactory>(); services.AddTransient<IWalletStoreFactory, WalletStoreFactory>();
services.AddTransient<IItemConfigurationCollection, ItemConfigurationCollection>(provider => services.AddTransient<IItemConfigurationCollection, ItemConfigurationCollection>(provider =>
{ {
@@ -105,7 +107,8 @@ public partial class App : Application
} }
}); });
services.AddHandler<ProfileImageHandler>(); services.AddHandler<ReadProfileImageHandler>();
services.AddHandler<QueryWalletHandler>(); services.AddHandler<QueryWalletHandler>();
services.AddHandler<ItemHandler>(); services.AddHandler<ItemHandler>();
services.AddHandler<CreateItemHandler>(); services.AddHandler<CreateItemHandler>();
@@ -212,13 +215,13 @@ public partial class App : Application
}); });
})!); })!);
services.AddTransient<IWalletFactory, WalletFactory>(); services.AddTransient<IWalletHostFactory, WalletHostFactory>();
services.AddHandler<CreateWalletHandler>();
services.AddHandler<ProfileImageHandler>();
services.AddSingleton<IWalletHostCollection, WalletHostCollection>(); services.AddSingleton<IWalletHostCollection, WalletHostCollection>();
services.AddInitializer<WalletInitializer>(); services.AddInitializer<WalletCollectionInitializer>();
services.AddHandler<CreateWalletHandler>();
services.AddHandler<ReadProfileImageHandler>();
services.AddTemplate<MainViewModel, MainView>("Main"); services.AddTemplate<MainViewModel, MainView>("Main");
services.AddHandler<SynchronizeMainViewModelHandler>(); services.AddHandler<SynchronizeMainViewModelHandler>();
+11 -24
View File
@@ -1,47 +1,34 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using System.Text;
using Toolkit.Foundation; using Toolkit.Foundation;
namespace Wallet; namespace Wallet;
public class CreateWalletHandler(IWalletFactory componentFactory, public class CreateWalletHandler(IWalletHostFactory componentFactory,
IPublisher publisher) : IPublisher publisher) :
IHandler<CreateEventArgs<Wallet<(string, string)>>, bool> IHandler<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>, bool>
{ {
public async Task<bool> Handle(CreateEventArgs<Wallet<(string, string)>> args, public async Task<bool> Handle(CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>> args,
CancellationToken cancellationToken) 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 } && name is { Length: > 0 } &&
password is { Length: > 0 }) password is { Length: > 0 })
{ {
if (componentFactory.Create(name) is IComponentHost host) if (componentFactory.Create(name) is IComponentHost host)
{ {
ISecurityKeyFactory keyFactory = host.Services.GetRequiredService<ISecurityKeyFactory>(); IWalletFactory factory = host.Services.GetRequiredService<IWalletFactory>();
IDecoratorService<SecurityKey> secureKeyStore = host.Services.GetRequiredService<IDecoratorService<SecurityKey>>(); if (await factory.Create(name, password, thumbnail))
IWalletStorageFactory WalletStorageFactory = host.Services.GetRequiredService<IWalletStorageFactory>();
if (keyFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key)
{ {
secureKeyStore.Set(key); host.Start();
publisher.Publish(Activated.As(new Wallet<IComponentHost>(host)));
if (await WalletStorageFactory.Create(name, key)) return true;
{
IWritableConfiguration<WalletConfiguration> configuration =
host.Services.GetRequiredService<IWritableConfiguration<WalletConfiguration>>();
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<IComponentHost>(host)));
return true;
}
} }
} }
} }
} }
+4 -4
View File
@@ -54,16 +54,16 @@ public partial class CreateWalletViewModel :
{ {
using (await new ActivityLock(this)) using (await new ActivityLock(this))
{ {
IsConfirmed = await Mediator.Handle<CreateEventArgs<Wallet<(string, string)>>, IsConfirmed = await Mediator.Handle<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>,
bool>(Create.As(new Wallet<(string, string)>((Name, Password)))); bool>(Create.As(new Wallet<(string, string, IImageDescriptor?)>((Name, Password, ImageDescriptor))));
return IsConfirmed; return IsConfirmed;
} }
} }
[RelayCommand] [RelayCommand]
public async Task Import() => ImageDescriptor = await Mediator.Handle<RequestEventArgs<ProfileImage>, public async Task Import() => ImageDescriptor = await Mediator.Handle<ReadEventArgs<ProfileImage>,
IImageDescriptor>(Request.As<ProfileImage>()); IImageDescriptor>(Read.As<ProfileImage>());
protected override void OnPropertyChanged(PropertyChangedEventArgs args) protected override void OnPropertyChanged(PropertyChangedEventArgs args)
{ {
+7 -6
View File
@@ -1,9 +1,10 @@
using Toolkit.Foundation; using Toolkit.Foundation;
namespace Wallet namespace Wallet;
public interface IWalletFactory
{ {
public interface IWalletFactory Task<bool> Create(string name,
{ string password,
IComponentHost? Create(string key); IImageDescriptor thumbnail);
} }
}
+8
View File
@@ -0,0 +1,8 @@
using Toolkit.Foundation;
namespace Wallet;
public interface IWalletHostFactory
{
IComponentHost? Create(string key);
}
@@ -1,6 +1,6 @@
namespace Wallet; namespace Wallet;
public interface IWalletStorageFactory public interface IWalletStoreFactory
{ {
Task<bool> Create(string name, SecurityKey key); Task<bool> Create(string name, SecurityKey key);
} }
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Wallet;
public class OpenWalletHandler(IConfigurationDescriptor<WalletConfiguration> descriptor, public class OpenWalletHandler(IConfigurationDescriptor<WalletConfiguration> descriptor,
ISecurityKeyFactory securityKeyFactory, ISecurityKeyFactory securityKeyFactory,
IWalletStorageFactory WalletStorageFactory) : IWalletStoreFactory WalletStorageFactory) :
IHandler<ActivateEventArgs<Wallet<string>>, bool> IHandler<ActivateEventArgs<Wallet<string>>, bool>
{ {
public async Task<bool> Handle(ActivateEventArgs<Wallet<string>> args, public async Task<bool> Handle(ActivateEventArgs<Wallet<string>> args,
+3
View File
@@ -1,3 +1,6 @@
namespace Wallet; namespace Wallet;
public record ProfileImage; public record ProfileImage;
public record ProfileImage<TValue>(TValue Value);
-22
View File
@@ -1,22 +0,0 @@
using Toolkit.Foundation;
namespace Wallet;
public class ProfileImageHandler(IFileProvider fileProvider,
IImageProvider imageProvider) :
IHandler<RequestEventArgs<ProfileImage>, IImageDescriptor?>
{
public async Task<IImageDescriptor?> Handle(RequestEventArgs<ProfileImage> 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;
}
}
+23
View File
@@ -0,0 +1,23 @@
using Toolkit.Foundation;
namespace Wallet;
public class ReadProfileImageHandler(IFileProvider fileProvider,
IImageReader imageReader) :
IHandler<ReadEventArgs<ProfileImage>, IImageDescriptor?>
{
public async Task<IImageDescriptor?> Handle(ReadEventArgs<ProfileImage> 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;
}
}
@@ -3,7 +3,7 @@ using Toolkit.Foundation;
namespace Wallet; namespace Wallet;
public class WalletInitializer(IHostEnvironment environment, public class WalletCollectionInitializer(IHostEnvironment environment,
IComponentFactory componentFactory, IComponentFactory componentFactory,
IWalletHostCollection Wallets) : IWalletHostCollection Wallets) :
IInitialization IInitialization
+28 -8
View File
@@ -1,18 +1,38 @@
using Toolkit.Foundation; using Microsoft.Extensions.Hosting;
using System.Text;
using Toolkit.Foundation;
namespace Wallet; namespace Wallet;
public class WalletFactory(IComponentFactory componentFactory) : public class WalletFactory(ISecurityKeyFactory securityKeyFactory,
IDecoratorService<SecurityKey> secureKeyStore,
IWalletStoreFactory walletStoreFactory,
IWritableConfiguration<WalletConfiguration> configuration,
IHostEnvironment environment,
IImageWriter imageWriter) :
IWalletFactory IWalletFactory
{ {
public IComponentHost? Create(string key) public async Task<bool> Create(string name,
string password,
IImageDescriptor thumbnail)
{ {
if (componentFactory.Create<WalletComponent, WalletConfiguration>($"Wallet:{key}", if (securityKeyFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key)
new WalletConfiguration()) is IComponentHost host)
{ {
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;
} }
} }
+18
View File
@@ -0,0 +1,18 @@
using Toolkit.Foundation;
namespace Wallet;
public class WalletHostFactory(IComponentFactory componentFactory) :
IWalletHostFactory
{
public IComponentHost? Create(string key)
{
if (componentFactory.Create<WalletComponent, WalletConfiguration>($"Wallet:{key}",
new WalletConfiguration()) is IComponentHost host)
{
return host;
}
return default;
}
}
@@ -6,10 +6,10 @@ using Toolkit.Foundation;
namespace Wallet; namespace Wallet;
public class WalletStorageFactory(IDecoratorService<WalletConnection> connection, public class WalletStoreFactory(IDecoratorService<WalletConnection> connection,
IHostEnvironment environment, IHostEnvironment environment,
IServiceProvider provider) : IServiceProvider provider) :
IWalletStorageFactory IWalletStoreFactory
{ {
public async Task<bool> Create(string name, public async Task<bool> Create(string name,
SecurityKey key) SecurityKey key)