refactor: bring in the modules
Signed-off-by: Gunwant Jain <mail@wantguns.dev>
This commit is contained in:
parent
8a3b29a0ef
commit
9c5a3af128
126
src/main.rs
126
src/main.rs
|
@ -1,120 +1,10 @@
|
|||
#[macro_use]
|
||||
extern crate rocket;
|
||||
use rocket::data::{Data, ToByteUnit};
|
||||
use rocket::shield::{Shield, NoSniff};
|
||||
use rocket::{form::Form, response::Redirect};
|
||||
use rocket_dyn_templates::Template;
|
||||
|
||||
extern crate tree_magic;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
|
||||
mod paste_id;
|
||||
mod pretty;
|
||||
mod pretty_syntax;
|
||||
|
||||
use paste_id::PasteId;
|
||||
use pretty::get_pretty_body;
|
||||
use pretty_syntax::PasteIdSyntax;
|
||||
|
||||
#[get("/p/<id_ext>", rank = 1)]
|
||||
async fn pretty_retrieve_ext(id_ext: PasteIdSyntax<'_>) -> Option<Template> {
|
||||
let id = id_ext.get_fname();
|
||||
let ext = id_ext.get_ext();
|
||||
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let filepath = Path::new(&filename);
|
||||
|
||||
let contents = get_pretty_body(&filename, &ext.to_string());
|
||||
let theme = env::var("THEME").unwrap_or("".to_string());
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert("title", id.to_string());
|
||||
map.insert("theme", theme);
|
||||
map.insert("body", contents);
|
||||
let rendered = Template::render("pretty", &map);
|
||||
|
||||
match tree_magic::match_filepath("text/plain", filepath) {
|
||||
true => Some(rendered),
|
||||
false => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/p/<id>", rank = 2)]
|
||||
async fn pretty_retrieve(id: PasteId<'_>) -> Option<Template> {
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let filepath = Path::new(&filename);
|
||||
|
||||
let contents = get_pretty_body(&filename, &String::from("txt"));
|
||||
let theme = env::var("THEME").unwrap_or("".to_string());
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert("title", id.to_string());
|
||||
map.insert("theme", theme);
|
||||
map.insert("body", contents);
|
||||
let rendered = Template::render("pretty", &map);
|
||||
|
||||
match tree_magic::match_filepath("text/plain", filepath) {
|
||||
true => Some(rendered),
|
||||
false => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/<id>")]
|
||||
async fn retrieve(id: PasteId<'_>) -> Option<File> {
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
|
||||
File::open(&filename).ok()
|
||||
}
|
||||
|
||||
#[post("/", data = "<paste>")]
|
||||
async fn upload(paste: Data<'_>) -> Result<Redirect, std::io::Error> {
|
||||
let id = PasteId::new(6);
|
||||
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let filepath = Path::new(&filename);
|
||||
|
||||
paste.open(100.mebibytes()).into_file(filepath).await?;
|
||||
|
||||
let url = match tree_magic::from_filepath(filepath)
|
||||
.as_str()
|
||||
.contains("text")
|
||||
{
|
||||
true => format!("/p/{id}", id = id),
|
||||
|
||||
false => format!("/{id}", id = id),
|
||||
};
|
||||
|
||||
Ok(Redirect::to(url))
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct PasteIdForm {
|
||||
val: String,
|
||||
}
|
||||
|
||||
#[post("/submit", data = "<paste>")]
|
||||
async fn submit(paste: Form<PasteIdForm>) -> 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("/")]
|
||||
async fn index() -> Option<Template> {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("title", "bin");
|
||||
Some(Template::render("index", &map))
|
||||
}
|
||||
mod models;
|
||||
mod routes;
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
|
@ -124,12 +14,12 @@ fn rocket() -> _ {
|
|||
.mount(
|
||||
"/",
|
||||
routes![
|
||||
index,
|
||||
upload,
|
||||
submit,
|
||||
retrieve,
|
||||
pretty_retrieve,
|
||||
pretty_retrieve_ext
|
||||
routes::index::index,
|
||||
routes::upload::upload,
|
||||
routes::submit::submit,
|
||||
routes::retrieve::retrieve,
|
||||
routes::pretty_retrieve::pretty_retrieve,
|
||||
routes::pretty_retrieve_ext::pretty_retrieve_ext
|
||||
],
|
||||
)
|
||||
.attach(shield)
|
||||
|
|
3
src/models/mod.rs
Normal file
3
src/models/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod paste_id;
|
||||
pub mod pretty_syntax;
|
||||
pub mod pretty;
|
10
src/routes/index.rs
Normal file
10
src/routes/index.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use rocket_dyn_templates::Template;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[get("/")]
|
||||
pub async fn index() -> Option<Template> {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("title", "bin");
|
||||
Some(Template::render("index", &map))
|
||||
}
|
6
src/routes/mod.rs
Normal file
6
src/routes/mod.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
pub mod index;
|
||||
pub mod pretty_retrieve;
|
||||
pub mod pretty_retrieve_ext;
|
||||
pub mod submit;
|
||||
pub mod upload;
|
||||
pub mod retrieve;
|
28
src/routes/pretty_retrieve.rs
Normal file
28
src/routes/pretty_retrieve.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use rocket_dyn_templates::Template;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::models::paste_id::PasteId;
|
||||
use crate::models::pretty::get_pretty_body;
|
||||
|
||||
#[get("/p/<id>", rank = 2)]
|
||||
pub async fn pretty_retrieve(id: PasteId<'_>) -> Option<Template> {
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let filepath = Path::new(&filename);
|
||||
|
||||
let contents = get_pretty_body(&filename, &String::from("txt"));
|
||||
let theme = env::var("THEME").unwrap_or("".to_string());
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert("title", id.to_string());
|
||||
map.insert("theme", theme);
|
||||
map.insert("body", contents);
|
||||
let rendered = Template::render("pretty", &map);
|
||||
|
||||
match tree_magic::match_filepath("text/plain", filepath) {
|
||||
true => Some(rendered),
|
||||
false => None,
|
||||
}
|
||||
}
|
31
src/routes/pretty_retrieve_ext.rs
Normal file
31
src/routes/pretty_retrieve_ext.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use rocket_dyn_templates::Template;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::models::pretty_syntax::PasteIdSyntax;
|
||||
use crate::models::pretty::get_pretty_body;
|
||||
|
||||
#[get("/p/<id_ext>", rank = 1)]
|
||||
pub async fn pretty_retrieve_ext(id_ext: PasteIdSyntax<'_>) -> Option<Template> {
|
||||
let id = id_ext.get_fname();
|
||||
let ext = id_ext.get_ext();
|
||||
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let filepath = Path::new(&filename);
|
||||
|
||||
let contents = get_pretty_body(&filename, &ext.to_string());
|
||||
let theme = env::var("THEME").unwrap_or("".to_string());
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert("title", id.to_string());
|
||||
map.insert("theme", theme);
|
||||
map.insert("body", contents);
|
||||
let rendered = Template::render("pretty", &map);
|
||||
|
||||
match tree_magic::match_filepath("text/plain", filepath) {
|
||||
true => Some(rendered),
|
||||
false => None,
|
||||
}
|
||||
}
|
10
src/routes/retrieve.rs
Normal file
10
src/routes/retrieve.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use std::fs::File;
|
||||
|
||||
use crate::models::paste_id::PasteId;
|
||||
|
||||
#[get("/<id>")]
|
||||
pub async fn retrieve(id: PasteId<'_>) -> Option<File> {
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
|
||||
File::open(&filename).ok()
|
||||
}
|
22
src/routes/submit.rs
Normal file
22
src/routes/submit.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
use rocket::{form::Form, response::Redirect};
|
||||
|
||||
use std::fs;
|
||||
|
||||
use crate::models::paste_id::PasteId;
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct PasteIdForm {
|
||||
val: String,
|
||||
}
|
||||
|
||||
#[post("/submit", data = "<paste>")]
|
||||
pub async fn submit(paste: Form<PasteIdForm>) -> Redirect {
|
||||
let id = PasteId::new(6);
|
||||
|
||||
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))
|
||||
}
|
27
src/routes/upload.rs
Normal file
27
src/routes/upload.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use rocket::data::{Data, ToByteUnit};
|
||||
use rocket::response::Redirect;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use crate::models::paste_id::PasteId;
|
||||
|
||||
#[post("/", data = "<paste>")]
|
||||
pub async fn upload(paste: Data<'_>) -> Result<Redirect, std::io::Error> {
|
||||
let id = PasteId::new(6);
|
||||
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
let filepath = Path::new(&filename);
|
||||
|
||||
paste.open(100.mebibytes()).into_file(filepath).await?;
|
||||
|
||||
let url = match tree_magic::from_filepath(filepath)
|
||||
.as_str()
|
||||
.contains("text")
|
||||
{
|
||||
true => format!("/p/{id}", id = id),
|
||||
|
||||
false => format!("/{id}", id = id),
|
||||
};
|
||||
|
||||
Ok(Redirect::to(url))
|
||||
}
|
Loading…
Reference in New Issue
Block a user