pastes: validate URLs using regex

Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
sphericalkat committed 2020-09-24 22:45:48 +05:30
1 parent 8ac5b3ca8b
commit c075068649
3 files changed
+5 -18

No files matched your search

+3 -1
View File
@@ -1,11 +1,13 @@
use anyhow::Result;
use diesel::pg::PgConnection;
use regex::Regex;
use super::entity::Paste;
use super::postgres;
pub fn create_paste(paste: &mut Paste, conn: &PgConnection) -> Result<usize> {
paste.is_url = Some(validator::validate_url(paste.content.clone()));
let re = Regex::new("^(https?://)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(:\\d+)?(/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(#[-a-z\\d_]*)?$").unwrap();
paste.is_url = Some(re.is_match(&*paste.content.clone()));
postgres::create_paste(paste, conn)
}