Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/RPC/CocoonService/FileSystem/
CopyFile.rs

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