Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17008 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php
namespace LeadersLinked\Library;

use LeadersLinked\Model\User;
use LeadersLinked\Model\UserProfile;
use LeadersLinked\Model\Company;
use LeadersLinked\Model\Group;

class Storage 
{
    /**
     * 
     * @var\LeadersLinked\Library\Storage 
     */
    private static $_instance;
    
    /**
     * 
     * @var array
     */
    private $config;
    
    
    /**
     * 
     * @var S3Files
     */
    private $s3Files;
    
    /**
     * 
     * @param array $config
     */
    private function __construct($config) 
    {
        $this->config = $config;
        $this->s3Files = S3Files::getInstance($this->config);
    }
    
    /**
     * 
     * @param array $config
     * @return \LeadersLinked\Library\Storage
     */
    public static function getInstance($config)
    {
        if(self::$_instance == null) {
            self::$_instance = new Storage($config);
        }
        
        return self::$_instance;
    }
    
    /**
     * 
     * @param User $user
     * @return string
     */
    public function getUserImage($user)
    {
        if($user->image) {
            $remoto = $this->getPathUser() . '/' . $user->uuid . '/' .  $user->image;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.user_image'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    
    /**
     *
     * @param User $user
     * @return string
     */
    public function getUserImageForCodeAndFilename($code, $filename)
    {
        if($filename) {
            $remoto = $this->getPathUser() . '/' . $code . '/' .  $filename;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.user_image'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    /**
     *
     * @param User $user
     * @param UserProfile $userProfile
     * @return string
     */
    public function getUserProfileImage($user, $userProfile)
    {
        if($userProfile->image) {
            $remoto = $this->getPathUser() . '/' . $user->uuid . '/' .  $userProfile->image;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.user_profile'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    /**
     *
     * @param User $user
     * @param UserProfile $userProfile
     * @return string
     */
    public function getUserProfileCover($user, $userProfile)
    {
        if($userProfile->cover) {
            $remoto = $this->getPathUser() . '/' . $user->uuid . '/' .  $userProfile->cover;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.user_cover'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    /**
     *
     * @param Company $company
     * @return string
     */
    public function getCompanyImage($company)
    {
        if($company->image) {
            $remoto = $this->getPathCompany() . '/' . $company->uuid . '/' .  $company->image;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.company_profile'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    /**
     *
     * @param string $code
     * @param string $filename
     * @return string
     */
    public function getCompanyImageForCodeAndFilename($code, $filename)
    {
        if($filename) {
            $remoto = $this->getPathCompany() . '/' . $code . '/' .  $filename;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.company_profile'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    /**
     *
     * @param Company $company
     * @return string
     */
    public function getCompanyCover($company)
    {
        if($company->cover) {
            $remoto = $this->getPathCompany() . '/' . $company->uuid . '/' .  $company->cover;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.company_cover'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    /**
     *
     * @param Group $group
     * @return string
     */
    public function getGroupImage($group)
    {
        if($group->image) {
            $remoto = $this->getPathGroup() . '/' . $group->uuid . '/' .  $group->image;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.group_profile'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    /**
     *
     * @param string $code
     * @param string $filename
     * @return string
     */
    public function getGroupImageForCodeAndFilename($code, $filename)
    {
        if($filename) {
            $remoto = $this->getPathGroup() . '/' . $code . '/' .  $filename;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.group_profile'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    /**
     *
     * @param Group $group
     * @return string
     */
    public function getGroupCover($group)
    {
        if($group->cover) {
            $remoto = $this->getPathGroup() . '/' . $group->uuid . '/' .  $group->cover;
            if($this->s3Files->objectExist($remoto)) {
                return $this->s3Files->getPresignedUrl($remoto);
            }
        }
        
        $remoto = $this->config['leaderslinked.images_default.group_cover'];
        return $this->s3Files->getPresignedUrl($remoto);
    }
    
    /**
     *
     * @param Group $group
     * @return string
     */
    public function getGenericImage($path, $code, $filename)
    {
        $remoto = $path . '/' . $code. '/' .  $filename;
        if($this->s3Files->objectExist($remoto)) {
            return $this->s3Files->getPresignedUrl($remoto);
        } else {
            $remoto = $this->config['leaderslinked.images_default.no_image'];
            return $this->s3Files->getPresignedUrl($remoto);
        }
    }
    
    /**
     *
     * @param string $path
     * @param string $code 
     * @param string $filename
     * @return string
     */
    public function getGenericFile($path, $code, $filename)
    {
        $remoto = $path . '/' . $code. '/' .  $filename;
        if($this->s3Files->objectExist($remoto)) {
            return $this->s3Files->getPresignedUrl($remoto);
        } else {
            return;
        }
    }
    
    
    /**
     *
     * @param string $path
     * @param string $code
     * @param string $filename
     * @return string
     */
    public function delete($path, $code, $filename)
    {
        $remoto = $path . '/' . $code. '/' .  $filename;
        try {
            $this->s3Files->deleteObject($remoto);
            return true;
        } catch (\Exception $exception) {
            //echo "No se pudo borrar el archivo : $remoto (" . $exception->getMessage() . ")";
            return false;
        }
        
    }
    
    /**
     *
     * @return string
     */
    public function getPathMedia()
    {
        return $this->config['leaderslinked.minio.fullpath_media'];
    }
    
    
    /**
     * 
     * @return string
     */
    public function getPathGroup()
    {
        return $this->config['leaderslinked.minio.fullpath_group'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathUser()
    {
        return $this->config['leaderslinked.minio.fullpath_user'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathImage()
    {
        return $this->config['leaderslinked.minio.fullpath_image'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathJob()
    {
        return $this->config['leaderslinked.minio.fullpath_job'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathCompany()
    {
        return $this->config['leaderslinked.minio.fullpath_company'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathFeed()
    {
        return $this->config['leaderslinked.minio.fullpath_feed'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathPost()
    {
        return $this->config['leaderslinked.minio.fullpath_post'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathMicrolearningTopic()
    {
        return $this->config['leaderslinked.minio.fullpath_microlearning_topic'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathMicrolearningCapsule()
    {
        return $this->config['leaderslinked.minio.fullpath_microlearning_capsule'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathMicrolearningSlide()
    {
        return $this->config['leaderslinked.minio.fullpath_microlearning_slide'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathJobDescription()
    {
        return $this->config['leaderslinked.minio.fullpath_job_description'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathJhSelfEvaluation()
    {
        return $this->config['leaderslinked.minio.fullpath_self_evaluation'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathRecruitmentSelection()
    {
        return $this->config['leaderslinked.minio.fullpath_recruitment_selection'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathPerformanceEvaluation()
    {
        return $this->config['leaderslinked.minio.fullpath_performance_evaluation'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathPlanningObjectivesAnGoals()
    {
        return $this->config['leaderslinked.minio.fullpath_planning_objectives_and_goals'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathSurvey()
    {
        return $this->config['leaderslinked.minio.fullpath_survey'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathNetwork()
    {
        return $this->config['leaderslinked.minio.fullpath_network'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathEngagementReward()
    {
        return $this->config['leaderslinked.minio.fullpath_engagement_reward'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathDailyPulse()
    {
        return $this->config['leaderslinked.minio.fullpath_daily_pulse'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathHabitEmoji()
    {
        return $this->config['leaderslinked.minio.fullpath_habit_emoji'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathHabitContent()
    {
        return $this->config['leaderslinked.minio.fullpath_habit_content'];
    }
    
    
    /**
     *
     * @return string
     */
    public function getPathKnowledgeArea()
    {
        return $this->config['leaderslinked.minio.fullpath_knowledge_area'];
    }
    
    /**
     *
     * @return string
     */
    public function getPathMyCoach()
    {
        return $this->config['leaderslinked.minio.fullpath_my_coach'];
    }
    
    /**
     * 
     * @param String $filename
     * return boolean
     */
    public function objectExist($filename) 
    {
        return $this->s3Files->objectExist($filename);
    }
    
    /**
     * 
     * @param string $remoto
     * @param string $local
     * @return boolean
     */
    public function putObject($remote, $local)
    {
        return $this->s3Files->putObject($remote, $local);
    }
    
    
    /**
     *
     * @param string $filename
 
     * @return boolean
     */
    public function deleteObject($filename)
    {
        return $this->s3Files->deleteObject($filename);
    }
    
    
    /**
     * 
     * @param string $path
     * @param string $code
     * @param string $filename
     * @return boolean
     */
    public function deleteFile($path, $code, $filename)
    {
        if($code) {
            $remoto = $path . '/' . $code. '/' .  $filename;
        } else {
            $remoto = $path . '/' .  $filename;
        }
        if($this->s3Files->objectExist($remoto)) {
            return $this->s3Files->deleteObject($remoto);
        } else {
            return true;
        }
    }

    
    /**
     *
     * @param string $path
     * @param string $code
     * @param string $full_localfilename
     * @return boolean
     */
    public function putFile($remotePath, $code, $full_localfilename)
    {
        if($code) {
            $remotePath = $remotePath . '/' . $code;
        } else {
            $remotePath = $remotePath;
        }
        
        $full_remotefilename = $remotePath . '/' . basename($full_localfilename);
        

        $result = $this->s3Files->putObject($full_remotefilename, $full_localfilename);
        //@unlink($filename);
        
        return $result;
    }

    
    /**
     *
     * @param string $path
     * @param string $code
     * @param string $tempfile
     * @param string $filename
     * @return boolean
     */
    public function moveAndPutFile($path, $code, $tempfile, $filename)
    {
        if($code) {
            $filepath = $path . '/' . $code;
        } else {
            $filepath = $path;
        }
        
        
        
        $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename;
        
        move_uploaded_file($tempfile, $filename);
        
        $result = $this->s3Files->putObject($filename, $filepath);
        @unlink($filename);
        
        return $result;
    }
    
    /**
     * 
     * @return string
     */
    public function getTempPath()
    {
        return 'data' . DIRECTORY_SEPARATOR . 'storage'; 
    }
    
    
    
    
    
    
}