woooowww alskdfjaelkjgioaefjvpowevap

This commit is contained in:
EvilMuffinHa 2022-03-01 11:18:48 -05:00
parent 2b0bc99ab2
commit d8a77905f9
6 changed files with 112 additions and 26 deletions

16
Cargo.lock generated
View File

@ -330,9 +330,11 @@ checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
name = "fastcloud" name = "fastcloud"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"lazy_static",
"rocket", "rocket",
"rocket_contrib", "rocket_contrib",
"rocket_upload", "rocket_upload",
"serde",
] ]
[[package]] [[package]]
@ -1338,6 +1340,20 @@ name = "serde"
version = "1.0.136" version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" 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]] [[package]]
name = "serde_json" name = "serde_json"

View File

@ -8,9 +8,6 @@ edition = "2021"
[dependencies] [dependencies]
rocket = "0.4.10" rocket = "0.4.10"
rocket_upload = "0.1.0" rocket_upload = "0.1.0"
rocket_contrib = { version = "0.4.10", default-features = false, features = ["handlebars_templates"] }
lazy_static = "1.4.0"
[dependencies.rocket_contrib] serde = { version = "1.0", features = ["derive"] }
version = "0.4.10"
default-features = false
features = ["handlebars_templates"]

View File

@ -0,0 +1 @@
abcde

View File

View File

@ -4,56 +4,94 @@
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
#[macro_use]
extern crate lazy_static;
use std::path::{PathBuf, Path}; use std::path::{PathBuf, Path};
use std::env; use std::env;
use std::fs::metadata;
use std::fs; use std::fs;
use std::ffi::OsStr;
use std::collections::HashMap; use std::collections::HashMap;
use rocket::response::NamedFile; use rocket::response::NamedFile;
use rocket::http::ContentType; use rocket::http::ContentType;
use rocket_contrib::templates::Template; use rocket_contrib::templates::Template;
use rocket_upload::MultipartDatas; 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")]
#[get("/serve/<path..>")] fn get_root() -> Template {
fn get_dir_entry(path: PathBuf) -> Template { let path = DIR.as_path().to_str().unwrap_or("");
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();
Template::render("dir", directory_structure(path.to_string())) Template::render("dir", directory_structure(path.to_string()))
} }
#[get("/serve/<path..>")]
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/<path..>")] #[post("/dir/<path..>")]
fn create_dir(path: PathBuf) { fn create_dir(path: PathBuf) {
let dir = DIR.as_path().join(path);
fs::create_dir_all(dir).unwrap();
} }
#[post("/del/<path..>")]
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<i32, String>> { fn directory_structure(path: String) -> HashMap<&'static str, HashMap<i32, DirPath>> {
let dir = Path::new(env::var("FASTCLOUD_DIR").unwrap_or_else(|_| String::from("")).as_str()).join(path); let dir = Path::new(path.as_str());
let paths = fs::read_dir(dir).unwrap() let paths = fs::read_dir(dir).unwrap()
.map(|x| x.unwrap().path().into_os_string().into_string().unwrap()) .map(|x| x.unwrap().path().into_os_string().into_string().unwrap())
.enumerate() .enumerate()
.map(|x| (x.0 as i32, x.1)) .map(|x| (x.0 as i32, DirPath {
.collect::<HashMap<i32, String>>(); is_file: Path::new(&x.1).is_file(),
let mut context: HashMap<&str, HashMap<i32, String>> = HashMap::new(); 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::<HashMap<i32, DirPath>>();
let mut context: HashMap<&str, HashMap<i32, DirPath>> = 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("ctx", paths);
context.insert("parent", pt);
context context
} }
#[get("/file/<path..>")] #[get("/file/<path..>")]
fn file_get(path: PathBuf) -> Option<NamedFile> { fn file_get(path: PathBuf) -> Option<NamedFile> {
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() rocket::response::NamedFile::open(dir.as_path()).ok()
} }
#[post("/upload/<path..>", data = "<data>")] #[post("/upload/<path..>", data = "<data>")]
fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas) { 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 { for f in data.files {
if !Path::new(&format!("{}/{}", dir.to_str().unwrap(), f.filename)).exists() { if !Path::new(&format!("{}/{}", dir.to_str().unwrap(), f.filename)).exists() {
f.persist(&dir); f.persist(&dir);
@ -63,16 +101,12 @@ fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas)
f.persist(&dir); f.persist(&dir);
break break
} }
} }
} }
} }
fn main() { fn main() {
rocket::ignite() rocket::ignite()
.attach(Template::fairing()) .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();
} }

38
templates/dir.html.hbs Normal file
View File

@ -0,0 +1,38 @@
{{! vim: set ft=html: }}
<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">
<a type="button" class="btn btn-outline-secondary text-start col-sm-1" href="/serve/{{parent.0.path}}" role="button">
..
</a>
{{#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}}
<!-- <a type="button" class="btn btn-outline-danger text-start col-sm-1 ms-1 me-1" href="/del/{{path}}", role="button">
<i class="bi bi-trash"></i>
</a> -->
</div>
{{/each}}
</div>
</div>
</div>
</body>
</html>