Mountain/RPC/CocoonService/FileSystem/
WriteFile.rs1use tonic::{Response, Status};
4use ::Vine::Generated::{Empty, WriteFileRequest};
5
6use crate::{RPC::CocoonService::CocoonServiceImpl, dev_log};
7
8pub async fn Fn(_Service:&CocoonServiceImpl, Request:WriteFileRequest) -> Result<Response<Empty>, Status> {
9 let Path = CocoonServiceImpl::UriToPath(Request.uri.as_ref())
10 .ok_or_else(|| Status::invalid_argument("write_file: missing or empty URI"))?;
11
12 dev_log!(
13 "cocoon",
14 "[CocoonService] Writing file: {:?} ({} bytes)",
15 Path,
16 Request.content.len()
17 );
18
19 if let Some(Parent) = Path.parent() {
20 if !Parent.as_os_str().is_empty() {
21 tokio::fs::create_dir_all(Parent)
22 .await
23 .map_err(|Error| Status::internal(format!("write_file: create_dir_all {:?}: {}", Parent, Error)))?;
24 }
25 }
26
27 tokio::fs::write(&Path, &Request.content).await.map_err(|Error| {
28 dev_log!("cocoon", "warn: [CocoonService] write_file failed for {:?}: {}", Path, Error);
29
30 Status::internal(format!("write_file: {}: {}", Path.display(), Error))
31 })?;
32
33 Ok(Response::new(Empty {}))
34}