🏡 index : ~doyle/bin.git

author Jordan Johnson-Doyle <jordan@doyle.la> 2019-02-15 21:01:37.0 +00:00:00
committer Jordan Johnson-Doyle <jordan@doyle.la> 2019-02-15 21:01:37.0 +00:00:00
commit
e689ff7714607b073c78d16b4c56ac981c9cb45d [patch]
tree
d5d8e9a59ad0d558f3b22c0bd7159e25b5d3d732
parent
d6fa8ad7b05d304ef7eed3e64c059a67f2604fbc
download
e689ff7714607b073c78d16b4c56ac981c9cb45d.tar.gz

Separate id generation and persisting



Diff

 src/io.rs   | 15 ++++++++++-----
 src/main.rs | 12 ++++++++++--
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/src/io.rs b/src/io.rs
index f72783a..2f56676 100644
--- a/src/io.rs
+++ a/src/io.rs
@@ -30,17 +30,16 @@
    }
}

/// Generates a randomly generated id, stores the given paste under that id and then returns the id.

///

/// Uses gpw to generate a (most likely) pronounceable URL.

pub fn store_paste(content: String) -> String {
/// Generates a 'pronounceable' random ID using gpw

pub fn generate_id() -> String {
    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();
    ENTRIES.write().unwrap().insert(id.clone(), content);

    id
    ENTRIES.write().unwrap().insert(id, content);
}

/// Get a paste by id.

diff --git a/src/main.rs b/src/main.rs
index c8e08f0..b75210b 100644
--- a/src/main.rs
+++ a/src/main.rs
@@ -12,7 +12,7 @@
mod highlight;
mod params;

use io::{store_paste, get_paste};
use io::{store_paste, get_paste, generate_id};
use highlight::highlight;
use params::{IsPlaintextRequest, HostHeader};

@@ -52,7 +52,8 @@

#[post("/", data = "<input>")]
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))
}

@@ -61,11 +62,12 @@
    let mut data = String::new();
    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 {
        Some(host) => Ok(format!("https://{}/{}", host, paste)),
        None => Ok(paste)
        Some(host) => Ok(format!("https://{}/{}", host, id)),
        None => Ok(id)
    }
}