From c2ad9ffd90d8a1023aa6d20c3fc0de4ade7f6576 Mon Sep 17 00:00:00 2001 From: Gunwant Jain Date: Wed, 6 Jan 2021 06:13:03 +0530 Subject: [PATCH] main.rs: add /submit route Getting into frontend biz. Also s/HOST/HOST_URL/ because I didn't realise that HOST is already a taken variable. Signed-off-by: Gunwant Jain --- src/main.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index c349dab..8e4eb1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,12 @@ use std::env; use std::io::prelude::*; use std::collections::HashMap; use std::path::Path; +use std::fs; use std::fs::File; use rocket_contrib::templates::Template; +use rocket::request::Form; +use rocket::response::Redirect; use rocket::Data; mod paste_id; @@ -57,16 +60,33 @@ fn upload(paste: Data) -> Result { paste.stream_to_file(filepath)?; let url = match tree_magic::from_filepath(filepath).as_str().contains("text") { - true => format!("https://{host}/p/{id}\n", host = env::var("HOST") + true => format!("https://{host}/p/{id}\n", host = env::var("HOST_URL") .unwrap_or("".to_string()), id = id), - false => format!("https://{host}/{id}\n", host = env::var("HOST") - .unwrap_or("".to_string()), id = id) + false => format!("https://{host}/{id}\n", host = env::var("HOST_URL") + .unwrap_or("http://localhost:8000".to_string()), id = id) }; Ok(url) } +#[derive(FromForm)] +struct PasteIdForm { + val: String, +} + +#[post("/submit", data = "")] +fn submit(paste: Form) -> Redirect { + let id = PasteId::new(4); + + let filename = format!("upload/{id}", id = id); + let content = paste.into_inner().val; + + fs::write(&filename, content).expect("Unable to write to the file"); + + Redirect::to(format!("/p/{id}", id = id)) +} + #[get("/")] fn index() -> Option