Skip to main content

Mountain/RPC/CocoonService/FileSystem/
DeleteFile.rs

1
2//! Remove a file or recursively remove a directory.
3
4use tonic::{Response, Status};
5
6use crate::{
7	RPC::CocoonService::CocoonServiceImpl,
8	Vine::Generated::{DeleteFileRequest, Empty},
9	dev_log,
10};
11
12pub async fn Fn(_Service:&CocoonServiceImpl, Request:DeleteFileRequest) -> Result<Response<Empty>, Status> {
13	let Path = CocoonServiceImpl::UriToPath(Request.uri.as_ref())
14		.ok_or_else(|| Status::invalid_argument("delete_file: missing URI"))?;
15
16	dev_log!("cocoon", "[CocoonService] delete_file: {:?}", Path);
17
18	if Path.is_dir() {
19		tokio::fs::remove_dir_all(&Path).await
20	} else {
21		tokio::fs::remove_file(&Path).await
22	}
23	.map_err(|Error| Status::internal(format!("delete_file: {}: {}", Path.display(), Error)))?;
24
25	Ok(Response::new(Empty {}))
26}