Skip to main content

Mountain/RPC/CocoonService/FileSystem/
ReadFile.rs

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