Skip to main content

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

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