From 294348a72e7c2b2ed1bedda6e15773aa934c9eda Mon Sep 17 00:00:00 2001 From: EvilMuffinHa Date: Wed, 2 Mar 2022 02:08:29 -0500 Subject: [PATCH] 422 --- src/main.rs | 102 ++++++++++++++++++++++++++++++++++------ templates/home.html.hbs | 17 +++++++ 2 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 templates/home.html.hbs diff --git a/src/main.rs b/src/main.rs index f82b105..7f85990 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,11 +11,19 @@ use std::fs; use std::ffi::OsStr; use std::collections::HashMap; use rocket::response::{NamedFile, Redirect}; -use rocket::http::ContentType; +use rocket::http::{Cookie, Cookies, ContentType, Status}; +use rocket::request::{self, Request, Form, FromRequest}; use rocket_contrib::templates::Template; use rocket_upload::MultipartDatas; use serde::{Serialize, Deserialize}; +lazy_static! { + static ref DIR: PathBuf = Path::new(env::var("FASTCLOUD_DIR").unwrap_or_else(|_| String::from("")).as_str()).to_path_buf(); + static ref ADMIN_KEY: String = env::var("FASTCLOUD_ADMIN_KEY").unwrap_or_else(|_| String::from("")); + static ref USER_KEY: String = env::var("FASTCLOUD_USER_KEY").unwrap_or_else(|_| String::from("")); + static ref PUBLIC_READ: bool = env::var("FASTCLOUD_PUBLIC").unwrap_or_else(|_| String::from("")) != *""; +} + #[derive(Serialize)] struct DirPath { is_file: bool, @@ -23,41 +31,91 @@ struct DirPath { name: String, } -#[derive(FromForm, Deserialize)] -struct LoginInfo { +#[derive(Debug, FromForm, Deserialize)] +struct GenericUser { key: String, } +struct AdminUser; +impl<'a, 'r> FromRequest<'a, 'r> for GenericUser { + type Error = (); -lazy_static! { - static ref DIR: PathBuf = Path::new(env::var("FASTCLOUD_DIR").unwrap_or_else(|_| String::from("")).as_str()).to_path_buf(); - static ref ADMIN_KEY: PathBuf = Path::new(env::var("FASTCLOUD_ADMIN_KEY").unwrap_or_else(|_| String::from("")).as_str()).to_path_buf(); - static ref USER_KEY: PathBuf = Path::new(env::var("FASTCLOUD_USER_KEY").unwrap_or_else(|_| String::from("")).as_str()).to_path_buf(); + fn from_request(request: &'a Request<'r>) -> request::Outcome { + match request.cookies().get("key") { + 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 { + request::Outcome::Failure((Status::BadRequest, ())) + } + }, + None => { + if *PUBLIC_READ { + request::Outcome::Success(GenericUser { key: String::from("") }) + } else { + request::Outcome::Failure((Status::BadRequest, ())) + } + } + } + } } +impl<'a, 'r> FromRequest<'a, 'r> for AdminUser { + type Error = (); + + fn from_request(request: &'a Request<'r>) -> request::Outcome { + match request.cookies().get("key") { + Some(key) => { + if key.value() == ADMIN_KEY.as_str() { + request::Outcome::Success(AdminUser) + } else { + request::Outcome::Failure((Status::BadRequest, ())) + } + }, + None => request::Outcome::Failure((Status::BadRequest, ())) + } + } +} + +#[get("/")] +fn root() -> Template { + let map: HashMap = HashMap::new(); + Template::render("home", map) +} + +#[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); + cook.set_secure(true); + cookies.add(cook); + Redirect::to("/serve") +} #[get("/serve")] -fn get_root() -> Template { +fn get_root(_guard: GenericUser) -> 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 { +fn get_dir_entry(path: PathBuf, _guard: GenericUser) -> 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) { +fn create_dir(path: PathBuf, _guard: AdminUser) { let dir = DIR.as_path().join(path); fs::create_dir_all(dir).unwrap(); } #[post("/del/")] -fn delete(path: PathBuf) -> Redirect { +fn delete(path: PathBuf, _guard: AdminUser) -> Redirect { let dir = DIR.as_path().join(path.clone()); if dir.is_file() { fs::remove_file(dir).unwrap(); @@ -103,13 +161,13 @@ fn directory_structure(path: String) -> HashMap<&'static str, HashMap")] -fn file_get(path: PathBuf) -> Option { +fn file_get(path: PathBuf, _guard: GenericUser) -> Option { 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) -> Redirect { +fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas, _guard: AdminUser) -> Redirect { let dir = DIR.as_path().join(path.clone()); for f in data.files { if !Path::new(&format!("{}/{}", dir.to_str().unwrap(), f.filename)).exists() { @@ -125,6 +183,22 @@ fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas) Redirect::to(uri!(get_dir_entry: path)) } +#[post("/upload", data = "")] +fn upload_root(_content_type: &ContentType, data:MultipartDatas, _guard: AdminUser) -> Redirect { + let dir = DIR.as_path(); + for f in data.files { + if !Path::new(&format!("{}/{}", dir.to_str().unwrap(), f.filename)).exists() { + f.persist(dir); + } + for i in 0.. { + if !Path::new(&format!("{}/{}.{}", dir.to_str().unwrap(), f.filename, i)).exists() { + f.persist(dir); + break + } + } + } + Redirect::to(uri!(get_root)) +} @@ -135,5 +209,5 @@ fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas) fn main() { rocket::ignite() .attach(Template::fairing()) - .mount("/", routes![get_root, get_dir_entry, create_dir, file_get, upload_file, delete]).launch(); + .mount("/", routes![root, login, get_root, get_dir_entry, create_dir, file_get, upload_root, upload_file, delete]).launch(); } diff --git a/templates/home.html.hbs b/templates/home.html.hbs new file mode 100644 index 0000000..0c07877 --- /dev/null +++ b/templates/home.html.hbs @@ -0,0 +1,17 @@ + + + + + + + +
+ + +
+ + + + + +