Skip to main content

Mountain/IPC/WindServiceHandlers/FileSystem/Managed/
FileWriteBinary.rs

1//! Wire method `file:writeBinary`. Active in dispatch. Mirrors the read
2//! path: RunTime `FileSystemWriter` does the actual byte write with create
3//! + overwrite flags on.
4
5use std::{path::PathBuf, sync::Arc};
6
7use CommonLibrary::{
8	Environment::Requires::Requires,
9	Error::CommonError::CommonError,
10	FileSystem::FileSystemWriter::FileSystemWriter,
11};
12use serde_json::Value;
13
14use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
15
16pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
17	let path = Arguments
18		.get(0)
19		.ok_or("Missing file path".to_string())?
20		.as_str()
21		.ok_or("File path must be a string".to_string())?;
22
23	let content = Arguments
24		.get(1)
25		.ok_or("Missing file content".to_string())?
26		.as_str()
27		.ok_or("File content must be a string".to_string())?;
28
29	let content_bytes = content.as_bytes().to_vec();
30
31	let content_len = content_bytes.len();
32
33	let provider:Arc<dyn FileSystemWriter> = RunTime.Environment.Require();
34
35	provider
36		.WriteFile(&PathBuf::from(path), content_bytes.clone(), true, true)
37		.await
38		.map_err(|e:CommonError| format!("Failed to write binary file: {}", e))?;
39
40	dev_log!("vfs-verbose", "writeBinary: {} ({} bytes)", path, content_len);
41
42	Ok(Value::Null)
43}