2025-12-17 16:40:34 -07:00
|
|
|
use axum::{
|
|
|
|
|
Extension, Json,
|
|
|
|
|
extract::{Path, State},
|
|
|
|
|
};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use serde_json::Value;
|
|
|
|
|
use unshell_lib::debug;
|
|
|
|
|
|
2025-12-20 18:19:08 -07:00
|
|
|
use crate::{Server, auth::structs::CurrentUser};
|
2025-12-17 16:40:34 -07:00
|
|
|
|
2025-12-17 17:43:08 -07:00
|
|
|
pub trait Tree {
|
|
|
|
|
fn is_folder() -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
2025-12-17 16:40:34 -07:00
|
|
|
|
2025-12-17 17:43:08 -07:00
|
|
|
fn get_children_string(&self) -> Vec<String> {
|
|
|
|
|
Vec::new()
|
|
|
|
|
}
|
2025-12-17 16:40:34 -07:00
|
|
|
|
2025-12-17 17:43:08 -07:00
|
|
|
fn select_child(&self, child: &str) -> Result<Tree2Repr, String>;
|
2025-12-17 16:40:34 -07:00
|
|
|
|
2025-12-17 17:43:08 -07:00
|
|
|
fn get_value(&self) -> String {
|
|
|
|
|
"DEFAULT".into()
|
|
|
|
|
}
|
2025-12-17 16:40:34 -07:00
|
|
|
|
2025-12-17 17:43:08 -07:00
|
|
|
fn get_path(&self, elements: &mut Vec<&str>) -> Result<Tree2Repr, String> {
|
|
|
|
|
if elements.is_empty() {
|
|
|
|
|
return if Self::is_folder() {
|
|
|
|
|
Ok(Tree2Repr::Folder(self.get_children_string()))
|
2025-12-17 16:40:34 -07:00
|
|
|
} else {
|
2025-12-17 17:43:08 -07:00
|
|
|
Ok(Tree2Repr::File(self.get_value()))
|
|
|
|
|
};
|
2025-12-17 16:40:34 -07:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 17:43:08 -07:00
|
|
|
let child = elements.remove(0);
|
|
|
|
|
|
|
|
|
|
if Self::is_folder() {
|
|
|
|
|
self.select_child(child)
|
|
|
|
|
} else {
|
|
|
|
|
Err("This is a folder, not a file".into())
|
2025-12-17 16:40:34 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get(&self, path: &str) -> Result<Tree2Repr, String> {
|
2025-12-17 17:43:08 -07:00
|
|
|
let mut path = if path.is_empty() {
|
2025-12-17 16:40:34 -07:00
|
|
|
Vec::new()
|
|
|
|
|
} else {
|
|
|
|
|
path.split("/").collect::<Vec<&str>>()
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-17 17:43:08 -07:00
|
|
|
self.get_path(&mut path)
|
2025-12-17 16:40:34 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 17:43:08 -07:00
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
|
|
|
|
pub enum Tree2Repr {
|
|
|
|
|
File(String),
|
|
|
|
|
Folder(Vec<String>),
|
2025-12-17 16:40:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Server {
|
|
|
|
|
pub async fn get_tree2(
|
|
|
|
|
State(server): State<Server>,
|
|
|
|
|
Path(path): Path<String>,
|
|
|
|
|
Extension(_): Extension<CurrentUser>,
|
|
|
|
|
) -> Json<Value> {
|
|
|
|
|
debug!("GET /api/interface/{}", path);
|
|
|
|
|
|
|
|
|
|
let result = (|| -> Result<_, String> {
|
2025-12-17 17:43:08 -07:00
|
|
|
let interface = server.get(&path)?;
|
2025-12-17 16:40:34 -07:00
|
|
|
|
|
|
|
|
Ok(interface)
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
Json(serde_json::to_value(result).unwrap())
|
|
|
|
|
}
|
|
|
|
|
pub async fn get_tree2_root(
|
|
|
|
|
State(server): State<Server>,
|
|
|
|
|
Extension(extension): Extension<CurrentUser>,
|
|
|
|
|
) -> Json<Value> {
|
|
|
|
|
Self::get_tree2(State(server), Path("".into()), Extension(extension)).await
|
|
|
|
|
}
|
|
|
|
|
}
|