diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 2c9370f..76e34df 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -25,4 +25,5 @@ pub mod list_unread_conversations_api; pub mod global_search_result_api; pub mod res_create_group; pub mod group_api; -pub mod advanced_group_api; \ No newline at end of file +pub mod advanced_group_api; +pub mod res_change_group_logo; \ No newline at end of file diff --git a/src/api_data/res_change_group_logo.rs b/src/api_data/res_change_group_logo.rs new file mode 100644 index 0000000..cec32cf --- /dev/null +++ b/src/api_data/res_change_group_logo.rs @@ -0,0 +1,22 @@ +//! # Change group logo result +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::utils::user_data_utils::user_data_url; + +#[derive(Serialize)] +pub struct ResChangeGroupLogo { + success: String, + url: String, +} + +impl ResChangeGroupLogo { + /// Construct a new instance of this structure + pub fn new(path: &str) -> ResChangeGroupLogo { + ResChangeGroupLogo { + success: "Group logo has been successfully updated!".to_string(), + url: user_data_url(path), + } + } +} \ No newline at end of file diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index c540ba4..554eeec 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -6,7 +6,9 @@ use std::collections::HashMap; use crate::api_data::advanced_group_api::AdvancedGroupApi; use crate::api_data::group_api::GroupApi; +use crate::api_data::res_change_group_logo::ResChangeGroupLogo; use crate::api_data::res_create_group::GroupCreationResult; +use crate::constants::DEFAULT_GROUP_LOGO; use crate::controllers::routes::RequestResult; use crate::data::group::{Group, GroupAccessLevel, GroupPostsCreationLevel, GroupRegistrationLevel, GroupVisibilityLevel}; use crate::data::group_id::GroupID; @@ -121,5 +123,15 @@ pub fn check_virtual_dir(r: &mut HttpRequestHandler) -> RequestResult { /// Change a group's logo pub fn upload_logo(r: &mut HttpRequestHandler) -> RequestResult { + // TODO : implement this method r.success("implement me") +} + +/// Delete a group's logo +pub fn delete_logo(r: &mut HttpRequestHandler) -> RequestResult { + let group_id = r.post_group_id_with_access("groupID", GroupAccessLevel::ADMIN_ACCESS)?; + + groups_helper::delete_logo(&group_id)?; + + r.set_response(ResChangeGroupLogo::new(DEFAULT_GROUP_LOGO)) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index a1c2ae8..10bfce1 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -146,6 +146,8 @@ pub fn get_routes() -> Vec { Route::post("/groups/upload_logo", Box::new(groups_controller::upload_logo)), + Route::post("/groups/delete_logo", Box::new(groups_controller::delete_logo)), + // Virtual directory controller Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)), diff --git a/src/data/group.rs b/src/data/group.rs index 35b23f6..b91fd6b 100644 --- a/src/data/group.rs +++ b/src/data/group.rs @@ -2,9 +2,11 @@ //! //! Group visibility level +use std::path::PathBuf; + use crate::constants::DEFAULT_GROUP_LOGO; use crate::data::group_id::GroupID; -use crate::utils::user_data_utils::user_data_url; +use crate::utils::user_data_utils::{user_data_path, user_data_url}; #[allow(non_camel_case_types)] #[derive(Eq, PartialEq, Hash, Debug)] @@ -125,6 +127,11 @@ pub struct Group { } impl Group { + /// Check out whether current group has a logo or not + pub fn has_logo(&self) -> bool { + self.logo.is_some() + } + /// Determine the path of the logo to use for this group pub fn get_logo_path(&self) -> &str { match &self.logo { @@ -137,6 +144,15 @@ impl Group { pub fn get_logo_url(&self) -> String { user_data_url(self.get_logo_path()) } + + /// Get file access to the logo + pub fn get_logo_sys_path(&self) -> PathBuf { + if !self.has_logo() { + panic!("This group has no logo!") + } + + user_data_path(self.logo.as_ref().unwrap().as_ref()) + } } #[cfg(test)] diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 9722f65..4cdaf97 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -300,6 +300,30 @@ pub fn set_settings(g: &Group) -> ResultBoxError { .exec() } +/// Set a new path for a logo +pub fn set_logo_path(g: &GroupID, path: Option) -> ResultBoxError { + database::UpdateInfo::new(GROUPS_LIST_TABLE) + .cond_group_id("id", g) + .set_opt_str("path_logo", path) + .exec() +} + +/// Delete the logo of a group +pub fn delete_logo(g: &GroupID) -> ResultBoxError { + let group = get_info(g)?; + + if !group.has_logo() { + return Ok(()); + } + + let logo_path = group.get_logo_sys_path(); + if logo_path.exists() { + std::fs::remove_file(logo_path)?; + } + + set_logo_path(g, None) +} + /// Turn a database entry into a group struct fn db_to_group(row: &database::RowResult) -> ResultBoxError { let group_id = row.get_group_id("id")?;