Files
Akari.Prototype/Akari.Prototype.Client/MainWindow.axaml.cs
2021-06-08 22:31:13 +02:00

135 lines
4.2 KiB
C#

using System;
using System.IO;
using System.Threading.Tasks;
using Akari.Prototype.Shared.Protos;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Google.Protobuf;
using Grpc.Net.Client;
using MessageBox.Avalonia;
namespace Akari.Prototype.Client
{
public partial class MainWindow : Window, IDisposable
{
public const string Hostname = "https://localhost:5001";
private readonly GrpcChannel _channel;
private string? _file = null;
public MainWindow()
{
InitializeComponent();
_channel = GrpcChannel.ForAddress(Hostname);
}
private async void OnPickFile(object sender, RoutedEventArgs e)
{
var mode = GetMode();
var dialog = new OpenFileDialog()
{
AllowMultiple = false,
Title = $"Pick a file to {mode}"
};
if (await dialog.ShowAsync(this) is string[] { Length: 1 } res)
{
_file = res[0];
TxtFile.Text = Path.GetFileName(_file);
}
}
private async void OnSubmit(object sender, RoutedEventArgs e)
{
if (_file is null)
{
return;
}
if (string.IsNullOrWhiteSpace(BoxName.Text))
{
await MessageBoxManager.GetMessageBoxStandardWindow("Error", $"Invalid application name", icon: MessageBox.Avalonia.Enums.Icon.Error).Show();
return;
}
if (string.IsNullOrWhiteSpace(BoxToken.Text))
{
await MessageBoxManager.GetMessageBoxStandardWindow("Error", $"Invalid token", icon: MessageBox.Avalonia.Enums.Icon.Error).Show();
return;
}
var client = new AkariApi.AkariApiClient(_channel);
// Decrypt
if (GetMode() == Mode.Decrypt)
{
var response = await client.DecryptAsync(new DecryptRequest()
{
Application = BoxName.Text,
Token = BoxToken.Text,
Encrypted = ByteString.CopyFrom(File.ReadAllBytes(_file))
});
if (response.ResponseCase == DecryptResponse.ResponseOneofCase.ErrorMessage)
{
await MessageBoxManager.GetMessageBoxStandardWindow("Error", $"Error during decryption, server response: {response.ErrorMessage}", icon: MessageBox.Avalonia.Enums.Icon.Error).Show();
}
else
{
string outPath = Path.ChangeExtension(_file, null);
File.WriteAllBytes(outPath, response.Plain.ToByteArray());
await MessageBoxManager.GetMessageBoxStandardWindow("Success", $"Saved decrypted file to {outPath}\n").Show();
}
}
// Encrypt
else
{
var response = await client.EncryptAsync(new EncryptRequest()
{
Application = BoxName.Text,
Token = BoxToken.Text,
Plain = ByteString.CopyFrom(File.ReadAllBytes(_file))
});
if (response.ResponseCase == EncryptResponse.ResponseOneofCase.ErrorMessage)
{
await MessageBoxManager.GetMessageBoxStandardWindow("Error", $"Error during encryption, server response: {response.ErrorMessage}", icon: MessageBox.Avalonia.Enums.Icon.Error).Show();
}
else
{
string outPath = $"{_file}.enc";
File.WriteAllBytes(outPath, response.Encrypted.ToByteArray());
await MessageBoxManager.GetMessageBoxStandardWindow("Success", $"Saved encrypted file to {outPath}").Show();
}
}
}
private Mode GetMode()
{
if (CbxMode.SelectedItem is ComboBoxItem { Content: string mode })
{
return Enum.Parse<Mode>(mode);
}
throw new Exception("Invalid mode in CbxMode");
}
public void Dispose()
{
_channel.Dispose();
}
}
}