|
@@ -1,8 +1,5 @@
|
|
|
-use rocket::{fs::FileServer, get, launch, routes, serde::json::{json, Value}};
|
|
|
-use std::{env, os::unix::fs::MetadataExt, path::PathBuf};
|
|
|
-use std::fs::read_dir;
|
|
|
-use humansize::{format_size, DECIMAL};
|
|
|
-
|
|
|
+use rocket::{fs::FileServer, launch, routes};
|
|
|
+use std::env;
|
|
|
use rocket::http::Header;
|
|
|
use rocket::{Request, Response};
|
|
|
use rocket::fairing::{Fairing, Info, Kind};
|
|
@@ -29,51 +26,67 @@ impl Fairing for Cors {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#[get("/ls/<path..>")]
|
|
|
-fn _list_dir(mut path: PathBuf) -> Value {
|
|
|
- if path.to_string_lossy().is_empty() {
|
|
|
- path = PathBuf::from(".");
|
|
|
- }
|
|
|
- let dir = read_dir(path);
|
|
|
- match dir {
|
|
|
- Ok(ls) => {
|
|
|
- let mut v: Vec<Value> = Vec::new();
|
|
|
- for elem in ls {
|
|
|
- if elem.is_err() {
|
|
|
- continue;
|
|
|
- }
|
|
|
- let e = elem.unwrap();
|
|
|
- let p = e.path();
|
|
|
- let name = e.file_name().to_string_lossy().to_string();
|
|
|
-
|
|
|
- if p.is_file() {
|
|
|
- let meta = e.metadata().unwrap();
|
|
|
- let size = meta.size();
|
|
|
- if name.starts_with(".") {
|
|
|
- v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL), "hidden": true}));
|
|
|
- } else {
|
|
|
- v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL)}));
|
|
|
- }
|
|
|
- } else if p.is_symlink() {
|
|
|
- if name.starts_with(".") {
|
|
|
- v.push(json!({"name": name.clone(), "type": "LINK", "hidden": true}));
|
|
|
- } else {
|
|
|
- v.push(json!({"name": name.clone(), "type": "LINK"}));
|
|
|
+#[cfg(feature="debug")]
|
|
|
+mod debug_extras {
|
|
|
+ use rocket::{get, routes, serde::json::{json, Value}, Route};
|
|
|
+ use std::path::PathBuf;
|
|
|
+ use std::fs::read_dir;
|
|
|
+ use humansize::{format_size, DECIMAL};
|
|
|
+ #[get("/ls/<path..>")]
|
|
|
+ fn _list_dir(mut path: PathBuf) -> Value {
|
|
|
+ if path.to_string_lossy().is_empty() {
|
|
|
+ path = PathBuf::from(".");
|
|
|
+ }
|
|
|
+ let dir = read_dir(path);
|
|
|
+ match dir {
|
|
|
+ Ok(ls) => {
|
|
|
+ let mut v: Vec<Value> = Vec::new();
|
|
|
+ for elem in ls {
|
|
|
+ if elem.is_err() {
|
|
|
+ continue;
|
|
|
}
|
|
|
- } else {
|
|
|
- if name.starts_with(".") {
|
|
|
- v.push(json!({"name": name.clone(), "type": "DIR", "hidden": true}))
|
|
|
+ let e = elem.unwrap();
|
|
|
+ let p = e.path();
|
|
|
+ let name = e.file_name().to_string_lossy().to_string();
|
|
|
+
|
|
|
+ if p.is_file() {
|
|
|
+ let meta = e.metadata().unwrap();
|
|
|
+ let size = meta.len();
|
|
|
+ if name.starts_with(".") {
|
|
|
+ v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL), "hidden": true}));
|
|
|
+ } else {
|
|
|
+ v.push(json!({"name": name.clone(), "type": "FILE", "size": format_size(size, DECIMAL)}));
|
|
|
+ }
|
|
|
+ } else if p.is_symlink() {
|
|
|
+ if name.starts_with(".") {
|
|
|
+ v.push(json!({"name": name.clone(), "type": "LINK", "hidden": true}));
|
|
|
+ } else {
|
|
|
+ v.push(json!({"name": name.clone(), "type": "LINK"}));
|
|
|
+ }
|
|
|
} else {
|
|
|
- v.push(json!({"name": name.clone(), "type": "DIR"}))
|
|
|
+ if name.starts_with(".") {
|
|
|
+ v.push(json!({"name": name.clone(), "type": "DIR", "hidden": true}))
|
|
|
+ } else {
|
|
|
+ v.push(json!({"name": name.clone(), "type": "DIR"}))
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ return json!({"status": 200, "dir": v});
|
|
|
+ }
|
|
|
+ Err(_) => {
|
|
|
+ return json!({"status": 404})
|
|
|
}
|
|
|
- return json!({"status": 200, "dir": v});
|
|
|
- }
|
|
|
- Err(_) => {
|
|
|
- return json!({"status": 404})
|
|
|
}
|
|
|
}
|
|
|
+ pub fn extras(rock: &mut Vec<Route>) {
|
|
|
+ rock.append(&mut routes![_list_dir]);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(not(feature="debug"))]
|
|
|
+mod debug_extras {
|
|
|
+ use rocket::Route;
|
|
|
+ pub fn extras(_rock: &mut Vec<Route>) {}
|
|
|
}
|
|
|
|
|
|
#[launch]
|
|
@@ -104,10 +117,15 @@ fn rocket() -> _ {
|
|
|
let fig = rock.figment().clone()
|
|
|
.merge((rocket::Config::PORT, port));
|
|
|
|
|
|
+ let mut r = routes![];
|
|
|
+ // This is a DEBUG only feature, it gives the option to list the given directory
|
|
|
+ if cfg!(feature="debug") {
|
|
|
+ debug_extras::extras(&mut r);
|
|
|
+ }
|
|
|
+
|
|
|
rock
|
|
|
.configure(fig)
|
|
|
.attach(Cors)
|
|
|
.mount("/", FileServer::from("."))
|
|
|
- // This is a DEBUG only feature, it gives the option to list the given directory
|
|
|
- //.mount("/", routes![_list_dir])
|
|
|
+ .mount("/", r)
|
|
|
}
|