Skip to main content

Mountain/RPC/CocoonService/FileSystem/
ReadFile.rs

1
2//! Read a file from disk and return its bytes (always tagged `utf-8` -
3//! the encoding negotiation lives in Cocoon).
4
5use tonic::{Response, Status};
6
7use crate::{
8	RPC::CocoonService::CocoonServiceImpl,
9	Vine::Generated::{ReadFileRequest, ReadFileResponse},
10	dev_log,
11};
12
13pub async fn Fn(_Service:&CocoonServiceImpl, Request:ReadFileRequest) -> Result<Response<ReadFileResponse>, Status> {
14	let Path = CocoonServiceImpl::UriToPath(Request.uri.as_ref())
15		.ok_or_else(|| Status::invalid_argument("read_file: missing or empty URI"))?;
16
17	dev_log!("cocoon", "[CocoonService] Reading file: {:?}", Path);
18
19	let Content = tokio::fs::read(&Path).await.map_err(|Error| {
20		dev_log!("cocoon", "warn: [CocoonService] read_file failed for {:?}: {}", Path, Error);
21		Status::not_found(format!("read_file: {}: {}", Path.display(), Error))
22	})?;
23
24	Ok(Response::new(ReadFileResponse {
25		content:Content,
26		encoding:"utf-8".to_string(),
27	}))
28}