diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 14d931d..062ae94 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -12,7 +12,7 @@ use crate::data::http_request_handler::HttpRequestHandler; use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel}; use crate::helpers::{groups_helper, posts_helper, user_helper}; use crate::utils::date_utils::time; -use crate::utils::string_utils::check_string_before_insert; +use crate::utils::string_utils::{check_string_before_insert, check_youtube_id}; use crate::utils::user_data_utils::user_data_path; impl PostFile { @@ -141,6 +141,18 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult { PostKind::POST_KIND_IMAGE(PostFile::new_from_created_file(&path)?) } + "youtube" => { + let youtube = r.post_string("youtube_id")?; + + if !check_youtube_id(&youtube) { + r.bad_request("Invalid YouTube ID!".to_string())?; + } + + PostKind::POST_KIND_YOUTUBE(youtube) + } + + // TODO : add support for next types + _ => { r.internal_error(ExecError::boxed_new("Unsupported kind of post!"))?; unreachable!(); diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 3a74bdd..c488def 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -75,7 +75,7 @@ pub fn create(p: &Post) -> ResultBoxError { .add_opt_str("texte", p.content.as_ref()); match &p.kind { - PostKind::POST_KIND_TEXT => {/* nothing to do */}, + PostKind::POST_KIND_TEXT => { /* nothing to do */ } // Posts with associated file POST_KIND_IMAGE(file) | POST_KIND_PDF(file) => { @@ -84,6 +84,11 @@ pub fn create(p: &Post) -> ResultBoxError { .add_opt_str("file_type", file.file_type.as_ref()); } + // YouTube posts + POST_KIND_YOUTUBE(id) => { + insert_query = insert_query.add_str("path", id) + .add_str("type", "youtube"); + } _ => unimplemented!() /* @@ -91,7 +96,7 @@ pub fn create(p: &Post) -> ResultBoxError { POST_KIND_MOVIE(_) => {}, POST_KIND_COUNTDOWN(_) => {}, POST_KIND_SURVEY => {}, - POST_KIND_YOUTUBE(_) => {},*/ + */ } // Execute insertion diff --git a/src/utils/string_utils.rs b/src/utils/string_utils.rs index 7cc745b..141a37b 100644 --- a/src/utils/string_utils.rs +++ b/src/utils/string_utils.rs @@ -50,4 +50,25 @@ pub fn check_url(url: &str) -> bool { /// ``` pub fn check_string_before_insert(s: &str) -> bool { s.trim().len() > 3 +} + +/// Check the validity of a YouTube ID +/// +/// ``` +/// use comunic_server::utils::string_utils::check_youtube_id; +/// +/// assert_eq!(check_youtube_id("/ab/"), false); +/// assert_eq!(check_youtube_id("abxZ96C"), true); +/// assert_eq!(check_youtube_id("a6C"), false); +/// ``` +pub fn check_youtube_id(id: &str) -> bool { + id.len() >= 5 + && !id.contains("/") + && !id.contains("\\") + && !id.contains("@") + && !id.contains("&") + && !id.contains("?") + && !id.contains(".") + && !id.contains("'") + && !id.contains("\"") } \ No newline at end of file