diff --git a/Cargo.lock b/Cargo.lock index a77337d..f8bad29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -331,6 +331,7 @@ name = "fastcloud" version = "0.1.0" dependencies = [ "lazy_static", + "rand 0.8.5", "rocket", "rocket_contrib", "rocket_upload", diff --git a/Cargo.toml b/Cargo.toml index 3310e29..1f9b12a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ rocket_upload = "0.1.0" rocket_contrib = { version = "0.4.10", default-features = false, features = ["handlebars_templates"] } lazy_static = "1.4.0" serde = { version = "1.0", features = ["derive"] } +rand = "0.8.5" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a0385e2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM rustlang/rust:nightly + +WORKDIR /app +COPY Cargo.toml ./ +COPY src ./src +COPY templates ./templates + + +RUN cargo build --release diff --git a/examples/test/test2/2.txt b/examples/test/test2/2.txt deleted file mode 100644 index 00dedf6..0000000 --- a/examples/test/test2/2.txt +++ /dev/null @@ -1 +0,0 @@ -abcde diff --git a/src/main.rs b/src/main.rs index 502d531..f82b105 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ - #![feature(proc_macro_hygiene, decl_macro)] #[macro_use] @@ -6,17 +5,16 @@ extern crate rocket; #[macro_use] extern crate lazy_static; - use std::path::{PathBuf, Path}; use std::env; use std::fs; use std::ffi::OsStr; use std::collections::HashMap; -use rocket::response::NamedFile; +use rocket::response::{NamedFile, Redirect}; use rocket::http::ContentType; use rocket_contrib::templates::Template; use rocket_upload::MultipartDatas; -use serde::Serialize; +use serde::{Serialize, Deserialize}; #[derive(Serialize)] struct DirPath { @@ -25,10 +23,20 @@ struct DirPath { name: String, } +#[derive(FromForm, Deserialize)] +struct LoginInfo { + key: String, +} + + + 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(); } + #[get("/serve")] fn get_root() -> Template { let path = DIR.as_path().to_str().unwrap_or(""); @@ -49,13 +57,15 @@ fn create_dir(path: PathBuf) { } #[post("/del/")] -fn delete(path: PathBuf) { - let dir = DIR.as_path().join(path); +fn delete(path: PathBuf) -> Redirect { + let dir = DIR.as_path().join(path.clone()); if dir.is_file() { - fs::remove_dir_all(dir).unwrap(); - } else { fs::remove_file(dir).unwrap(); + } else { + fs::remove_dir_all(dir).unwrap(); } + let npath = path.parent().unwrap().to_path_buf(); + Redirect::to(uri!(get_dir_entry: npath)) } fn directory_structure(path: String) -> HashMap<&'static str, HashMap> { @@ -75,10 +85,19 @@ fn directory_structure(path: String) -> HashMap<&'static str, HashMap Option { } #[post("/upload/", data = "")] -fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas) { - let dir = DIR.as_path().join(path); +fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas) -> 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() { f.persist(&dir); @@ -103,8 +122,16 @@ fn upload_file(path: PathBuf, _content_type: &ContentType, data:MultipartDatas) } } } + Redirect::to(uri!(get_dir_entry: path)) } + + + +// TODO: move files +// TODO: rename files +// TODO: rewrite frontend + fn main() { rocket::ignite() .attach(Template::fairing()) diff --git a/templates/dir.html.hbs b/templates/dir.html.hbs index 0d5f279..2a91862 100644 --- a/templates/dir.html.hbs +++ b/templates/dir.html.hbs @@ -1,5 +1,3 @@ -{{! vim: set ft=html: }} - @@ -10,6 +8,10 @@
+
+ + +
.. @@ -26,9 +28,11 @@ {{name}} {{/if}} - +
+ +
{{/each}}