diff --git a/Cargo.lock b/Cargo.lock index 7957717..a77337d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -330,9 +330,11 @@ checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" name = "fastcloud" version = "0.1.0" dependencies = [ + "lazy_static", "rocket", "rocket_contrib", "rocket_upload", + "serde", ] [[package]] @@ -1338,6 +1340,20 @@ name = "serde" version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +dependencies = [ + "proc-macro2 1.0.36", + "quote 1.0.15", + "syn 1.0.86", +] [[package]] name = "serde_json" diff --git a/Cargo.toml b/Cargo.toml index 7353b7c..3310e29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,9 +8,6 @@ edition = "2021" [dependencies] rocket = "0.4.10" rocket_upload = "0.1.0" - - -[dependencies.rocket_contrib] -version = "0.4.10" -default-features = false -features = ["handlebars_templates"] +rocket_contrib = { version = "0.4.10", default-features = false, features = ["handlebars_templates"] } +lazy_static = "1.4.0" +serde = { version = "1.0", features = ["derive"] } diff --git a/examples/test/test2/2.txt b/examples/test/test2/2.txt new file mode 100644 index 0000000..00dedf6 --- /dev/null +++ b/examples/test/test2/2.txt @@ -0,0 +1 @@ +abcde diff --git a/examples/test/test2/test3/3.txt b/examples/test/test2/test3/3.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index 56ca78e..502d531 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,56 +4,94 @@ #[macro_use] extern crate rocket; +#[macro_use] +extern crate lazy_static; + use std::path::{PathBuf, Path}; use std::env; -use std::fs::metadata; use std::fs; +use std::ffi::OsStr; use std::collections::HashMap; use rocket::response::NamedFile; use rocket::http::ContentType; use rocket_contrib::templates::Template; use rocket_upload::MultipartDatas; +use serde::Serialize; +#[derive(Serialize)] +struct DirPath { + is_file: bool, + path: String, + name: String, +} +lazy_static! { + static ref DIR: PathBuf = Path::new(env::var("FASTCLOUD_DIR").unwrap_or_else(|_| String::from("")).as_str()).to_path_buf(); +} - -#[get("/serve/")] -fn get_dir_entry(path: PathBuf) -> Template { - let dir = Path::new(env::var("FASTCLOUD_DIR").unwrap_or_else(|_| String::from("")).as_str()).join(path); - let path = dir.to_str().unwrap_or(""); - let md = metadata(path).unwrap(); +#[get("/serve")] +fn get_root() -> Template { + let path = DIR.as_path().to_str().unwrap_or(""); Template::render("dir", directory_structure(path.to_string())) } - +#[get("/serve/")] +fn get_dir_entry(path: PathBuf) -> Template { + let dir = DIR.as_path().join(path); + let path = dir.to_str().unwrap_or(""); + Template::render("dir", directory_structure(path.to_string())) +} #[post("/dir/")] fn create_dir(path: PathBuf) { + let dir = DIR.as_path().join(path); + fs::create_dir_all(dir).unwrap(); } +#[post("/del/")] +fn delete(path: PathBuf) { + let dir = DIR.as_path().join(path); + if dir.is_file() { + fs::remove_dir_all(dir).unwrap(); + } else { + fs::remove_file(dir).unwrap(); + } +} -fn directory_structure(path: String) -> HashMap<&'static str, HashMap> { - let dir = Path::new(env::var("FASTCLOUD_DIR").unwrap_or_else(|_| String::from("")).as_str()).join(path); +fn directory_structure(path: String) -> HashMap<&'static str, HashMap> { + let dir = Path::new(path.as_str()); let paths = fs::read_dir(dir).unwrap() .map(|x| x.unwrap().path().into_os_string().into_string().unwrap()) .enumerate() - .map(|x| (x.0 as i32, x.1)) - .collect::>(); - let mut context: HashMap<&str, HashMap> = HashMap::new(); + .map(|x| (x.0 as i32, DirPath { + is_file: Path::new(&x.1).is_file(), + path: Path::new(&x.1).strip_prefix(DIR.clone().into_os_string().into_string().unwrap()).unwrap().to_str().unwrap().to_string(), + name: Path::new(&x.1).file_name().unwrap().to_str().unwrap().to_string(), + })) + .collect::>(); + let mut context: HashMap<&str, HashMap> = HashMap::new(); + let parent = DirPath { + is_file: false, + path: dir.strip_prefix(DIR.clone().into_os_string().into_string().unwrap()).unwrap().parent().unwrap_or_else(|| Path::new("")).to_str().unwrap().to_string(), + name: dir.parent().unwrap().file_name().unwrap_or_else(|| OsStr::new("")).to_str().unwrap().to_string(), + }; + let mut pt = HashMap::new(); + pt.insert(0, parent); context.insert("ctx", paths); + context.insert("parent", pt); context } #[get("/file/")] fn file_get(path: PathBuf) -> Option { - let dir = Path::new(env::var("FASTCLOUD_DIR").unwrap_or_else(|_| String::from("")).as_str()).join(path); + let dir = DIR.as_path().join(path); rocket::response::NamedFile::open(dir.as_path()).ok() } #[post("/upload/", data = "")] fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas) { - let dir = Path::new(env::var("FASTCLOUD_DIR").unwrap_or_else(|_| String::from("")).as_str()).join(path); + let dir = DIR.as_path().join(path); for f in data.files { if !Path::new(&format!("{}/{}", dir.to_str().unwrap(), f.filename)).exists() { f.persist(&dir); @@ -63,16 +101,12 @@ fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas) f.persist(&dir); break } - } } } - - - fn main() { rocket::ignite() .attach(Template::fairing()) - .mount("/", routes![get_dir_entry, create_dir, file_get, upload_file]).launch(); + .mount("/", routes![get_root, get_dir_entry, create_dir, file_get, upload_file, delete]).launch(); } diff --git a/templates/dir.html.hbs b/templates/dir.html.hbs new file mode 100644 index 0000000..0d5f279 --- /dev/null +++ b/templates/dir.html.hbs @@ -0,0 +1,38 @@ +{{! vim: set ft=html: }} + + + + + + + + +
+
+
+ + .. + + {{#each ctx}} +
+ {{#if is_file}} + + + {{name}} + + {{else}} + + + {{name}} + + {{/if}} + +
+ {{/each}} +
+
+
+ +