2018-09-19 10:48:39 +02:00
|
|
|
use ammonia::{Builder, UrlRelative};
|
2018-06-11 11:43:27 +02:00
|
|
|
use serde::{self, Serialize, Deserialize,
|
|
|
|
Serializer, Deserializer, de::Visitor};
|
|
|
|
use std::{fmt::{self, Display},
|
2018-09-19 10:48:39 +02:00
|
|
|
borrow::{Borrow, Cow}, io::Write,
|
|
|
|
iter, ops::Deref};
|
2018-06-11 11:43:27 +02:00
|
|
|
use diesel::{self, deserialize::Queryable,
|
|
|
|
types::ToSql,
|
|
|
|
sql_types::Text,
|
|
|
|
serialize::{self, Output}};
|
|
|
|
|
2018-09-19 10:48:39 +02:00
|
|
|
lazy_static! {
|
|
|
|
static ref CLEAN: Builder<'static> = {
|
|
|
|
let mut b = Builder::new();
|
|
|
|
b.add_generic_attributes(iter::once("id"))
|
|
|
|
.add_tags(iter::once("iframe"))
|
|
|
|
.id_prefix(Some("postcontent-"))
|
|
|
|
.url_relative(UrlRelative::Custom(Box::new(url_add_prefix)))
|
|
|
|
.add_tag_attributes("iframe",
|
|
|
|
["width", "height", "src", "frameborder"]
|
|
|
|
.iter()
|
|
|
|
.map(|&v| v));
|
|
|
|
b
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn url_add_prefix(url: &str) -> Option<Cow<str>> {
|
|
|
|
if url.starts_with('#') && ! url.starts_with("#postcontent-") {//if start with an #
|
|
|
|
let mut new_url = "#postcontent-".to_owned();//change to valid id
|
|
|
|
new_url.push_str(&url[1..]);
|
|
|
|
Some(Cow::Owned(new_url))
|
|
|
|
} else {
|
|
|
|
Some(Cow::Borrowed(url))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:28:42 +02:00
|
|
|
#[derive(Debug, Clone, AsExpression, FromSqlRow, Default)]
|
2018-06-11 11:43:27 +02:00
|
|
|
#[sql_type = "Text"]
|
|
|
|
pub struct SafeString{
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SafeString{
|
|
|
|
pub fn new(value: &str) -> Self {
|
|
|
|
SafeString{
|
2018-09-19 10:48:39 +02:00
|
|
|
value: CLEAN.clean(&value).to_string(),
|
2018-06-11 11:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn set(&mut self, value: &str) {
|
2018-09-19 10:48:39 +02:00
|
|
|
self.value = CLEAN.clean(value).to_string();
|
2018-06-11 11:43:27 +02:00
|
|
|
}
|
|
|
|
pub fn get(&self) -> &String {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for SafeString {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where S: Serializer, {
|
|
|
|
serializer.serialize_str(&self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SafeStringVisitor;
|
|
|
|
|
|
|
|
impl<'de> Visitor<'de> for SafeStringVisitor {
|
|
|
|
type Value = SafeString;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter:&mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, value: &str) -> Result<SafeString, E>
|
|
|
|
where E: serde::de::Error{
|
|
|
|
Ok(SafeString::new(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for SafeString {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where D: Deserializer<'de>, {
|
|
|
|
Ok(
|
|
|
|
deserializer.deserialize_string(SafeStringVisitor)?
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:58:15 +02:00
|
|
|
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
|
2018-06-11 11:43:27 +02:00
|
|
|
impl Queryable<Text, diesel::pg::Pg> for SafeString {
|
|
|
|
type Row = String;
|
|
|
|
fn build(value: Self::Row) -> Self {
|
|
|
|
SafeString::new(&value)
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 23:06:40 +02:00
|
|
|
|
2018-10-08 19:58:15 +02:00
|
|
|
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
|
2018-09-27 23:06:40 +02:00
|
|
|
impl Queryable<Text, diesel::sqlite::Sqlite> for SafeString {
|
|
|
|
type Row = String;
|
|
|
|
fn build(value: Self::Row) -> Self {
|
|
|
|
SafeString::new(&value)
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 19:58:15 +02:00
|
|
|
|
2018-06-11 11:43:27 +02:00
|
|
|
|
|
|
|
impl<DB> ToSql<diesel::sql_types::Text, DB> for SafeString
|
|
|
|
where
|
|
|
|
DB: diesel::backend::Backend,
|
|
|
|
str: ToSql<diesel::sql_types::Text, DB>, {
|
|
|
|
fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> serialize::Result {
|
|
|
|
str::to_sql(&self.value, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Borrow<str> for SafeString {
|
|
|
|
fn borrow(&self) -> &str {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for SafeString {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for SafeString {
|
|
|
|
type Target = str;
|
|
|
|
fn deref(&self) -> &str {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for SafeString {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 18:26:42 +02:00
|
|
|
|
|
|
|
use rocket::request::FromFormValue;
|
|
|
|
use rocket::http::RawStr;
|
|
|
|
|
|
|
|
impl<'v> FromFormValue<'v> for SafeString {
|
|
|
|
type Error = &'v RawStr;
|
|
|
|
|
|
|
|
fn from_form_value(form_value: &'v RawStr) -> Result<SafeString, &'v RawStr> {
|
|
|
|
let val = String::from_form_value(form_value)?;
|
2018-09-14 19:50:59 +02:00
|
|
|
Ok(SafeString::new(&val))
|
2018-09-14 18:26:42 +02:00
|
|
|
}
|
|
|
|
}
|