Skip to main content

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

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