diff --git a/Cargo.lock b/Cargo.lock index 0cade06..cdd5f31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -545,7 +545,7 @@ dependencies = [ "traitobject", "typeable", "unicase", - "url", + "url 1.7.2", ] [[package]] @@ -559,6 +559,17 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "idna" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "indexmap" version = "1.3.2" @@ -646,6 +657,7 @@ dependencies = [ "slog-async", "slog-term", "uuid", + "validator", ] [[package]] @@ -1493,11 +1505,22 @@ version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" dependencies = [ - "idna", + "idna 0.1.5", "matches", "percent-encoding 1.0.1", ] +[[package]] +name = "url" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" +dependencies = [ + "idna 0.2.0", + "matches", + "percent-encoding 2.1.0", +] + [[package]] name = "uuid" version = "0.8.1" @@ -1508,6 +1531,21 @@ dependencies = [ "serde", ] +[[package]] +name = "validator" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e60fadf92c22236de4028ceb0b8af50ed3430d41ad43d7a7d63b6bd1a8f47c38" +dependencies = [ + "idna 0.2.0", + "lazy_static", + "regex", + "serde", + "serde_derive", + "serde_json", + "url 2.1.1", +] + [[package]] name = "vcpkg" version = "0.2.10" diff --git a/Cargo.toml b/Cargo.toml index 2bbb99c..637a410 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ jsonwebtoken = "7.1.2" slog = "2.5.2" slog-term = "2.6.0" anyhow = "1.0" +validator = "0.10.1" slog-async = "2.5.0" bcrypt = "0.8" chrono = "0.4.11" diff --git a/src/api/routes/paste.rs b/src/api/routes/paste.rs index 0e51e3b..8ab6678 100644 --- a/src/api/routes/paste.rs +++ b/src/api/routes/paste.rs @@ -5,10 +5,11 @@ use rocket::response::status::Custom; use rocket_contrib::json::Json; use serde_json::Value; -use crate::core::paste::{entity::Paste, service::create_paste}; +use crate::core::paste::{entity::Paste, service::create_paste, service::fetch_paste}; use crate::core::users::{service::create_or_fetch_user}; use crate::utils::{db, phonetic_key}; use crate::utils::phonetic_key::get_random_id; +use diesel::result::Error; #[post("/", data = "")] fn create(mut paste: Json, conn: db::DbConn, mut ck: Cookies) -> Custom> { @@ -54,6 +55,27 @@ fn create(mut paste: Json, conn: db::DbConn, mut ck: Cookies) -> Custom")] +fn fetch(id: String, conn: db::DbConn) -> Custom> { + let paste = match fetch_paste(id, &conn) { + Ok(paste) => paste, + Err(err) => { + return match err.downcast_ref::() { + Some(Error::NotFound) => Custom(Status::NotFound, Json(json!({ + "err": err.to_string(), + "msg": "Unable to find a paste with that ID" + }))), + _ => Custom(Status::InternalServerError, Json(json!({ + "err": err.to_string(), + "msg": "Something went wrong, try again" + }))) + } + } + }; + + Custom(Status::Found, Json(json!(paste))) +} + pub fn fuel(rocket: Rocket) -> Rocket { - rocket.mount("/api/paste", routes![create]) + rocket.mount("/api/paste", routes![create, fetch]) } \ No newline at end of file diff --git a/src/core/paste/postgres.rs b/src/core/paste/postgres.rs index 45cab94..2daf29a 100644 --- a/src/core/paste/postgres.rs +++ b/src/core/paste/postgres.rs @@ -1,6 +1,6 @@ use anyhow::Result; use diesel::pg::PgConnection; -use diesel::RunQueryDsl; +use diesel::prelude::*; use crate::schema::pastes; @@ -11,4 +11,9 @@ pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result { .values(paste) .execute(conn)?; Ok(rows) +} + +pub fn fetch_paste(id: String, conn: &PgConnection) -> Result { + let paste = pastes::table.find(id).get_result::(conn)?; + Ok(paste) } \ No newline at end of file diff --git a/src/core/paste/service.rs b/src/core/paste/service.rs index 5ad7f2c..3a6563d 100644 --- a/src/core/paste/service.rs +++ b/src/core/paste/service.rs @@ -4,6 +4,11 @@ use diesel::pg::PgConnection; use super::entity::Paste; use super::postgres; -pub fn create_paste(paste: &Paste, conn: &PgConnection) -> Result { +pub fn create_paste(paste: &mut Paste, conn: &PgConnection) -> Result { + paste.is_url = validator::validate_url(paste.content.clone()); postgres::create_paste(paste, conn) +} + +pub fn fetch_paste(id: String, conn: &PgConnection) -> Result { + postgres::fetch_paste(id, conn) } \ No newline at end of file diff --git a/src/schema.rs b/src/schema.rs index 260832c..33c788f 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -1,6 +1,6 @@ table! { pastes (id) { - id -> Varchar, + id -> Nullable, belongs_to -> Nullable, is_url -> Bool, content -> Text,