initial commit

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-10-30 09:53:40 +05:30
commit ae85e9fe85
9 changed files with 4115 additions and 0 deletions

104
src/main.rs Normal file
View File

@@ -0,0 +1,104 @@
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
use rand::distributions::{Alphanumeric, DistString};
use rand::thread_rng;
use rocket::{form::Form, fs::NamedFile, fs::TempFile, http::Status, response::status, Config};
use std::{env, fs};
#[derive(FromForm)]
struct Upload<'r> {
file: TempFile<'r>,
#[field(default = "")]
key: String,
}
#[post("/", data = "<upload>")]
async fn post_file(mut upload: Form<Upload<'_>>) -> status::Custom<String> {
if env_use_key() && env_key() != upload.key {
return status::Custom(
Status::BadRequest,
"key not found in the header".to_string(),
);
}
if let Some(name) = upload.file.name() {
let extension = upload
.file
.content_type()
.unwrap()
.extension()
.unwrap_or("file".into());
let new_name = format!(
"{}-{}.{}",
Alphanumeric.sample_string(&mut thread_rng(), 4),
name,
extension
);
let uploaded = upload
.file
.copy_to(format!("{}/{}", env_root_dir(), new_name))
.await;
if let Err(error) = uploaded {
println!("Error while copying from temp file: {:?}", error);
return status::Custom(
Status::InternalServerError,
"Some stupid internal error occurred".to_string(),
);
}
return status::Custom(Status::Ok, format!("{}/{}", env_user_url(), new_name));
} else {
return status::Custom(Status::BadRequest, "File name invalid".to_string());
}
}
#[get("/<filename>", format = "text/html")]
async fn get_file(filename: String) -> Option<NamedFile> {
NamedFile::open(format!("{}/{}", env_root_dir(), filename))
.await
.ok()
}
#[get("/")]
fn index() -> String {
format!(
"Use curl to upload:\n\
curl -F file=@\"[file]\\n\" {url}\n\
If key is enabled then a header \"key\" might be required in which case it would be\n\
curl -F file=@\"[file]\" --header \"key: [key]\" {url}",
url = env_user_url()
)
}
fn env_root_dir() -> String {
env::var("ROOT_DIR").unwrap_or("/var/files".to_string())
}
fn env_use_key() -> bool {
env::var("USE_KEY")
.unwrap_or("false".to_string())
.parse::<bool>()
.unwrap_or(false)
}
fn env_key() -> String {
env::var("KEY").expect("KEY not set in the environment")
}
fn env_user_url() -> String {
let default_config = Config::default();
env::var("USER_URL").unwrap_or(format!("http://localhost:{}", default_config.port))
}
#[launch]
fn rocket() -> _ {
fs::create_dir_all(env_root_dir()).unwrap();
rocket::build().mount("/", routes![post_file, get_file, index])
}