feat(stage5): Rust read_file_base64 导出辅助命令

This commit is contained in:
Muse
2026-11-13 22:58:35 +08:00
parent e5ad58eb93
commit c8fc67104b
2 changed files with 24 additions and 0 deletions

View File

@@ -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<String, String> {
let bytes = fs::read(&path).map_err(|e| format!("读取文件失败:{e}"))?;
Ok(base64_encode(&bytes))
}

View File

@@ -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");