Skip to main content

Mountain/RPC/CocoonService/FileSystem/
CopyFile.rs

1
2//! Copy a file, creating any missing target parents first.
3
4use tonic::{Response, Status};
5
6use crate::{
7	RPC::CocoonService::CocoonServiceImpl,
8	Vine::Generated::{CopyFileRequest, Empty},
9	dev_log,
10};
11
12pub async fn Fn(_Service:&CocoonServiceImpl, Request:CopyFileRequest) -> Result<Response<Empty>, Status> {
13	let SourcePath = CocoonServiceImpl::UriToPath(Request.source.as_ref())
14		.ok_or_else(|| Status::invalid_argument("copy_file: missing source URI"))?;
15
16	let DestinationPath = CocoonServiceImpl::UriToPath(Request.target.as_ref())
17		.ok_or_else(|| Status::invalid_argument("copy_file: missing target URI"))?;
18
19	dev_log!("cocoon", "[CocoonService] copy_file: {:?} → {:?}", SourcePath, DestinationPath);
20
21	if let Some(Parent) = DestinationPath.parent() {
22		if !Parent.as_os_str().is_empty() {
23			tokio::fs::create_dir_all(Parent)
24				.await
25				.map_err(|Error| Status::internal(format!("copy_file: create_dir_all failed: {}", Error)))?;
26		}
27	}
28
29	tokio::fs::copy(&SourcePath, &DestinationPath)
30		.await
31		.map_err(|Error| Status::internal(format!("copy_file: {}: {}", SourcePath.display(), Error)))?;
32
33	Ok(Response::new(Empty {}))
34}