woooowww alskdfjaelkjgioaefjvpowevap
This commit is contained in:
parent
2b0bc99ab2
commit
d8a77905f9
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -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"] }
|
||||
|
|
1
examples/test/test2/2.txt
Normal file
1
examples/test/test2/2.txt
Normal file
|
@ -0,0 +1 @@
|
|||
abcde
|
0
examples/test/test2/test3/3.txt
Normal file
0
examples/test/test2/test3/3.txt
Normal file
74
src/main.rs
74
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/<path..>")]
|
||||
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/<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..>")]
|
||||
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>> {
|
||||
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<i32, DirPath>> {
|
||||
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::<HashMap<i32, String>>();
|
||||
let mut context: HashMap<&str, HashMap<i32, String>> = 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::<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("parent", pt);
|
||||
context
|
||||
}
|
||||
|
||||
|
||||
#[get("/file/<path..>")]
|
||||
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()
|
||||
}
|
||||
|
||||
#[post("/upload/<path..>", data = "<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();
|
||||
}
|
||||
|
|
38
templates/dir.html.hbs
Normal file
38
templates/dir.html.hbs
Normal 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>
|
Loading…
Reference in New Issue
Block a user