1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-20 16:35:17 +00:00

Add support for multiple files types

This commit is contained in:
2021-03-06 09:35:36 +01:00
parent dc93d58d6b
commit afcce8463f
11 changed files with 377 additions and 35 deletions

View File

@ -2,16 +2,15 @@
//!
//! @author Pierre Hubert
use crate::api_data::conversation_api::ConversationAPI;
use crate::api_data::conversation_message_api::ConversationMessageAPI;
use crate::api_data::list_unread_conversations_api::UnreadConversationAPI;
use crate::api_data::res_count_unread_conversations::ResultCountUnreadConversations;
use crate::api_data::res_create_conversation::ResCreateConversation;
use crate::api_data::res_find_private_conversations::ResFindPrivateConversations;
use crate::constants::{MAX_CONVERSATION_MESSAGE_LENGTH, MIN_CONVERSATION_MESSAGE_LENGTH};
use crate::constants::{ALLOWED_CONVERSATION_FILES_TYPES, CONVERSATION_FILES_MAX_SIZE, MAX_CONVERSATION_MESSAGE_LENGTH, MIN_CONVERSATION_MESSAGE_LENGTH};
use crate::controllers::user_ws_controller;
use crate::data::base_request_handler::BaseRequestHandler;
use crate::data::base_request_handler::{BaseRequestHandler, RequestValue};
use crate::data::conversation::{ConversationMemberSetting, NewConversationSettings};
use crate::data::conversation_message::ConversationMessageFile;
use crate::data::error::Res;
@ -25,6 +24,7 @@ use crate::helpers::{conversations_helper, events_helper, user_helper};
use crate::helpers::events_helper::Event;
use crate::routes::RequestResult;
use crate::utils::string_utils::remove_html_nodes;
use crate::utils::user_data_utils::{delete_user_data_file_if_exists, user_data_path};
/// Create a new conversation
pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
@ -251,22 +251,100 @@ pub fn send_message(r: &mut HttpRequestHandler) -> RequestResult {
// TODO : add support for other files type
// Get image
let file = match r.has_file("image") {
false => None,
true => {
let path = r.save_post_image("image", "conversations", 1200, 1200)?;
// Get associated file
let file = match r.post_parameter_opt("file") {
Some(RequestValue::File(file)) => {
// File name
let mut name = file.name.to_string();
if file.buff.len() > CONVERSATION_FILES_MAX_SIZE {
r.bad_request("File is too big!".to_string())?;
}
// Determine file mime type
let mut mime_type = r.post_file_type("file")?;
// Check for thumbnail
let mut thumbnail = match r.has_file("thumbnail") {
false => None,
true => Some("thumbnail".to_string())
};
let path;
if !ALLOWED_CONVERSATION_FILES_TYPES.contains(&mime_type.as_str()) {
r.bad_request("File type is not allowed!".to_string())?;
}
// Images
if mime_type.starts_with("image/") {
if let None = thumbnail {
thumbnail = Some("file".to_string());
}
path = r.save_post_image("file", "conversation", 2000, 2000)?;
mime_type = "image/png".to_string();
name = "picture.png".to_string();
}
// PDF
else if mime_type.eq("application/pdf") {
path = r.save_post_pdf("file", "conversation")?;
}
// MP3
else if mime_type.eq("audio/mpeg") {
path = r.save_post_mp3("file", "conversation")?;
}
// MP4
else if mime_type.eq("video/mp4") {
path = r.save_post_mp4("file", "conversation")?;
}
// ZIP archive
else if mime_type.eq("application/zip") {
path = r.save_post_zip("file", "conversation")?;
}
// Office document
else if mime_type.starts_with("application/") {
path = r.save_post_office_doc("file", "conversation")?;
}
// Text files
else {
path = r.save_post_txt_doc("file", "conversation")?;
}
// Attempt to save thumbnail, if it fails we can not save message
let thumbnail = match thumbnail {
None => None,
Some(f) => Some(match r.save_post_image(&f, "conversations-thumb", 200, 200) {
Ok(s) => Ok(s),
Err(e) => {
eprintln!("Failed to save conversation thumbnail! {:#?}", e);
delete_user_data_file_if_exists(&path).unwrap();
Err(e)
}
}?)
};
Some(ConversationMessageFile {
path: path.clone(),
size: std::fs::metadata(&path)?.len(),
name: "picture.png".to_string(),
thumbnail: Some(r.save_post_image("image", "conversations", 50, 50)?),
r#type: "image/png".to_string(),
size: std::fs::metadata(user_data_path(path.as_ref()))?.len(),
name,
thumbnail,
r#type: mime_type,
})
}
_ => None,
};
// Get message, if there is no image
// Get message, if there is no file
let message = if let None = file {
let msg = r.post_string_without_html("message", MIN_CONVERSATION_MESSAGE_LENGTH, true)?;