User can upload a new account image

This commit is contained in:
Pierre 2018-05-01 09:33:52 +02:00
parent 0acce000d6
commit 471ae35dd4
3 changed files with 87 additions and 4 deletions

View File

@ -213,6 +213,31 @@ class SettingsController {
return $this->AccountImageSettingsToAPI($settings);
}
/**
* Set a new account image for the user
*
* @url POST /settings/upload_account_image
*/
public function upload_account_image(){
//Login required
user_login_required();
//Check if it is a valid file
if(!check_post_file("picture"))
Rest_fatal_error(400, "An error occured while receiving image !");
//Try to save image
$file_uri = save_post_image("picture", 0, "avatars", 800, 800);
//Update account image information
if(!components()->accountImage->update(userID, substr(strrchr($file_uri, "/"), 1)))
Rest_fatal_error(500, "An error occured while trying to apply new account image !");
//Success
return array("success" => "The new account image has been successfully saved!");
}
/**
* Turn a GeneralSettings object into a valid API object
*

View File

@ -23,7 +23,7 @@ class AccountImage {
const errorAccountImage = "0Red.png";
/**
* Returns the file of an account image
* Returns the file name of an account image
*
* @param int $userID The ID of the user on which we perform research
* @return string The URL pointing on the account image
@ -138,6 +138,49 @@ class AccountImage {
return $settings;
}
/**
* Update the account image of a user
*
* @param int $userID The ID of the target user
* @param string $fileName The name of the new account image
* @return bool TRUE for a success / FALSE else
*/
public function update(int $userID, string $fileName) : bool {
//First, we must delete previous account image (if any)
if($this->has($userID)){
if(!$this->delete($userID))
return FALSE;
}
//Save image file name into metadata file
return (bool) file_put_contents($this->getPathMetadataFile($userID), $fileName);
}
/**
* Delete user account image
*
* @param int $userID The ID of the target user
* @return bool TRUE for a success / FALSE else
*/
public function delete(int $userID) : bool {
$path = $this->getFileAccountImage($userID);
if($path != ""){
$syspath = path_user_data($this->getPath($userID), TRUE);
if(file_exists($syspath))
unlink($syspath);
//Delete metadata file
unlink($this->getPathMetadataFile($userID));
}
//Success by default
return TRUE;
}
/**
* Get the file to the user account image, if the user has any
*
@ -145,13 +188,25 @@ class AccountImage {
* @return string The path to the user account image, empty string else
*/
private function getFileAccountImage(int $userID) : string {
$fileName = path_user_data(cs()->config->get(self::accountImageDirConfItem)."adresse_avatars/".$userID.".txt", TRUE);
$fileName = $this->getPathMetadataFile($userID);
if(file_exists($fileName))
return file_get_contents($fileName);
else
return "";
}
/**
* Get the system path of the file that contains the name of the account
* image file
*
* @param int $userID The ID of the target user
* @return string The sys path pointing of the file that contains user account
* image
*/
private function getPathMetadataFile(int $userID) : string {
return path_user_data(cs()->config->get(self::accountImageDirConfItem)."adresse_avatars/".$userID.".txt", TRUE);
}
}
//Register class

View File

@ -17,8 +17,11 @@ function prepareFileCreation(int $userID, string $componentName) : string {
//Get user data folder name
$user_data_folder = path_user_data("", true);
//Determine subfolder name
$subfolder = $componentName."/".$userID."/";
//Determine subfolder name (if any)
if($userID != 0)
$subfolder = $componentName."/".$userID."/";
else
$subfolder = $componentName."/";
//Check if folders exists or not
if(!file_exists($user_data_folder.$subfolder)){