Skip to main content

Mountain/RPC/CocoonService/FileSystem/
ReadFile.rs

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