agent_hub_runtime/commands/preview_native/
export.rs1use super::*;
2use serde::Deserialize;
3#[derive(Deserialize)]
4pub struct Expected {
5 id: String,
6 revision: String,
7}
8#[tauri::command]
9pub async fn preview_native_export(
10 webview: tauri::Webview,
11 name: String,
12 content: String,
13 binary: bool,
14 expected: Expected,
15) -> Result<bool, AppError> {
16 let view = target(&webview, &expected.id)?;
17 if content.len() > 24 * 1024 * 1024 || name.len() > 200 || name.contains(['/', '\\']) {
18 return Err(AppError::invalid_input(
19 "Export exceeds the supported limit",
20 ));
21 }
22 let Some(file) = rfd::AsyncFileDialog::new()
23 .set_file_name(&name)
24 .save_file()
25 .await
26 else {
27 return Ok(false);
28 };
29 let _job = webview.state::<NativeViews>().lock(&expected.id).await?;
30 control::verify(&view, &expected.revision).await?;
31 let path = file.path().to_path_buf();
32 tauri::async_runtime::spawn_blocking(move || {
33 use base64::Engine;
34 let bytes = if binary {
35 base64::engine::general_purpose::STANDARD
36 .decode(content)
37 .map_err(AppError::io)?
38 } else {
39 content.into_bytes()
40 };
41 std::fs::write(path, bytes).map_err(AppError::io)?;
42 Ok(true)
43 })
44 .await?
45}