mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-27 07:49:27 +00:00
Accept conversation messages with images
This commit is contained in:
parent
898cbc2e41
commit
612050a1bc
@ -242,10 +242,41 @@ class conversationsController{
|
|||||||
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 sending request !");
|
||||||
|
|
||||||
//Process images NOT IMPLEMENTED YET
|
//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))
|
if(!CS::get()->components->conversations->sendMessage(userID, $conversationID, $message, $image))
|
||||||
Rest_fatal_error(500, "Couldn't send the message !");
|
Rest_fatal_error(500, "Couldn't send the message !");
|
||||||
|
|
||||||
Rest_fatal_error("200", "All right now");
|
Rest_fatal_error("200", "All right now");
|
||||||
|
@ -423,9 +423,10 @@ class conversations {
|
|||||||
* @param Integer $userID The ID of the user inserting the message
|
* @param Integer $userID The ID of the user inserting the message
|
||||||
* @param Integer $conversationID The ID of the target conversation
|
* @param Integer $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
|
||||||
* @return Boolean True for a success
|
* @return Boolean True for a success
|
||||||
*/
|
*/
|
||||||
private function insertMessage($userID, $conversationID, $message){
|
private function insertMessage($userID, $conversationID, $message, $image_path = false){
|
||||||
|
|
||||||
//Prepare values
|
//Prepare values
|
||||||
$tableName = $this->conversationMessagesTable;
|
$tableName = $this->conversationMessagesTable;
|
||||||
@ -436,6 +437,10 @@ class conversations {
|
|||||||
"message" => $message
|
"message" => $message
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//Add image path (if required)
|
||||||
|
if($image_path)
|
||||||
|
$values['image_path'] = $image_path;
|
||||||
|
|
||||||
//Try to insert new value in database
|
//Try to insert new value in database
|
||||||
if(!CS::get()->db->addLine($tableName, $values))
|
if(!CS::get()->db->addLine($tableName, $values))
|
||||||
return false; //An error occured
|
return false; //An error occured
|
||||||
@ -510,15 +515,16 @@ class conversations {
|
|||||||
* @param Integer $userID The ID of the user sending the message
|
* @param Integer $userID The ID of the user sending the message
|
||||||
* @param Integer $conversationID The ID of the target conversation
|
* @param Integer $conversationID The ID of the target conversation
|
||||||
* @param String $message The message
|
* @param String $message The message
|
||||||
|
* @param Mixed $image_path Optionna, define the path to an image associated with the message
|
||||||
* @return Boolean True for a success
|
* @return Boolean True for a success
|
||||||
*/
|
*/
|
||||||
public function sendMessage($userID, $conversationID, $message){
|
public function sendMessage($userID, $conversationID, $message, $image_path = false){
|
||||||
|
|
||||||
//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; ...)
|
||||||
|
|
||||||
//First, try to insert the message
|
//First, try to insert the message
|
||||||
if(!$this->insertMessage($userID, $conversationID, $message))
|
if(!$this->insertMessage($userID, $conversationID, $message, $image_path))
|
||||||
return false; //An error occured
|
return false; //An error occured
|
||||||
|
|
||||||
//Then, update the last activity of the conversation
|
//Then, update the last activity of the conversation
|
||||||
|
153
functions/files.php
Normal file
153
functions/files.php
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Files functions
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare a file creation
|
||||||
|
*
|
||||||
|
* @param Integer $userID The ID of the user owning the file
|
||||||
|
* @param String $componentName The name of the target component
|
||||||
|
* @return String the file path (based on user_data folder)
|
||||||
|
*/
|
||||||
|
function prepareFileCreation($userID, $componentName){
|
||||||
|
|
||||||
|
//Get user data folder name
|
||||||
|
$user_data_folder = path_user_data("", true);
|
||||||
|
|
||||||
|
//Determine subfolder name
|
||||||
|
$subfolder = $componentName."/".$userID."/";
|
||||||
|
|
||||||
|
//Check if folders exists or not
|
||||||
|
if(!file_exists($user_data_folder.$subfolder)){
|
||||||
|
//Create folders recursively
|
||||||
|
mkdir($user_data_folder.$subfolder, 0777, true);
|
||||||
|
|
||||||
|
//Create security file (empty index.html file)
|
||||||
|
file_put_contents($user_data_folder.$subfolder."index.html", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Return folder
|
||||||
|
return $subfolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a file name for a new file
|
||||||
|
*
|
||||||
|
* @param String $directory The target directory
|
||||||
|
* @param String $extension The file extension
|
||||||
|
* @return String The generated file name
|
||||||
|
*/
|
||||||
|
function generateNewFileName($directory, $extension){
|
||||||
|
|
||||||
|
//First, check if folder exists
|
||||||
|
if(!file_exists($directory))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
//Generate a random filename
|
||||||
|
do {
|
||||||
|
//Generate a new filename
|
||||||
|
$fileName = random_str(25).".".$extension;
|
||||||
|
}
|
||||||
|
while(file_exists($directory.$fileName));
|
||||||
|
|
||||||
|
//Return the generated filename
|
||||||
|
return $fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reduces an image size
|
||||||
|
*
|
||||||
|
* @param String $fileName The name of the image to reduce
|
||||||
|
* @param String $targetFile The target of the reduced image
|
||||||
|
* @param Integer $maxWidth The maximal width of the image
|
||||||
|
* @param Integer $maxHeight The maximal height of the image
|
||||||
|
* @param String $outFormat The output format
|
||||||
|
* @param String $img_string Optionnal, the image string if it is a string
|
||||||
|
* @return Boolean True for a success / False for a failure
|
||||||
|
*/
|
||||||
|
function reduce_image($fileName, $targetFile, $maxWidth, $maxHeight, $outFormat = "image/png", $img_string=""){
|
||||||
|
|
||||||
|
//Check if we have to reduce physical image or an image contained in a variable
|
||||||
|
if($fileName != "string"){
|
||||||
|
|
||||||
|
//Check if image exists or not
|
||||||
|
if(!file_exists($fileName))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
//Try to get image size
|
||||||
|
if(!$imageInfos = getimagesize($fileName))
|
||||||
|
return false; //File doesn't seems to be an image
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//Get informations about the image in the string
|
||||||
|
if(!$imageInfos = getimagesizefromstring($img_string))
|
||||||
|
return false; //Couldn't create image
|
||||||
|
}
|
||||||
|
|
||||||
|
//Extract image width and height
|
||||||
|
$width = $imageInfos[0];
|
||||||
|
$height = $imageInfos[1];
|
||||||
|
|
||||||
|
//Check image size
|
||||||
|
if($width == 0 || $height == 0)
|
||||||
|
return false; //Can't process such image
|
||||||
|
|
||||||
|
//Try to open image
|
||||||
|
if($fileName === "string")
|
||||||
|
$src = imagecreatefromstring($img_string);
|
||||||
|
elseif($imageInfos['mime'] === "image/png")
|
||||||
|
$src = imagecreatefrompng($fileName);
|
||||||
|
elseif($imageInfos['mime'] === "image/jpeg")
|
||||||
|
$src = imagecreatefromjpeg($fileName);
|
||||||
|
elseif($imageInfos['mime'] === "image/gif")
|
||||||
|
$src = imagecreatefromgif($fileName);
|
||||||
|
elseif($imagesInfos['mime'] === "image/x-ms-bmp")
|
||||||
|
$src = imagecreatefrombmp($fileName);
|
||||||
|
else
|
||||||
|
return false; //Unrecognized image type
|
||||||
|
|
||||||
|
//Check if image size can be kept as is
|
||||||
|
if($width <= $maxWidth AND $height <= $maxHeight){
|
||||||
|
//We keep the same dimensions
|
||||||
|
$newWidth = $width;
|
||||||
|
$newHeight = $height;
|
||||||
|
}
|
||||||
|
elseif($width > $maxWidth){
|
||||||
|
$newWidth = $maxWidth;
|
||||||
|
$newHeight = floor(($height*$maxWidth)/$width);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$newHeight = $maxHeight;
|
||||||
|
$newWidth = floor(($width*$maxHeight)/$height);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create reduced image
|
||||||
|
$dest = imagecreatetruecolor($newWidth, $newHeight);
|
||||||
|
|
||||||
|
//Copy image
|
||||||
|
imagecopyresized($dest, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
||||||
|
|
||||||
|
//Try to export image
|
||||||
|
//PNG
|
||||||
|
if($outFormat === "image/png"){
|
||||||
|
if(!imagepng($dest, $targetFile, 2))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//JPEG
|
||||||
|
elseif($outFormat === "image/jpeg"){
|
||||||
|
if(!imagejpeg($dest, $targetFile, 2))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//UNSUPORTED
|
||||||
|
else
|
||||||
|
return false; //Unkown export format
|
||||||
|
|
||||||
|
//Success
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user