This commit is contained in:
EvilMuffinHa 2022-03-02 00:17:19 -05:00
parent d8a77905f9
commit ad9cf01d90
6 changed files with 58 additions and 17 deletions

1
Cargo.lock generated
View File

@ -331,6 +331,7 @@ name = "fastcloud"
version = "0.1.0"
dependencies = [
"lazy_static",
"rand 0.8.5",
"rocket",
"rocket_contrib",
"rocket_upload",

View File

@ -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"

9
Dockerfile Normal file
View File

@ -0,0 +1,9 @@
FROM rustlang/rust:nightly
WORKDIR /app
COPY Cargo.toml ./
COPY src ./src
COPY templates ./templates
RUN cargo build --release

View File

@ -1 +0,0 @@
abcde

View File

@ -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/<path..>")]
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<i32, DirPath>> {
@ -75,10 +85,19 @@ fn directory_structure(path: String) -> HashMap<&'static str, HashMap<i32, DirPa
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 current = DirPath {
is_file: false,
path: dir.strip_prefix(DIR.clone().into_os_string().into_string().unwrap()).unwrap_or_else(|_| Path::new("")).to_str().unwrap().to_string(),
name: dir.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);
let mut pt = HashMap::new();
pt.insert(0, current);
context.insert("current", pt);
context.insert("ctx", paths);
context
}
@ -90,8 +109,8 @@ fn file_get(path: PathBuf) -> Option<NamedFile> {
}
#[post("/upload/<path..>", data = "<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())

View File

@ -1,5 +1,3 @@
{{! 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">
@ -10,6 +8,10 @@
<div class="vstack gap-3">
<div class="bg-light border rounded-3 p-2">
<div class="d-grid gap-2">
<form action="/upload/{{current.0.path}}" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="upload"/>
<input type="submit" value="Upload"/>
</form>
<a type="button" class="btn btn-outline-secondary text-start col-sm-1" href="/serve/{{parent.0.path}}" role="button">
..
</a>
@ -26,9 +28,11 @@
{{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> -->
<form action="/del/{{path}}" method="post">
<button class="btn btn-outline-danger text-start ms-1 me-1">
<i class="bi bi-trash"></i>
</button>
</form>
</div>
{{/each}}
</div>