mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-23 22:09:29 +00:00
Optimized image send process in conversation system
This commit is contained in:
parent
7a0cbc3214
commit
217c6c4213
@ -220,50 +220,25 @@ class conversationsController{
|
|||||||
if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID))
|
if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID))
|
||||||
Rest_fatal_error(401, "Specified user doesn't belongs to the conversation !");
|
Rest_fatal_error(401, "Specified user doesn't belongs to the conversation !");
|
||||||
|
|
||||||
//Check if informations were specified about the new message or not
|
//Check if information were specified about the new message or not
|
||||||
if(!isset($_POST['message']) AND !isset($_POST['image']))
|
if(!isset($_POST['message']))
|
||||||
Rest_fatal_error(401, "Nothing to be sent with the new message !");
|
Rest_fatal_error(401, "New conversation messages must contain a message (can be empty if there is an image) !");
|
||||||
|
|
||||||
//Else extract informations
|
//Else extract informations
|
||||||
$message = (isset($_POST['message']) ? $_POST['message'] : "");
|
$message = (string) (isset($_POST['message']) ? $_POST['message'] : "");
|
||||||
$image = (isset($_POST['image']) ? $_POST['image'] : false);
|
|
||||||
|
//Check for image
|
||||||
|
$image = "";
|
||||||
|
if(check_post_file("image")){
|
||||||
|
|
||||||
|
//Save image and retrieve its URI
|
||||||
|
$image = save_post_image("image", userID, "conversations", 1200, 1200);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//Check message validity
|
//Check message validity
|
||||||
if(!check_string_before_insert($message) && !$image)
|
if(!check_string_before_insert($message) && $image == "")
|
||||||
Rest_fatal_error(401, "Invalid message sending request !");
|
Rest_fatal_error(401, "Invalid message creation request !");
|
||||||
|
|
||||||
//Check for images
|
|
||||||
if($image){
|
|
||||||
//Get image informations
|
|
||||||
$arrayImage = explode(";", $image);
|
|
||||||
if(count($arrayImage) != 2)
|
|
||||||
Rest_fatal_error(400, "Invalid image informations !");
|
|
||||||
|
|
||||||
$image = str_replace("base64,", "", $arrayImage[1]);
|
|
||||||
|
|
||||||
//Try to base64_decode image
|
|
||||||
$decoded_image = base64_decode($image, true);
|
|
||||||
|
|
||||||
//Check for errors
|
|
||||||
if(!$decoded_image)
|
|
||||||
Rest_fatal_error(500, "Couldn't extract image !");
|
|
||||||
|
|
||||||
//Get target folder
|
|
||||||
$targetFolder = prepareFileCreation(userID, "conversations");
|
|
||||||
|
|
||||||
//Generate target file name
|
|
||||||
$targetFileName = $targetFolder.generateNewFileName(path_user_data($targetFolder, true), "png");
|
|
||||||
$relativeFileName = path_user_data($targetFileName, true);
|
|
||||||
|
|
||||||
//Try to resize image
|
|
||||||
if(!reduce_image("string", $relativeFileName, 1200, 1200, "image/png", $decoded_image)){
|
|
||||||
//Returns error
|
|
||||||
Rest_fatal_error(500, "Couldn't resize image !");
|
|
||||||
}
|
|
||||||
|
|
||||||
//Save image URI
|
|
||||||
$image = $targetFileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Insert the new message
|
//Insert the new message
|
||||||
if(!CS::get()->components->conversations->sendMessage(userID, $conversationID, $message, $image))
|
if(!CS::get()->components->conversations->sendMessage(userID, $conversationID, $message, $image))
|
||||||
|
@ -425,10 +425,10 @@ class conversations {
|
|||||||
* @param int $userID The ID of the user inserting the message
|
* @param int $userID The ID of the user inserting the message
|
||||||
* @param int $conversationID The ID of the target conversation
|
* @param int $conversationID The ID of the target conversation
|
||||||
* @param string $message The message to insert
|
* @param string $message The message to insert
|
||||||
* @param Mixed $image_path Optionnal, the path to an image associated with the message
|
* @param string $image_path Optionnal, the path to an image associated with the message (empty string by default)
|
||||||
* @return bool True for a success
|
* @return bool True for a success
|
||||||
*/
|
*/
|
||||||
private function insertMessage(int $userID, int $conversationID, string $message, $image_path = false) : bool{
|
private function insertMessage(int $userID, int $conversationID, string $message, string $image_path = "") : bool{
|
||||||
|
|
||||||
//Prepare values
|
//Prepare values
|
||||||
$tableName = $this->conversationsMessagesTable;
|
$tableName = $this->conversationsMessagesTable;
|
||||||
@ -440,7 +440,7 @@ class conversations {
|
|||||||
);
|
);
|
||||||
|
|
||||||
//Add image path (if required)
|
//Add image path (if required)
|
||||||
if($image_path)
|
if($image_path != "")
|
||||||
$values['image_path'] = $image_path;
|
$values['image_path'] = $image_path;
|
||||||
|
|
||||||
//Try to insert new value in database
|
//Try to insert new value in database
|
||||||
@ -547,10 +547,10 @@ class conversations {
|
|||||||
* @param int $userID The ID of the user sending the message
|
* @param int $userID The ID of the user sending the message
|
||||||
* @param int $conversationID The ID of the target conversation
|
* @param int $conversationID The ID of the target conversation
|
||||||
* @param string $message The message
|
* @param string $message The message
|
||||||
* @param Mixed $image_path Optionnal, define the path to an image associated with the message
|
* @param string $image_path Optionnal, define the path to an image associated with the message (empty string by default)
|
||||||
* @return bool True for a success
|
* @return bool True for a success
|
||||||
*/
|
*/
|
||||||
public function sendMessage(int $userID, int $conversationID, string $message, $image_path = false) : bool{
|
public function sendMessage(int $userID, int $conversationID, string $message, string $image_path = "") : bool{
|
||||||
|
|
||||||
//GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation
|
//GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation
|
||||||
//But it manage all operations (insert message; save image; inform other users; ...)
|
//But it manage all operations (insert message; save image; inform other users; ...)
|
||||||
|
Loading…
Reference in New Issue
Block a user