Skip to main content

Mountain/RPC/CocoonService/GenericRequest/FileSystem/
Stat.rs

1use std::time::UNIX_EPOCH;
2
3use serde_json::{Value, json};
4use tonic::Response;
5use ::Vine::Generated::GenericResponse;
6
7pub async fn Fn(RequestId:u64, Params:Value) -> Response<GenericResponse> {
8	let Path = Params
9		.as_str()
10		.or_else(|| Params.get("path").and_then(|V| V.as_str()))
11		.unwrap_or("");
12
13	match tokio::fs::metadata(Path).await {
14		Ok(Meta) => {
15			let Mtime = Meta
16				.modified()
17				.ok()
18				.and_then(|T| T.duration_since(UNIX_EPOCH).ok())
19				.map(|D| D.as_millis() as u64)
20				.unwrap_or(0);
21
22			super::OkResponse(
23				RequestId,
24				&json!({
25					"type": if Meta.is_dir() { 2 } else { 1 },
26					"is_file": Meta.is_file(),
27					"is_directory": Meta.is_dir(),
28					"size": Meta.len(),
29					"mtime": Mtime,
30				}),
31			)
32		},
33
34		Err(Error) => super::ErrResponse(RequestId, -32000, format!("fs.stat: {}", Error)),
35	}
36}