first commit

This commit is contained in:
EvilMuffinHa 2022-03-01 02:26:14 -05:00
commit 2b0bc99ab2
6 changed files with 1780 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1685
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

16
Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "fastcloud"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.10"
rocket_upload = "0.1.0"
[dependencies.rocket_contrib]
version = "0.4.10"
default-features = false
features = ["handlebars_templates"]

Binary file not shown.

BIN
examples/test/clap.mp4 Normal file

Binary file not shown.

78
src/main.rs Normal file
View File

@ -0,0 +1,78 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
use std::path::{PathBuf, Path};
use std::env;
use std::fs::metadata;
use std::fs;
use std::collections::HashMap;
use rocket::response::NamedFile;
use rocket::http::ContentType;
use rocket_contrib::templates::Template;
use rocket_upload::MultipartDatas;
#[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();
Template::render("dir", directory_structure(path.to_string()))
}
#[post("/dir/<path..>")]
fn create_dir(path: PathBuf) {
}
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);
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();
context.insert("ctx", paths);
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);
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);
for f in data.files {
if !Path::new(&format!("{}/{}", dir.to_str().unwrap(), f.filename)).exists() {
f.persist(&dir);
}
for i in 0.. {
if !Path::new(&format!("{}/{}.{}", dir.to_str().unwrap(), f.filename, i)).exists() {
f.persist(&dir);
break
}
}
}
}
fn main() {
rocket::ignite()
.attach(Template::fairing())
.mount("/", routes![get_dir_entry, create_dir, file_get, upload_file]).launch();
}