Rev 17012 | Rev 17044 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpnamespace LeadersLinked\Library;use LeadersLinked\Model\User;use LeadersLinked\Model\UserProfile;use LeadersLinked\Model\Company;use LeadersLinked\Model\Group;class Storage{const TYPE_CHAT = 'chat';const TYPE_GROUP = 'group';const TYPE_USER = 'user';const TYPE_IMAGE = 'image';const TYPE_JOB = 'job';const TYPE_COMPANY = 'company';const TYPE_FEED = 'feed';CONST TYPE_POST = 'post';const TYPE_MICROLEARNING_TOPIC = 'topic';const TYPE_MICROLEARNING_CAPSULE = 'capsule';const TYPE_MICROLEARNING_SLIDE = 'slide';const TYPE_JOB_DESCRIPTION = 'jobdesc';const TYPE_SELF_EVALUATION = 'selfval';const TYPE_PERFORMANCE_EVALUATION = 'perfeva';const TYPE_RECRUITMENT_SELECTION = 'recrsel';const TYPE_PLANNING_OBJECTIVES_AND_GOALS = 'plannig';const TYPE_MESSAGE = 'message';const TYPE_SURVEY = 'survey';const TYPE_NETWORK = 'network';const TYPE_DAILY_PULSE = 'dailpuls';const TYPE_ENGAGEMENT_REWARD = 'engarewr';const TYPE_KNOWLEDGE_AREA = 'knowarea';const TYPE_MY_COACH = 'mycoach';const TYPE_HABIT_EMOJI = 'habit-emoji';const TYPE_HABIT_CONTENT = 'habit-content';const BASE_PATH = 'data' . DIRECTORY_SEPARATOR . 'storage';/**** @var\LeadersLinked\Library\Storage*/private static $_instance;/**** @var array*/private $config;/**** @var \Laminas\Db\Adapter\AdapterInterface*/private $adapter;/**** @param array $config* @param \Laminas\Db\Adapter\AdapterInterface $adapter*/private function __construct($config, $adapter){$this->config = $config;$this->adapter = $adapter;}/**** @param array $config* @param \Laminas\Db\Adapter\AdapterInterface $adapter* @return \LeadersLinked\Library\Storage*/public static function getInstance($config, $adapter){if(self::$_instance == null) {self::$_instance = new Storage($config, $adapter);}return self::$_instance;}/**** @param User $user* @return string*/public function getUserImage($user){if($user->image) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathUser() . DIRECTORY_SEPARATOR . $user->uuid . DIRECTORY_SEPARATOR . $user->image;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.user_image'];return $this->getPresignedUrl($remoto);}/**** @param User $user* @return string*/public function getUserImageForCodeAndFilename($code, $filename){if($filename) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathUser() . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.user_image'];return $this->getPresignedUrl($remoto);}/**** @param User $user* @param UserProfile $userProfile* @return string*/public function getUserProfileImage($user, $userProfile){if($userProfile->image) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathUser() . DIRECTORY_SEPARATOR . $user->uuid . DIRECTORY_SEPARATOR . $userProfile->image;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.user_profile'];return $this->getPresignedUrl($remoto);}/**** @param User $user* @param UserProfile $userProfile* @return string*/public function getUserProfileCover($user, $userProfile){if($userProfile->cover) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathUser() . DIRECTORY_SEPARATOR . $user->uuid . DIRECTORY_SEPARATOR . $userProfile->cover;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.user_cover'];return $this->getPresignedUrl($remoto);}/**** @param Company $company* @return string*/public function getCompanyImage($company){if($company->image) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathCompany() . DIRECTORY_SEPARATOR . $company->uuid . DIRECTORY_SEPARATOR . $company->image;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.company_profile'];return $this->getPresignedUrl($remoto);}/**** @param string $code* @param string $filename* @return string*/public function getCompanyImageForCodeAndFilename($code, $filename){if($filename) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathCompany() . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.company_profile'];return $this->getPresignedUrl($remoto);}/**** @param Company $company* @return string*/public function getCompanyCover($company){if($company->cover) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathCompany() . DIRECTORY_SEPARATOR . $company->uuid . DIRECTORY_SEPARATOR . $company->cover;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.company_cover'];return $this->getPresignedUrl($remoto);}/**** @param Group $group* @return string*/public function getGroupImage($group){if($group->image) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathGroup() . DIRECTORY_SEPARATOR . $group->uuid . DIRECTORY_SEPARATOR . $group->image;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.group_profile'];return $this->getPresignedUrl($remoto);}/**** @param string $code* @param string $filename* @return string*/public function getGroupImageForCodeAndFilename($code, $filename){if($filename) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR .$this->getPathGroup() . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.group_profile'];return $this->getPresignedUrl($remoto);}/**** @param Group $group* @return string*/public function getGroupCover($group){if($group->cover) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR .$this->getPathGroup() . DIRECTORY_SEPARATOR . $group->uuid . DIRECTORY_SEPARATOR . $group->cover;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);}}$remoto = $this->config['leaderslinked.images_default.group_cover'];return $this->getPresignedUrl($remoto);}/**** @param Group $group* @return string*/public function getGenericImage($path, $code, $filename){$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR .$path . DIRECTORY_SEPARATOR . $code. DIRECTORY_SEPARATOR . $filename;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);} else {$remoto = $this->config['leaderslinked.images_default.no_image'];return $this->getPresignedUrl($remoto);}}/**** @param string $path* @param string $code,* @param string $filename* @param boolean $checkExists* @return string*/public function getFileFromDisk($path, $code, $filename){$current_file = self::BASE_PATH . DIRECTORY_SEPARATOR .$path . DIRECTORY_SEPARATOR . $code. DIRECTORY_SEPARATOR . $filename;if(file_exists($current_file)) {return $current_file;}return;}/**** @param string $path* @param string $code,* @return string*/public function getDirectoryFromDisk($path, $code){$directory = self::BASE_PATH . DIRECTORY_SEPARATOR .$path . DIRECTORY_SEPARATOR . $code;if(file_exists($directory)) {return $directory;}return;}/**** @param string $path* @param string $code,* @param string $filename* @param boolean $checkExists* @return string*/public function getGenericFile($path, $code, $filename, $checkExists = true){$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR .$path . DIRECTORY_SEPARATOR . $code. DIRECTORY_SEPARATOR . $filename;if($checkExists) {if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);} else {return;}}return $this->getPresignedUrl($remoto);}public function getPathByType($type){switch($type){case TYPE_CHAT :return $this->getPathChat();case TYPE_GROUP :return $this->getPathGroup();case TYPE_USER :return $this->getPathUser();case TYPE_IMAGE :return $this->getPathImage();case TYPE_JOB :return $this->getPathJob();case TYPE_COMPANY :return $this->getPathCompany();case TYPE_FEED :return $this->getPathFeed();case TYPE_POST :return $this->getPathPost();case TYPE_MICROLEARNING_TOPIC :return $this->getPathMicrolearningTopic();case TYPE_MICROLEARNING_CAPSULE :return $this->getPathMicrolearningCapsule();case TYPE_MICROLEARNING_SLIDE :return $this->getPathMicrolearningSlide();case TYPE_JOB_DESCRIPTION :return $this->getPathJobDescription();case TYPE_SELF_EVALUATION :return $this->getPathSelfEvaluation();case TYPE_PERFORMANCE_EVALUATION :return $this->getPathPerformanceEvaluation();case TYPE_RECRUITMENT_SELECTION :return $this->getPathRecruitmentSelection();case TYPE_PLANNING_OBJECTIVES_AND_GOALS :return $this->getPathPlanningObjectivesAnGoals();case TYPE_MESSAGE :return $this->getPathMessage();case TYPE_SURVEY :return $this->getPathSurvey();case TYPE_NETWORK :return $this->getPathNetwork();case TYPE_DAILY_PULSE :return $this->getPathDailyPulse();case TYPE_ENGAGEMENT_REWARD :return $this->getPathEngagementReward();case TYPE_KNOWLEDGE_AREA :return $this->getPathKnowledgeArea();case TYPE_MY_COACH :return $this->getPathMyCoach();case TYPE_HABIT_EMOJI :return $this->getPathHabitEmoji();case TYPE_HABIT_CONTENT :return $this->getPathHabitContent();default :return DIRECTORY_SEPARATOR;}}/**** @param Group $group* @return string*/public function getGenericImageByType($type, $code, $filename){$path = $this->getPathByType($type);$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR .$path . DIRECTORY_SEPARATOR . $code. DIRECTORY_SEPARATOR . $filename;if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);} else {$remoto = $this->config['leaderslinked.images_default.no_image'];return $this->getPresignedUrl($remoto);}}/**** @param string $type* @param string $code,* @param string $filename* @param boolean $checkExists* @return string*/public function getGenericFileByType($type, $code, $filename, $checkExists = true){$path = $this->getPathByType($type);$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR .$path . DIRECTORY_SEPARATOR . $code. DIRECTORY_SEPARATOR . $filename;if($checkExists) {if(file_exists($remoto)) {return $this->getPresignedUrl($remoto);} else {return;}}return $this->getPresignedUrl($remoto);}/**** @param string $path* @param string $code* @param string $filename* @return string*/public function delete($path, $code, $filename){$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR .$path . DIRECTORY_SEPARATOR . $code. DIRECTORY_SEPARATOR . $filename;return @unlink($remoto);}/**** @return string*/public function getPathMedia(){return $this->config['leaderslinked.storage.fullpath_media'];}/**** @return string*/public function getPathHabitEmoji(){return $this->config['leaderslinked.storage.fullpath_habit_emoji'];}/**** @return string*/public function getPathHabitContent(){return $this->config['leaderslinked.storage.fullpath_habit_content'];}/**** @return string*/public function getPathGroup(){return $this->config['leaderslinked.storage.fullpath_group'];}/**** @return string*/public function getPathUser(){return $this->config['leaderslinked.storage.fullpath_user'];}/**** @return string*/public function getPathImage(){return $this->config['leaderslinked.storage.fullpath_image'];}/**** @return string*/public function getPathJob(){return $this->config['leaderslinked.storage.fullpath_job'];}/**** @return string*/public function getPathChat(){return $this->config['leaderslinked.storage.fullpath_chat'];}/**** @return string*/public function getPathCompany(){return $this->config['leaderslinked.storage.fullpath_company'];}/**** @return string*/public function getPathFeed(){return $this->config['leaderslinked.storage.fullpath_feed'];}/**** @return string*/public function getPathMessage(){return $this->config['leaderslinked.storage.fullpath_message'];}/**** @return string*/public function getPathPost(){return $this->config['leaderslinked.storage.fullpath_post'];}/**** @return string*/public function getPathMicrolearningTopic(){return $this->config['leaderslinked.storage.fullpath_microlearning_topic'];}/**** @return string*/public function getPathMicrolearningCapsule(){return $this->config['leaderslinked.storage.fullpath_microlearning_capsule'];}/**** @return string*/public function getPathMicrolearningSlide(){return $this->config['leaderslinked.storage.fullpath_microlearning_slide'];}/**** @return string*/public function getPathJobDescription(){return $this->config['leaderslinked.storage.fullpath_job_description'];}/**** @return string*/public function getPathSelfEvaluation(){return $this->config['leaderslinked.storage.fullpath_self_evaluation'];}/**** @return string*/public function getPathRecruitmentSelection(){return $this->config['leaderslinked.storage.fullpath_recruitment_selection'];}/**** @return string*/public function getPathPerformanceEvaluation(){return $this->config['leaderslinked.storage.fullpath_performance_evaluation'];}/**** @return string*/public function getPathPlanningObjectivesAnGoals(){return $this->config['leaderslinked.storage.fullpath_planning_objectives_and_goals'];}/**** @return string*/public function getPathSurvey(){return $this->config['leaderslinked.storage.fullpath_survey'];}/**** @return string*/public function getPathNetwork(){return $this->config['leaderslinked.storage.fullpath_network'];}/**** @return string*/public function getPathEngagementReward(){return $this->config['leaderslinked.storage.fullpath_engagement_reward'];}/**** @return string*/public function getPathDailyPulse(){return $this->config['leaderslinked.storage.fullpath_daily_pulse'];}/**** @return string*/public function getPathKnowledgeArea(){return $this->config['leaderslinked.storage.fullpath_knowledge_area'];}/**** @return string*/public function getPathMyCoach(){return $this->config['leaderslinked.storage.fullpath_my_coach'];}/**** @param String $filename* return boolean*/public function objectExist($filename){return file_exists($filename);}/**** @param string $remote* @param string $local* @return boolean*/public function putObject($remote, $local){$dir = dirname($remote);if(!file_exists($dir)) {@mkdir($dir, 0755, true);}return @rename($local, $remote);}/**** @param string $filename* @return boolean*/public function deleteObject($filename){return @unlink($filename);}/**** @param string $path* @param string $code* @param string $filename* @return boolean*/public function deleteFile($path, $code, $filename){if($code) {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code. DIRECTORY_SEPARATOR . $filename;} else {$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $filename;}if(file_exists($remoto)) {return @unlink($remoto);} else {return true;}}/**** @param string $path* @param string $code* @return boolean*/public function deleteDirectory($path, $code){$remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code;if(file_exists($remoto)) {$this->deleteDirectoryRecursive($remoto);return true;} else {return true;}}/**** @param string $dir*/private function deleteDirectoryRecursive($dir) {if (is_dir($dir)) {$objects = scandir($dir);foreach ($objects as $object) {if ($object != ''.'' && $object != '..') {if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir.'/'.$object)) {@rmdir($dir. DIRECTORY_SEPARATOR .$object);} else {@unlink($dir. DIRECTORY_SEPARATOR .$object);}}}@rmdir($dir);}}/**** @param string $path* @param string $code* @param string $local_filename* @return boolean*/public function putFile($path, $code, $local_filename){if($code) {$remote = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . basename($local_filename);} else {$remote = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . basename($local_filename);}$dir = dirname($remote);if(!file_exists($dir)) {@mkdir($dir, 0755, true);}return @rename($local_filename, $remote);}/**** @param string $tempfile* @param string $path* @param string $code* @param string $filename* @return boolean*/public function moveAndPutFile($tempfile, $path, $code, $filename){if($code) {$target_file = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;} else {$target_file = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $filename;}$dir = dirname($target_file);if(!file_exists($dir)) {@mkdir($dir, 0755, true);}return move_uploaded_file($tempfile, $target_file);}/**** @return string*/public function getStoagePath(){return 'data' . DIRECTORY_SEPARATOR . 'storage';}/**** @return string*/public function getTempPath() {$interal_path = 'data' . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'tmp';if(!file_exists($interal_path)) {mkdir($interal_path, 0775);}return $interal_path;}/**** @param string $url* @return string*/public function getPresignedUrl($url){$code = hash('sha256', $url);$storageFileMapper = \LeadersLinked\Mapper\StorageFileMapper::getInstance($this->adapter);$storageFile = $storageFileMapper->fetchOneByCode($code);if(!$storageFile) {$storageFile = new \LeadersLinked\Model\StorageFile();$storageFile->code = $code;$storageFile->path = $url;$storageFile->salt = \LeadersLinked\Library\Functions::generatePassword(8);$storageFileMapper->insert($storageFile);}/*$hostname = empty($_SERVER['HTTP_ORIGIN']) ? '' : $_SERVER['HTTP_ORIGIN'];if(empty($hostname)) {$hostname = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];if(empty($hostname)) {$hostname = empty($_SERVER['HTTP_HOST']) ? '' : $_SERVER['HTTP_HOST'];}}print_r($_SERVER);$hostname = trim(str_replace('http://', 'https://', $hostname));*/return 'https://' . $_SERVER['SERVER_NAME'] . '/storage/' . $code;}}