Skip to main content

Mountain/RPC/CocoonService/FileSystem/
CopyFile.rs

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