Skip to main content

Mountain/RPC/CocoonService/GenericRequest/FileSystem/
StatUri.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 Uri = Params
9		.get("uri")
10		.and_then(|V| V.as_str())
11		.or_else(|| Params.as_str())
12		.unwrap_or("")
13		.replace("file://", "");
14
15	match tokio::fs::metadata(&Uri).await {
16		Ok(Meta) => {
17			let Mtime = Meta
18				.modified()
19				.ok()
20				.and_then(|T| T.duration_since(UNIX_EPOCH).ok())
21				.map(|D| D.as_millis() as u64)
22				.unwrap_or(0);
23
24			super::OkResponse(
25				RequestId,
26				&json!({
27					"type": if Meta.is_dir() { 2 } else { 1 },
28					"is_file": Meta.is_file(),
29					"is_directory": Meta.is_dir(),
30					"size": Meta.len(),
31					"mtime": Mtime,
32				}),
33			)
34		},
35
36		Err(Error) => super::ErrResponse(RequestId, -32000, format!("stat: {}", Error)),
37	}
38}