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