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