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
+11 -24
View File
@@ -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<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)
{
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<ISecurityKeyFactory>();
IDecoratorService<SecurityKey> secureKeyStore = host.Services.GetRequiredService<IDecoratorService<SecurityKey>>();
IWalletStorageFactory WalletStorageFactory = host.Services.GetRequiredService<IWalletStorageFactory>();
if (keyFactory.Create(Encoding.UTF8.GetBytes(password)) is SecurityKey key)
IWalletFactory factory = host.Services.GetRequiredService<IWalletFactory>();
if (await factory.Create(name, password, thumbnail))
{
secureKeyStore.Set(key);
host.Start();
publisher.Publish(Activated.As(new Wallet<IComponentHost>(host)));
if (await WalletStorageFactory.Create(name, key))
{
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;
}
return true;
}
}
}
}
+4 -4
View File
@@ -54,16 +54,16 @@ public partial class CreateWalletViewModel :
{
using (await new ActivityLock(this))
{
IsConfirmed = await Mediator.Handle<CreateEventArgs<Wallet<(string, string)>>,
bool>(Create.As(new Wallet<(string, string)>((Name, Password))));
IsConfirmed = await Mediator.Handle<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>,
bool>(Create.As(new Wallet<(string, string, IImageDescriptor?)>((Name, Password, ImageDescriptor))));
return IsConfirmed;
}
}
[RelayCommand]
public async Task Import() => ImageDescriptor = await Mediator.Handle<RequestEventArgs<ProfileImage>,
IImageDescriptor>(Request.As<ProfileImage>());
public async Task Import() => ImageDescriptor = await Mediator.Handle<ReadEventArgs<ProfileImage>,
IImageDescriptor>(Read.As<ProfileImage>());
protected override void OnPropertyChanged(PropertyChangedEventArgs args)
{
+7 -6
View File
@@ -1,9 +1,10 @@
using Toolkit.Foundation;
namespace Wallet
namespace Wallet;
public interface IWalletFactory
{
public interface IWalletFactory
{
IComponentHost? Create(string key);
}
}
Task<bool> Create(string name,
string password,
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;
public interface IWalletStorageFactory
public interface IWalletStoreFactory
{
Task<bool> Create(string name, SecurityKey key);
}
+1 -1
View File
@@ -5,7 +5,7 @@ namespace Wallet;
public class OpenWalletHandler(IConfigurationDescriptor<WalletConfiguration> descriptor,
ISecurityKeyFactory securityKeyFactory,
IWalletStorageFactory WalletStorageFactory) :
IWalletStoreFactory WalletStorageFactory) :
IHandler<ActivateEventArgs<Wallet<string>>, bool>
{
public async Task<bool> Handle(ActivateEventArgs<Wallet<string>> args,
+3
View File
@@ -1,3 +1,6 @@
namespace Wallet;
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;
public class WalletInitializer(IHostEnvironment environment,
public class WalletCollectionInitializer(IHostEnvironment environment,
IComponentFactory componentFactory,
IWalletHostCollection Wallets) :
IInitialization
+28 -8
View File
@@ -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<SecurityKey> secureKeyStore,
IWalletStoreFactory walletStoreFactory,
IWritableConfiguration<WalletConfiguration> configuration,
IHostEnvironment environment,
IImageWriter imageWriter) :
IWalletFactory
{
public IComponentHost? Create(string key)
public async Task<bool> Create(string name,
string password,
IImageDescriptor thumbnail)
{
if (componentFactory.Create<WalletComponent, WalletConfiguration>($"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;
}
}
}
+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;
public class WalletStorageFactory(IDecoratorService<WalletConnection> connection,
public class WalletStoreFactory(IDecoratorService<WalletConnection> connection,
IHostEnvironment environment,
IServiceProvider provider) :
IWalletStorageFactory
IWalletStoreFactory
{
public async Task<bool> Create(string name,
SecurityKey key)