Refactored clipboard handling

This commit is contained in:
Thorsten Sommer 2024-11-05 20:41:42 +01:00
parent 6044550d92
commit e0a2906655
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 65 additions and 62 deletions

60
runtime/src/clipboard.rs Normal file
View File

@ -0,0 +1,60 @@
use arboard::Clipboard;
use log::{debug, error};
use rocket::post;
use rocket::serde::json::Json;
use serde::Serialize;
use crate::api_token::APIToken;
use crate::encryption::{EncryptedText, ENCRYPTION};
#[post("/clipboard/set", data = "<encrypted_text>")]
pub fn set_clipboard(_token: APIToken, encrypted_text: EncryptedText) -> Json<SetClipboardResponse> {
// Decrypt this text first:
let decrypted_text = match ENCRYPTION.decrypt(&encrypted_text) {
Ok(text) => text,
Err(e) => {
error!(Source = "Clipboard"; "Failed to decrypt the text: {e}.");
return Json(SetClipboardResponse {
success: false,
issue: e,
})
},
};
let clipboard_result = Clipboard::new();
let mut clipboard = match clipboard_result {
Ok(clipboard) => clipboard,
Err(e) => {
error!(Source = "Clipboard"; "Failed to get the clipboard instance: {e}.");
return Json(SetClipboardResponse {
success: false,
issue: e.to_string(),
})
},
};
let set_text_result = clipboard.set_text(decrypted_text);
match set_text_result {
Ok(_) => {
debug!(Source = "Clipboard"; "Text was set to the clipboard successfully.");
Json(SetClipboardResponse {
success: true,
issue: String::from(""),
})
},
Err(e) => {
error!(Source = "Clipboard"; "Failed to set text to the clipboard: {e}.");
Json(SetClipboardResponse {
success: false,
issue: e.to_string(),
})
},
}
}
#[derive(Serialize)]
pub struct SetClipboardResponse {
success: bool,
issue: String,
}

View File

@ -5,4 +5,5 @@ pub mod dotnet;
pub mod network; pub mod network;
pub mod api_token; pub mod api_token;
pub mod app_window; pub mod app_window;
pub mod secret; pub mod secret;
pub mod clipboard;

View File

@ -7,19 +7,14 @@ extern crate core;
use std::collections::HashSet; use std::collections::HashSet;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use arboard::Clipboard; use log::{info, warn};
use serde::Serialize;
use log::{debug, error, info, warn};
use rcgen::generate_simple_self_signed; use rcgen::generate_simple_self_signed;
use rocket::figment::Figment; use rocket::figment::Figment;
use rocket::{post, routes}; use rocket::routes;
use rocket::config::{Shutdown}; use rocket::config::{Shutdown};
use rocket::serde::json::Json;
use sha2::{Sha256, Digest}; use sha2::{Sha256, Digest};
use mindwork_ai_studio::api_token::APIToken;
use mindwork_ai_studio::app_window::start_tauri; use mindwork_ai_studio::app_window::start_tauri;
use mindwork_ai_studio::dotnet::start_dotnet_server; use mindwork_ai_studio::dotnet::start_dotnet_server;
use mindwork_ai_studio::encryption::{EncryptedText, ENCRYPTION};
use mindwork_ai_studio::environment::is_dev; use mindwork_ai_studio::environment::is_dev;
use mindwork_ai_studio::log::init_logging; use mindwork_ai_studio::log::init_logging;
use mindwork_ai_studio::network::get_available_port; use mindwork_ai_studio::network::get_available_port;
@ -143,7 +138,7 @@ async fn main() {
.mount("/", routes![ .mount("/", routes![
mindwork_ai_studio::dotnet::dotnet_port, mindwork_ai_studio::dotnet::dotnet_port,
mindwork_ai_studio::dotnet::dotnet_ready, mindwork_ai_studio::dotnet::dotnet_ready,
set_clipboard, mindwork_ai_studio::clipboard::set_clipboard,
mindwork_ai_studio::app_window::check_for_update, mindwork_ai_studio::app_window::check_for_update,
mindwork_ai_studio::app_window::install_update, mindwork_ai_studio::app_window::install_update,
mindwork_ai_studio::secret::get_secret, mindwork_ai_studio::secret::get_secret,
@ -159,57 +154,4 @@ async fn main() {
info!("Secret password for the IPC channel was generated successfully."); info!("Secret password for the IPC channel was generated successfully.");
start_dotnet_server(*API_SERVER_PORT, certificate_fingerprint); start_dotnet_server(*API_SERVER_PORT, certificate_fingerprint);
start_tauri(); start_tauri();
}
#[post("/clipboard/set", data = "<encrypted_text>")]
fn set_clipboard(_token: APIToken, encrypted_text: EncryptedText) -> Json<SetClipboardResponse> {
// Decrypt this text first:
let decrypted_text = match ENCRYPTION.decrypt(&encrypted_text) {
Ok(text) => text,
Err(e) => {
error!(Source = "Clipboard"; "Failed to decrypt the text: {e}.");
return Json(SetClipboardResponse {
success: false,
issue: e,
})
},
};
let clipboard_result = Clipboard::new();
let mut clipboard = match clipboard_result {
Ok(clipboard) => clipboard,
Err(e) => {
error!(Source = "Clipboard"; "Failed to get the clipboard instance: {e}.");
return Json(SetClipboardResponse {
success: false,
issue: e.to_string(),
})
},
};
let set_text_result = clipboard.set_text(decrypted_text);
match set_text_result {
Ok(_) => {
debug!(Source = "Clipboard"; "Text was set to the clipboard successfully.");
Json(SetClipboardResponse {
success: true,
issue: String::from(""),
})
},
Err(e) => {
error!(Source = "Clipboard"; "Failed to set text to the clipboard: {e}.");
Json(SetClipboardResponse {
success: false,
issue: e.to_string(),
})
},
}
}
#[derive(Serialize)]
struct SetClipboardResponse {
success: bool,
issue: String,
} }