From 51c38cea41a8b98392a3da9ceffb8384e81db1e6 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Wed, 21 Aug 2024 00:46:21 +0800 Subject: [PATCH] feat: works --- .gitignore | 1 + src-tauri/src/main.rs | 42 ++++++++++++++++++++++++++--------- src/index.html | 51 +++++-------------------------------------- src/main.js | 16 +++++++++----- src/styles.css | 44 ++++++++++++++++++++++--------------- 5 files changed, 76 insertions(+), 78 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ec551b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.tinyenc diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 478a3d5..89b14d4 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -2,7 +2,7 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use aes_gcm_stream::{Aes256GcmStreamDecryptor, Aes256GcmStreamEncryptor}; -use rust_util::{opt_result, simple_error, XResult}; +use rust_util::{failure_and_exit, opt_result, simple_error, XResult}; use std::fs; const ALGORITHM_AES_256_GCM: &str = "aes-256-gcm"; @@ -51,8 +51,9 @@ impl EncryptArgs { fn read(&self) -> XResult> { let file_content = opt_result!(fs::read(&self.file_name), "Read from file: {} failed: {}", &self.file_name); let mut decryptor = Aes256GcmStreamDecryptor::new(self.key.clone(), &self.nonce); - decryptor.update(&file_content); - let plaintext = opt_result!(decryptor.finalize(), "Decrypt input file failed: {}"); + let mut plaintext = vec![]; + plaintext.extend_from_slice(&decryptor.update(&file_content)); + plaintext.extend_from_slice(&opt_result!(decryptor.finalize(), "Decrypt input file failed: {}")); Ok(plaintext) } @@ -63,26 +64,46 @@ impl EncryptArgs { fn write(&self, content: &[u8]) -> XResult<()> { let mut encryptor = Aes256GcmStreamEncryptor::new(self.key.clone(), &self.nonce); - encryptor.update(content); - let (mut ciphertext, tag) = encryptor.finalize(); + let mut ciphertext = vec![]; + ciphertext.extend_from_slice(&encryptor.update(content)); + let (final_block, tag) = encryptor.finalize(); + ciphertext.extend_from_slice(&final_block); ciphertext.extend_from_slice(&tag); opt_result!(fs::write(&self.file_name, &ciphertext), "Write to file: {} failed: {}", &self.file_name); Ok(()) } } +fn inner_read_content() -> XResult { + let encrypt_args = EncryptArgs::new()?; + encrypt_args.read_to_string() +} + +fn inner_write_content(content: &str) -> XResult<()> { + let encrypt_args = EncryptArgs::new()?; + encrypt_args.write_string(content) +} + #[tauri::command] fn read_content() -> String { - // TODO ... - let args: Vec = std::env::args().collect(); - args.join(", ") + match inner_read_content() { + Ok(content) => content, + Err(e) => failure_and_exit!("Read content failed: {}", e) + } } #[tauri::command] fn save_content(content: &str) -> String { - // TOD save.... - format!("Hello, {}! You've been greeted from Rust!", content) + match inner_write_content(content) { + Ok(_) => std::process::exit(0), + Err(e) => failure_and_exit!("Write content failed: {}", e), + } +} + +#[tauri::command] +fn do_exit() -> String { + std::process::exit(-1); } fn main() { @@ -90,6 +111,7 @@ fn main() { .invoke_handler(tauri::generate_handler![ read_content, save_content, + do_exit, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src/index.html b/src/index.html index ebfb718..91e79d6 100644 --- a/src/index.html +++ b/src/index.html @@ -10,52 +10,13 @@
- - - - - - - - - - - - - -
header
- -
- -
- foot -
- +

-

diff --git a/src/main.js b/src/main.js index cd68c66..3c45cf5 100644 --- a/src/main.js +++ b/src/main.js @@ -1,23 +1,29 @@ const { invoke } = window.__TAURI__.tauri; let contentEl; -let greetMsgEl; +let messageEl; async function init_content() { contentEl.value = await invoke("read_content"); } async function save_content() { - contentEl.value = "test"; - //greetMsgEl.textContent = await invoke("save_content", { name: greetInputEl.value }) + ' >>>';// + await invoke("read_content"); + await invoke("save_content", { content: contentEl.value }); +} + +async function do_exit() { + contentEl.value = await invoke("do_exit"); } window.addEventListener("DOMContentLoaded", () => { contentEl = document.querySelector("#content"); - contentEl.value = "aaaa"; - //greetMsgEl = document.querySelector("#greet-msg"); + messageEl = document.querySelector("#message"); init_content(); document.querySelector("#save").addEventListener("click", (e) => { save_content(); }); + document.querySelector("#exit").addEventListener("click", (e) => { + do_exit(); + }); + messageEl.innerText = 'init success' }); diff --git a/src/styles.css b/src/styles.css index 7257a59..b04c8b6 100644 --- a/src/styles.css +++ b/src/styles.css @@ -17,24 +17,17 @@ -webkit-text-size-adjust: 100%; } +body { + margin: 3vh 1vw; +} + .container { - margin: 0; - padding-top: 10vh; display: flex; flex-direction: column; justify-content: center; text-align: center; -} - -.logo { - height: 6em; - padding: 1.5em; - will-change: filter; - transition: 0.75s; -} - -.logo.tauri:hover { - filter: drop-shadow(0 0 2em #24c8db); + width: 98vw; + height: 94vh; } .row { @@ -54,10 +47,23 @@ a:hover { h1 { text-align: center; + margin-top: 0.1vh; + margin-bottom: 0.1vh; +} + +textarea { + border-radius: 8px; + border: 1px solid transparent; + padding: 0; + font-size: 1em; + font-weight: 500; + font-family: inherit; + color: #0f0f0f; + background-color: #ffffff; + transition: border-color 0.25s; + box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2); } -input, -textarea, button { border-radius: 8px; border: 1px solid transparent; @@ -78,19 +84,21 @@ button { button:hover { border-color: #396cd8; } + button:active { border-color: #396cd8; background-color: #e8e8e8; } -input, textarea, button { outline: none; } -#greet-input { - margin-right: 5px; +#content { + margin-left: 0.1vw; + margin-right: 0.1vw; + height: 80vh; } @media (prefers-color-scheme: dark) {