Prevent cloning paste on show where not necessary
This commit is contained in:
parent
c2219e15f4
commit
5b7868080f
4 changed files with 82 additions and 60 deletions
25
src/io.rs
25
src/io.rs
|
@ -1,5 +1,6 @@
|
|||
extern crate gpw;
|
||||
extern crate linked_hash_map;
|
||||
extern crate owning_ref;
|
||||
extern crate rand;
|
||||
|
||||
use rand::distributions::Alphanumeric;
|
||||
|
@ -7,9 +8,11 @@ use rand::{thread_rng, Rng};
|
|||
|
||||
use linked_hash_map::LinkedHashMap;
|
||||
|
||||
use owning_ref::RwLockReadGuardRef;
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::env;
|
||||
use std::sync::RwLock;
|
||||
use std::sync::{PoisonError, RwLock};
|
||||
|
||||
lazy_static! {
|
||||
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new());
|
||||
|
@ -25,12 +28,12 @@ lazy_static! {
|
|||
///
|
||||
/// During the purge, `ENTRIES` is locked and the current thread will block.
|
||||
fn purge_old() {
|
||||
let entries_len = ENTRIES.read().unwrap_or_else(|p| p.into_inner()).len();
|
||||
let entries_len = ENTRIES.read().unwrap_or_else(PoisonError::into_inner).len();
|
||||
|
||||
if entries_len > *BUFFER_SIZE {
|
||||
let to_remove = entries_len - *BUFFER_SIZE;
|
||||
|
||||
let mut entries = ENTRIES.write().unwrap_or_else(|p| p.into_inner());
|
||||
let mut entries = ENTRIES.write().unwrap_or_else(PoisonError::into_inner);
|
||||
|
||||
for _ in 0..to_remove {
|
||||
entries.pop_front();
|
||||
|
@ -54,17 +57,19 @@ pub fn store_paste(id: String, content: String) {
|
|||
purge_old();
|
||||
ENTRIES
|
||||
.write()
|
||||
.unwrap_or_else(|p| p.into_inner())
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.insert(id, content);
|
||||
}
|
||||
|
||||
/// Get a paste by id.
|
||||
///
|
||||
/// Returns `None` if the paste doesn't exist.
|
||||
pub fn get_paste(id: &str) -> Option<String> {
|
||||
ENTRIES
|
||||
.read()
|
||||
.unwrap_or_else(|p| p.into_inner())
|
||||
.get(id)
|
||||
.cloned()
|
||||
pub fn get_paste(id: &str) -> Option<RwLockReadGuardRef<LinkedHashMap<String, String>, String>> {
|
||||
let or = RwLockReadGuardRef::new(ENTRIES.read().unwrap_or_else(PoisonError::into_inner));
|
||||
|
||||
if or.contains_key(id) {
|
||||
Some(or.map(|x| x.get(id).unwrap()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
31
src/main.rs
31
src/main.rs
|
@ -17,7 +17,7 @@ use highlight::highlight;
|
|||
use io::{generate_id, get_paste, store_paste};
|
||||
use params::{HostHeader, IsPlaintextRequest};
|
||||
|
||||
use askama::{MarkupDisplay, Template};
|
||||
use askama::{Html as AskamaHtml, MarkupDisplay, Template};
|
||||
|
||||
use rocket::http::{ContentType, Status};
|
||||
use rocket::request::Form;
|
||||
|
@ -25,6 +25,7 @@ use rocket::response::content::{Content, Html};
|
|||
use rocket::response::Redirect;
|
||||
use rocket::Data;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::io::Read;
|
||||
|
||||
///
|
||||
|
@ -39,7 +40,7 @@ struct Index;
|
|||
fn index() -> Result<Html<String>, Status> {
|
||||
Index
|
||||
.render()
|
||||
.map(|h| Html(h))
|
||||
.map(Html)
|
||||
.map_err(|_| Status::InternalServerError)
|
||||
}
|
||||
|
||||
|
@ -82,8 +83,8 @@ fn submit_raw(input: Data, host: HostHeader) -> std::io::Result<String> {
|
|||
|
||||
#[derive(Template)]
|
||||
#[template(path = "paste.html")]
|
||||
struct ShowPaste {
|
||||
content: MarkupDisplay<String>,
|
||||
struct ShowPaste<'a> {
|
||||
content: MarkupDisplay<AskamaHtml, Cow<'a, String>>,
|
||||
}
|
||||
|
||||
#[get("/<key>")]
|
||||
|
@ -94,22 +95,24 @@ fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result<Content<Stri
|
|||
|
||||
// get() returns a read-only lock, we're not going to be writing to this key
|
||||
// again so we can hold this for as long as we want
|
||||
let entry = get_paste(key).ok_or_else(|| Status::NotFound)?;
|
||||
let entry = &*get_paste(key).ok_or_else(|| Status::NotFound)?;
|
||||
|
||||
if *plaintext {
|
||||
Ok(Content(ContentType::Plain, entry))
|
||||
Ok(Content(ContentType::Plain, entry.to_string()))
|
||||
} else {
|
||||
let content = match ext {
|
||||
None => MarkupDisplay::Unsafe(entry),
|
||||
Some(extension) => highlight(&entry, extension)
|
||||
.map(|h| MarkupDisplay::Safe(h))
|
||||
.ok_or_else(|| Status::NotFound)?,
|
||||
Some(extension) => match highlight(&entry, extension) {
|
||||
Some(html) => MarkupDisplay::new_safe(Cow::Owned(html), AskamaHtml),
|
||||
None => return Err(Status::NotFound),
|
||||
},
|
||||
None => MarkupDisplay::new_unsafe(Cow::Borrowed(entry), AskamaHtml),
|
||||
};
|
||||
|
||||
ShowPaste { content }
|
||||
.render()
|
||||
.map(|html| Content(ContentType::HTML, html))
|
||||
.map_err(|_| Status::InternalServerError)
|
||||
let template = ShowPaste { content };
|
||||
match template.render() {
|
||||
Ok(html) => Ok(Content(ContentType::HTML, html)),
|
||||
Err(_) => Err(Status::InternalServerError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue