add /p/<id> routes
This is to work with text/plain type files only Signed-off-by: Gunwant Jain <mail@wantguns.dev>
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
upload/*
|
||||||
|
|||||||
759
Cargo.lock
generated
759
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -9,3 +9,11 @@ edition = "2018"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
rand = "0.8.0"
|
rand = "0.8.0"
|
||||||
rocket = "0.4.6"
|
rocket = "0.4.6"
|
||||||
|
rocket_contrib = { version = "0.4.6", features = ["tera_templates"] }
|
||||||
|
tree_magic = "0.2.3"
|
||||||
|
|
||||||
|
# [dependencies.rocket_contrib]
|
||||||
|
# version = "0.4.6"
|
||||||
|
# default-features = false
|
||||||
|
# features = ["tera_templates"]
|
||||||
|
|
||||||
|
|||||||
20
src/main.rs
20
src/main.rs
@@ -1,17 +1,31 @@
|
|||||||
#![feature(proc_macro_hygiene, decl_macro)]
|
#![feature(proc_macro_hygiene, decl_macro)]
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use] extern crate rocket;
|
||||||
|
|
||||||
|
extern crate tree_magic;
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
use rocket::Data;
|
use rocket::Data;
|
||||||
use rocket::http::RawStr;
|
use rocket::http::RawStr;
|
||||||
|
use rocket_contrib::templates::Template;
|
||||||
|
|
||||||
mod paste_id;
|
mod paste_id;
|
||||||
|
|
||||||
use paste_id::PasteId;
|
use paste_id::PasteId;
|
||||||
|
|
||||||
|
#[get("/p/<id>")]
|
||||||
|
fn pretty_retrieve(id: PasteId) -> Option<File> {
|
||||||
|
let filename = format!("upload/{id}", id = id);
|
||||||
|
let filepath = Path::new(&filename);
|
||||||
|
|
||||||
|
match tree_magic::from_filepath(filepath).as_str() {
|
||||||
|
"text/plain" => File::open(&filename).ok(),
|
||||||
|
_ => Err("does not have the MIME type of a plaintext file").ok()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/<id>")]
|
#[get("/<id>")]
|
||||||
fn retrieve(id: PasteId) -> Option<File> {
|
fn retrieve(id: PasteId) -> Option<File> {
|
||||||
let filename = format!("upload/{id}", id = id);
|
let filename = format!("upload/{id}", id = id);
|
||||||
@@ -43,11 +57,15 @@ USAGE
|
|||||||
GET /<id>
|
GET /<id>
|
||||||
|
|
||||||
retrieves the content for the paste with id `<id>`
|
retrieves the content for the paste with id `<id>`
|
||||||
|
|
||||||
|
GET /p/<id>
|
||||||
|
|
||||||
|
retrieves the content for the paste with id `<id>`, with syntax highlighting
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.mount("/", routes![index, upload, retrieve])
|
.mount("/", routes![index, upload, retrieve, pretty_retrieve])
|
||||||
.launch();
|
.launch();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user