YYOOOOOO
This commit is contained in:
parent
294348a72e
commit
34e080e598
66
src/main.rs
66
src/main.rs
|
@ -31,6 +31,11 @@ struct DirPath {
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(FromForm)]
|
||||||
|
struct DirName {
|
||||||
|
name: String
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, FromForm, Deserialize)]
|
#[derive(Debug, FromForm, Deserialize)]
|
||||||
struct GenericUser {
|
struct GenericUser {
|
||||||
key: String,
|
key: String,
|
||||||
|
@ -46,9 +51,11 @@ impl<'a, 'r> FromRequest<'a, 'r> for GenericUser {
|
||||||
Some(key) => {
|
Some(key) => {
|
||||||
if key.value() == ADMIN_KEY.as_str() || key.value() == USER_KEY.as_str() {
|
if key.value() == ADMIN_KEY.as_str() || key.value() == USER_KEY.as_str() {
|
||||||
request::Outcome::Success(GenericUser { key: String::from(key.value()) })
|
request::Outcome::Success(GenericUser { key: String::from(key.value()) })
|
||||||
|
} else if *PUBLIC_READ {
|
||||||
|
request::Outcome::Success(GenericUser { key: String::from("") })
|
||||||
} else {
|
} else {
|
||||||
request::Outcome::Failure((Status::BadRequest, ()))
|
request::Outcome::Failure((Status::BadRequest, ()))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
if *PUBLIC_READ {
|
if *PUBLIC_READ {
|
||||||
|
@ -70,10 +77,10 @@ impl<'a, 'r> FromRequest<'a, 'r> for AdminUser {
|
||||||
if key.value() == ADMIN_KEY.as_str() {
|
if key.value() == ADMIN_KEY.as_str() {
|
||||||
request::Outcome::Success(AdminUser)
|
request::Outcome::Success(AdminUser)
|
||||||
} else {
|
} 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 = "<form>")]
|
#[post("/login", data = "<form>")]
|
||||||
fn login(form: Form<GenericUser>, mut cookies: Cookies) -> Redirect {
|
fn login(form: Form<GenericUser>, mut cookies: Cookies) -> Redirect {
|
||||||
println!("{:?}", form);
|
|
||||||
let key = form.into_inner().key;
|
let key = form.into_inner().key;
|
||||||
let mut cook = Cookie::new("key", key);
|
let mut cook = Cookie::new("key", key);
|
||||||
cook.set_http_only(true);
|
cook.set_http_only(true);
|
||||||
|
@ -95,26 +101,60 @@ fn login(form: Form<GenericUser>, mut cookies: Cookies) -> Redirect {
|
||||||
Redirect::to("/serve")
|
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")]
|
#[get("/serve")]
|
||||||
fn get_root(_guard: GenericUser) -> Template {
|
fn get_root(_guard: AdminUser) -> Template {
|
||||||
let path = DIR.as_path().to_str().unwrap_or("");
|
let path = DIR.as_path().to_str().unwrap_or("");
|
||||||
Template::render("dir", directory_structure(path.to_string()))
|
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/<path..>")]
|
#[get("/serve/<path..>")]
|
||||||
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 dir = DIR.as_path().join(path);
|
||||||
let path = dir.to_str().unwrap_or("");
|
let path = dir.to_str().unwrap_or("");
|
||||||
Template::render("dir", directory_structure(path.to_string()))
|
Template::render("dir", directory_structure(path.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/dir/<path..>")]
|
#[get("/serve/<path..>", rank = 2)]
|
||||||
fn create_dir(path: PathBuf, _guard: AdminUser) {
|
fn get_dir_entry_generic(path: PathBuf, _guard: GenericUser) -> Template {
|
||||||
let dir = DIR.as_path().join(path);
|
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/<path..>")]
|
#[post("/dir/<path..>", data = "<form>")]
|
||||||
|
fn create_dir(path: PathBuf, form: Form<DirName>, _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 = "<form>")]
|
||||||
|
fn create_dir_root(form: Form<DirName>, _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/<path..>")]
|
||||||
fn delete(path: PathBuf, _guard: AdminUser) -> Redirect {
|
fn delete(path: PathBuf, _guard: AdminUser) -> Redirect {
|
||||||
let dir = DIR.as_path().join(path.clone());
|
let dir = DIR.as_path().join(path.clone());
|
||||||
if dir.is_file() {
|
if dir.is_file() {
|
||||||
|
@ -209,5 +249,9 @@ fn upload_root(_content_type: &ContentType, data:MultipartDatas, _guard: AdminUs
|
||||||
fn main() {
|
fn main() {
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.attach(Template::fairing())
|
.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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,19 @@
|
||||||
<div class="bg-light border rounded-3 p-2">
|
<div class="bg-light border rounded-3 p-2">
|
||||||
<div class="d-grid gap-2">
|
<div class="d-grid gap-2">
|
||||||
<form action="/upload/{{current.0.path}}" method="post" enctype="multipart/form-data">
|
<form action="/upload/{{current.0.path}}" method="post" enctype="multipart/form-data">
|
||||||
<input type="file" id="file" name="upload"/>
|
<input type="file" id="file" name="upload" onchange="this.form.submit()" hidden/>
|
||||||
<input type="submit" value="Upload"/>
|
|
||||||
</form>
|
</form>
|
||||||
<a type="button" class="btn btn-outline-secondary text-start col-sm-1" href="/serve/{{parent.0.path}}" role="button">
|
<div class="d-flex flex-row">
|
||||||
..
|
<a type="button" class="btn btn-outline-primary text-start col-sm-1 ms-1 me-1" href="/serve/{{parent.0.path}}" role="button">
|
||||||
</a>
|
..
|
||||||
|
</a>
|
||||||
|
<a type="button" class="btn btn-outline-secondary text-start col-sm-1 ms-1 me-1" href="/logout" role="button">
|
||||||
|
Logout
|
||||||
|
</a>
|
||||||
|
<button class="btn btn-outline-success text-start col-sm-1 ms-1 me-1" onclick="document.getElementById('file').click()">
|
||||||
|
<i class="bi bi-file-earmark-plus"></i>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
{{#each ctx}}
|
{{#each ctx}}
|
||||||
<div class="d-flex flex-row">
|
<div class="d-flex flex-row">
|
||||||
{{#if is_file}}
|
{{#if is_file}}
|
||||||
|
@ -28,15 +35,24 @@
|
||||||
{{name}}
|
{{name}}
|
||||||
</a>
|
</a>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
<form action="/del/{{path}}" method="post">
|
|
||||||
<button class="btn btn-outline-danger text-start ms-1 me-1">
|
<a type="button" class="btn btn-outline-danger text-start ms-1 me-1" href="/del/{{path}}" role="button">
|
||||||
<i class="bi bi-trash"></i>
|
<i class="bi bi-trash"></i>
|
||||||
</button>
|
</a>
|
||||||
</form>
|
|
||||||
</div>
|
</div>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
|
<div class="d-flex flex-row">
|
||||||
|
<form class='form-inline' action="/dir/{{current.0.path}}" method="post" id = "create-form">
|
||||||
|
<div class="input-group mb-3 flex-nowrap">
|
||||||
|
<input type="text" class="form-control" id="name" name="name" placeholder = "Create Folder"/>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<script>
|
||||||
|
document.getElementById("name").addEventListener("keyup", function (event) { if (event.keyCode == 13) { document.getElementById("create-form").submit(); }});
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
38
templates/generic.html.hbs
Normal file
38
templates/generic.html.hbs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="vstack gap-3">
|
||||||
|
<div class="bg-light border rounded-3 p-2">
|
||||||
|
<div class="d-grid gap-2">
|
||||||
|
<div class="d-flex flex-row">
|
||||||
|
<a type="button" class="btn btn-outline-secondary text-start col-sm-1 ms-1 me-1" href="/serve/{{parent.0.path}}" role="button">
|
||||||
|
..
|
||||||
|
</a>
|
||||||
|
<a type="button" class="btn btn-outline-secondary text-start col-sm-1 ms-1 me-1" href="/logout" role="button">
|
||||||
|
Logout
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
{{#each ctx}}
|
||||||
|
<div class="d-flex flex-row">
|
||||||
|
{{#if is_file}}
|
||||||
|
<a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/file/{{path}}" role="button">
|
||||||
|
<i class="bi bi-file-earmark"></i>
|
||||||
|
{{name}}
|
||||||
|
</a>
|
||||||
|
{{else}}
|
||||||
|
<a type="button" class="btn btn-outline-secondary text-start col-sm-3" href="/serve/{{path}}" role="button">
|
||||||
|
<i class="bi bi-folder-fill"></i>
|
||||||
|
{{name}}
|
||||||
|
</a>
|
||||||
|
{{/if}}
|
||||||
|
</div>
|
||||||
|
{{/each}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -4,13 +4,22 @@
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<form action="/login" method="post">
|
<div class="vstack gap-3">
|
||||||
<div class="form-outline mb-4">
|
<div class="bg-light border rounded-3 p-2">
|
||||||
<input type="text", id="key", name="key" class="form-control form-control-lg" placeholder="Enter Key"/>
|
<div class="d-grid gap-2 col-sm-6">
|
||||||
<input type="submit" value="Login"/>
|
<form class='form-inline' action="/login" method="post" id = "login-form" enctype="application/x-ww-form-urlencoded">
|
||||||
|
<div class="input-group mb-3 flex-nowrap col-sm-6">
|
||||||
|
<input type="password" class="form-control" id="key" name="key" placeholder = "Enter Key"/>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
document.getElementById("login").addEventListener("keyup", function (event) { if (event.keyCode == 13) { document.getElementById("login-form").submit(); }});
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user