Separate id generation and persisting

This commit is contained in:
Jordan Johnson-Doyle 2019-02-15 21:01:37 +00:00
parent d6fa8ad7b0
commit e689ff7714
No known key found for this signature in database
GPG key ID: A95F87B578CE79B6
2 changed files with 14 additions and 13 deletions

View file

@ -30,17 +30,16 @@ fn purge_old() {
} }
} }
/// Generates a randomly generated id, stores the given paste under that id and then returns the id. /// Generates a 'pronounceable' random ID using gpw
/// pub fn generate_id() -> String {
/// Uses gpw to generate a (most likely) pronounceable URL.
pub fn store_paste(content: String) -> String {
thread_local!(static KEYGEN: RefCell<gpw::PasswordGenerator> = RefCell::new(gpw::PasswordGenerator::default())); thread_local!(static KEYGEN: RefCell<gpw::PasswordGenerator> = RefCell::new(gpw::PasswordGenerator::default()));
let id = KEYGEN.with(|k| k.borrow_mut().next().unwrap()); KEYGEN.with(|k| k.borrow_mut().next().unwrap())
}
/// Stores a paste under the given id
pub fn store_paste(id: String, content: String) {
purge_old(); purge_old();
ENTRIES.write().unwrap().insert(id.clone(), content); ENTRIES.write().unwrap().insert(id, content);
id
} }
/// Get a paste by id. /// Get a paste by id.

View file

@ -12,7 +12,7 @@ mod io;
mod highlight; mod highlight;
mod params; mod params;
use io::{store_paste, get_paste}; use io::{store_paste, get_paste, generate_id};
use highlight::highlight; use highlight::highlight;
use params::{IsPlaintextRequest, HostHeader}; use params::{IsPlaintextRequest, HostHeader};
@ -52,7 +52,8 @@ struct IndexForm {
#[post("/", data = "<input>")] #[post("/", data = "<input>")]
fn submit(input: Form<IndexForm>) -> Redirect { fn submit(input: Form<IndexForm>) -> Redirect {
let id = store_paste(input.into_inner().val); let id = generate_id();
store_paste(id.clone(), input.into_inner().val);
Redirect::to(uri!(render: id)) Redirect::to(uri!(render: id))
} }
@ -61,11 +62,12 @@ fn submit_raw(input: Data, host: HostHeader) -> std::io::Result<String> {
let mut data = String::new(); let mut data = String::new();
input.open().take(1024 * 1000).read_to_string(&mut data)?; input.open().take(1024 * 1000).read_to_string(&mut data)?;
let paste = store_paste(data); let id = generate_id();
store_paste(id.clone(), data);
match *host { match *host {
Some(host) => Ok(format!("https://{}/{}", host, paste)), Some(host) => Ok(format!("https://{}/{}", host, id)),
None => Ok(paste) None => Ok(id)
} }
} }