From 34e080e5983c59cf50dfaf145157f005a0689697 Mon Sep 17 00:00:00 2001 From: EvilMuffinHa Date: Wed, 2 Mar 2022 11:01:46 -0500 Subject: [PATCH] YYOOOOOO --- src/main.rs | 66 +++++++++++++++++++++++++++++++------- templates/dir.html.hbs | 36 +++++++++++++++------ templates/generic.html.hbs | 38 ++++++++++++++++++++++ templates/home.html.hbs | 19 ++++++++--- 4 files changed, 133 insertions(+), 26 deletions(-) create mode 100644 templates/generic.html.hbs diff --git a/src/main.rs b/src/main.rs index 7f85990..ab2a079 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,11 @@ struct DirPath { name: String, } +#[derive(FromForm)] +struct DirName { + name: String +} + #[derive(Debug, FromForm, Deserialize)] struct GenericUser { key: String, @@ -46,9 +51,11 @@ impl<'a, 'r> FromRequest<'a, 'r> for GenericUser { Some(key) => { if key.value() == ADMIN_KEY.as_str() || key.value() == USER_KEY.as_str() { request::Outcome::Success(GenericUser { key: String::from(key.value()) }) + } else if *PUBLIC_READ { + request::Outcome::Success(GenericUser { key: String::from("") }) } else { request::Outcome::Failure((Status::BadRequest, ())) - } + } }, None => { if *PUBLIC_READ { @@ -70,10 +77,10 @@ impl<'a, 'r> FromRequest<'a, 'r> for AdminUser { if key.value() == ADMIN_KEY.as_str() { request::Outcome::Success(AdminUser) } else { - request::Outcome::Failure((Status::BadRequest, ())) + request::Outcome::Forward(()) } }, - None => request::Outcome::Failure((Status::BadRequest, ())) + None => request::Outcome::Forward(()) } } } @@ -86,7 +93,6 @@ fn root() -> Template { #[post("/login", data = "
")] fn login(form: Form, mut cookies: Cookies) -> Redirect { - println!("{:?}", form); let key = form.into_inner().key; let mut cook = Cookie::new("key", key); cook.set_http_only(true); @@ -95,26 +101,60 @@ fn login(form: Form, mut cookies: Cookies) -> Redirect { Redirect::to("/serve") } +#[get("/logout")] +fn logout(mut cookies: Cookies) -> Redirect { + match cookies.get("key") { + Some(_) => { + cookies.remove(Cookie::named("key")); + Redirect::to("/") + } + None => { + Redirect::to("/") + } + } +} + #[get("/serve")] -fn get_root(_guard: GenericUser) -> Template { +fn get_root(_guard: AdminUser) -> Template { let path = DIR.as_path().to_str().unwrap_or(""); Template::render("dir", directory_structure(path.to_string())) } +#[get("/serve", rank = 2)] +fn get_root_generic(_guard: GenericUser) -> Template { + let path = DIR.as_path().to_str().unwrap_or(""); + Template::render("generic", directory_structure(path.to_string())) +} + #[get("/serve/")] -fn get_dir_entry(path: PathBuf, _guard: GenericUser) -> Template { +fn get_dir_entry(path: PathBuf, _guard: AdminUser) -> 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, _guard: AdminUser) { +#[get("/serve/", rank = 2)] +fn get_dir_entry_generic(path: PathBuf, _guard: GenericUser) -> Template { let dir = DIR.as_path().join(path); - fs::create_dir_all(dir).unwrap(); + let path = dir.to_str().unwrap_or(""); + Template::render("generic", directory_structure(path.to_string())) } -#[post("/del/")] +#[post("/dir/", data = "")] +fn create_dir(path: PathBuf, form: Form, _guard: AdminUser) -> Redirect { + let dir = DIR.as_path().join(path.clone()).join(form.into_inner().name); + fs::create_dir_all(dir).unwrap(); + Redirect::to(uri!(get_dir_entry: path)) +} + +#[post("/dir", data = "")] +fn create_dir_root(form: Form, _guard: AdminUser) -> Redirect { + let dir = DIR.as_path().join(form.into_inner().name); + fs::create_dir_all(dir).unwrap(); + Redirect::to(uri!(get_root)) +} + +#[get("/del/")] fn delete(path: PathBuf, _guard: AdminUser) -> Redirect { let dir = DIR.as_path().join(path.clone()); if dir.is_file() { @@ -209,5 +249,9 @@ fn upload_root(_content_type: &ContentType, data:MultipartDatas, _guard: AdminUs fn main() { rocket::ignite() .attach(Template::fairing()) - .mount("/", routes![root, login, get_root, get_dir_entry, create_dir, file_get, upload_root, upload_file, delete]).launch(); + .mount("/", routes![ + root, login, logout, + get_root, get_root_generic, get_dir_entry, get_dir_entry_generic, + create_dir, create_dir_root, file_get, + upload_root, upload_file, delete]).launch(); } diff --git a/templates/dir.html.hbs b/templates/dir.html.hbs index 2a91862..33aba35 100644 --- a/templates/dir.html.hbs +++ b/templates/dir.html.hbs @@ -9,12 +9,19 @@
- - + - - .. - + {{#each ctx}}
{{#if is_file}} @@ -28,15 +35,24 @@ {{name}} {{/if}} -
- -
+ + + +
{{/each}} +
+
+
+ +
+
+
+ diff --git a/templates/generic.html.hbs b/templates/generic.html.hbs new file mode 100644 index 0000000..dc8e18f --- /dev/null +++ b/templates/generic.html.hbs @@ -0,0 +1,38 @@ + + + + + + + +
+
+
+ + {{#each ctx}} +
+ {{#if is_file}} + + + {{name}} + + {{else}} + + + {{name}} + + {{/if}} +
+ {{/each}} +
+
+
+ + diff --git a/templates/home.html.hbs b/templates/home.html.hbs index 0c07877..41d266d 100644 --- a/templates/home.html.hbs +++ b/templates/home.html.hbs @@ -4,13 +4,22 @@ -
-
- - +
+
+
+ +
+ +
+ +
- +
+