-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic File Key Store Import/Export feature (as zip)
- Loading branch information
Showing
9 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
KeyManager.Library.KeyStore.File.UI/Domain/FileKeyStoreImportExportControlViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using CommunityToolkit.Mvvm.Input; | ||
using Leosac.KeyManager.Library.UI.Domain; | ||
using Leosac.WpfApp; | ||
using log4net; | ||
using Microsoft.Win32; | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
|
||
namespace Leosac.KeyManager.Library.KeyStore.File.UI.Domain | ||
{ | ||
public class FileKeyStoreImportExportControlViewModel : KeyStoreAdditionalControlViewModel | ||
{ | ||
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType); | ||
|
||
public FileKeyStoreImportExportControlViewModel() | ||
{ | ||
ImportCommand = new RelayCommand(Import); | ||
|
||
ExportCommand = new RelayCommand(Export); | ||
} | ||
|
||
public RelayCommand ImportCommand { get; } | ||
|
||
public RelayCommand ExportCommand { get; } | ||
|
||
private void Import() | ||
{ | ||
var ofd = new OpenFileDialog(); | ||
ofd.Filter = "File Key Store Archive (*.zip)|*.zip"; | ||
ofd.CheckFileExists = true; | ||
if (KeyStore is FileKeyStore ks && ofd.ShowDialog() == true) | ||
{ | ||
FileKeyStoreArchive.Import(ofd.FileName, ks); | ||
} | ||
} | ||
|
||
private void Export() | ||
{ | ||
var sfd = new SaveFileDialog(); | ||
sfd.Filter = "File Key Store Archive (*.zip)|*.zip"; | ||
if (KeyStore is FileKeyStore ks && sfd.ShowDialog() == true) | ||
{ | ||
try | ||
{ | ||
FileKeyStoreArchive.Export(sfd.FileName, ks); | ||
} | ||
catch (Exception ex) | ||
{ | ||
log.Error("Export of the File Key Store as an archive failed.", ex); | ||
SnackbarHelper.EnqueueError(SnackbarMessageQueue, ex); | ||
} | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
KeyManager.Library.KeyStore.File.UI/FileKeyStoreImportExportControl.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<UserControl x:Class="Leosac.KeyManager.Library.KeyStore.File.UI.FileKeyStoreImportExportControl" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:domain="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI.Domain" | ||
xmlns:libui="clr-namespace:Leosac.KeyManager.Library.UI;assembly=KeyManager.Library.UI" | ||
xmlns:local="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI" | ||
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" | ||
xmlns:properties="clr-namespace:Leosac.KeyManager.Library.KeyStore.File.UI.Properties" | ||
xmlns:wpfappctrls="clr-namespace:Leosac.WpfApp.Controls;assembly=WpfApp" | ||
mc:Ignorable="d" | ||
d:DataContext="{d:DesignInstance domain:FileKeyStoreImportExportControlViewModel}" | ||
d:DesignHeight="450" d:DesignWidth="800"> | ||
<Grid> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition /> | ||
<ColumnDefinition /> | ||
</Grid.ColumnDefinitions> | ||
<Button Grid.Column="0" Margin="5" Height="128" Command="{Binding ImportCommand}"> | ||
<Button.Content> | ||
<WrapPanel> | ||
<materialDesign:PackIcon Kind="Import" Width="64" Height="64" /> | ||
<TextBlock Text="{x:Static properties:Resources.Import}" VerticalAlignment="Center" Margin="3" /> | ||
</WrapPanel> | ||
</Button.Content> | ||
</Button> | ||
<Button Grid.Column="1" Margin="5" Height="128" Command="{Binding ExportCommand}"> | ||
<Button.Content> | ||
<WrapPanel> | ||
<materialDesign:PackIcon Kind="Export" Width="64" Height="64" /> | ||
<TextBlock Text="{x:Static properties:Resources.Export}" VerticalAlignment="Center" Margin="3" /> | ||
</WrapPanel> | ||
</Button.Content> | ||
</Button> | ||
</Grid> | ||
</UserControl> |
18 changes: 18 additions & 0 deletions
18
KeyManager.Library.KeyStore.File.UI/FileKeyStoreImportExportControl.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Leosac.KeyManager.Library.KeyStore.File.UI.Domain; | ||
using System.Windows.Controls; | ||
|
||
namespace Leosac.KeyManager.Library.KeyStore.File.UI | ||
{ | ||
/// <summary> | ||
/// Interaction logic for FileKeyStoreImportExportControl.xaml | ||
/// </summary> | ||
public partial class FileKeyStoreImportExportControl : UserControl | ||
{ | ||
public FileKeyStoreImportExportControl() | ||
{ | ||
InitializeComponent(); | ||
|
||
DataContext = new FileKeyStoreImportExportControlViewModel(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
KeyManager.Library.KeyStore.File.UI/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.IO.Compression; | ||
|
||
namespace Leosac.KeyManager.Library.KeyStore.File | ||
{ | ||
public class FileKeyStoreArchive | ||
{ | ||
public static void Import(string fileName, FileKeyStore keyStore) | ||
{ | ||
if (System.IO.File.Exists(fileName)) | ||
{ | ||
ZipFile.ExtractToDirectory(fileName, keyStore.GetFileProperties().Fullpath, true); | ||
} | ||
} | ||
|
||
public static void Export(string fileName, FileKeyStore keyStore) | ||
{ | ||
if (System.IO.File.Exists(fileName)) | ||
{ | ||
System.IO.File.Delete(fileName); | ||
} | ||
|
||
ZipFile.CreateFromDirectory(keyStore.GetFileProperties().Fullpath, fileName); | ||
} | ||
} | ||
} |