From c8fc67104b9476fb97ce2118aab19cbde8646b99 Mon Sep 17 00:00:00 2001 From: Muse Date: Fri, 13 Nov 2026 22:58:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(stage5):=20Rust=20read=5Ffile=5Fbase64=20?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E8=BE=85=E5=8A=A9=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/commands.rs | 23 +++++++++++++++++++++++ src-tauri/src/lib.rs | 1 + 2 files changed, 24 insertions(+) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 8785558..187d433 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -371,3 +371,26 @@ pub fn db_recovery_delete(db: State<'_, AppDb>, doc_key: String) -> Result<(), S let conn = lock_db(&db)?; storage::recovery_delete(&conn, &doc_key) } + +// ————————————————— 阶段 5:导出辅助 ————————————————— + +fn base64_encode(data: &[u8]) -> String { + const TABLE: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut out = String::with_capacity((data.len() + 2) / 3 * 4); + for chunk in data.chunks(3) { + let b = [chunk[0], *chunk.get(1).unwrap_or(&0), *chunk.get(2).unwrap_or(&0)]; + let n = ((b[0] as u32) << 16) | ((b[1] as u32) << 8) | b[2] as u32; + out.push(TABLE[(n >> 18) as usize & 63] as char); + out.push(TABLE[(n >> 12) as usize & 63] as char); + out.push(if chunk.len() > 1 { TABLE[(n >> 6) as usize & 63] as char } else { '=' }); + out.push(if chunk.len() > 2 { TABLE[n as usize & 63] as char } else { '=' }); + } + out +} + +/// 读任意文件并以 base64 返回(导出图片内嵌、DOCX 导入共用)。 +#[tauri::command] +pub fn read_file_base64(path: String) -> Result { + let bytes = fs::read(&path).map_err(|e| format!("读取文件失败:{e}"))?; + Ok(base64_encode(&bytes)) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 5a3dd28..a24bdbc 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -42,6 +42,7 @@ pub fn run() { commands::db_recovery_list, commands::db_recovery_save, commands::db_recovery_delete, + commands::read_file_base64, ]) .run(tauri::generate_context!()) .expect("error while running new-markdown");