From a74d8523b5abdf3aa0ece072b472b927d22b0eb8 Mon Sep 17 00:00:00 2001 From: Anthony Borghi <anthony.borghi@veremes.com> Date: Tue, 4 Dec 2018 14:47:04 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20premi=C3=A8re=20version=20du?= =?UTF-8?q?=20syst=C3=A8me=20d'interface=20fichier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../class/vmlib/files/Files.interface.inc | 21 ++ .../class/vmlib/files/Files_manager.class.inc | 60 ++++ .../class/vmlib/files/Local_files.class.inc | 123 +++++++ vas/rest/class/vmlib/files/S3_files.class.inc | 308 ++++++++++++++++++ 4 files changed, 512 insertions(+) create mode 100644 vas/rest/class/vmlib/files/Files.interface.inc create mode 100644 vas/rest/class/vmlib/files/Files_manager.class.inc create mode 100644 vas/rest/class/vmlib/files/Local_files.class.inc create mode 100644 vas/rest/class/vmlib/files/S3_files.class.inc diff --git a/vas/rest/class/vmlib/files/Files.interface.inc b/vas/rest/class/vmlib/files/Files.interface.inc new file mode 100644 index 00000000..26139688 --- /dev/null +++ b/vas/rest/class/vmlib/files/Files.interface.inc @@ -0,0 +1,21 @@ +<?php + +interface Files{ + // PHP native functions + public function file_exists ($sFilePath); + public function file_put_contents ($sFilePath, $sData, $iFlags = 0, $mContext = null); + public function file_get_contents ($sFilePath, $bUseIncludePath = FALSE, $mContext = null, $iOffset = 0, $iMaxLen = -1); + public function file_get_contents_with_etag ($sFilePath, $sEtag ,$bUseIncludePath = FALSE, $mContext = null, $iOffset = 0, $iMaxLen = -1); + public function copy ($sSourceFilePath, $sDestFilePath); + public function filesize ($sFilePath); + public function readfile ($sFilePath, $bUseIncludePath = FALSE, $mContext = null); + public function rename ($sSourceFilePath, $sDestFilePath, $mContext = null); + public function unlink ($sFilePath, $mContext = null); + public function filemtime ($sFilePath); + public function filemtime_formated ($sFilePath, $sFormat); + // Veremes functions + public function getFileInfos ($sFilePath); + public function getProxyPassUrl ($sFilePath); + public function getFileEtag($sFilePath); +} +?> diff --git a/vas/rest/class/vmlib/files/Files_manager.class.inc b/vas/rest/class/vmlib/files/Files_manager.class.inc new file mode 100644 index 00000000..715e18bf --- /dev/null +++ b/vas/rest/class/vmlib/files/Files_manager.class.inc @@ -0,0 +1,60 @@ +<?php + +require_once ("S3_files.class.inc"); +require_once ("Local_files.class.inc"); + +class Files_manager{ + + public $oFileInterface; + + function __construct($oProperties){ + switch ($oProperties["filesystem"]) { + case 'fs': + $this->oFileInterface = new Local_files ($oProperties); + break; + case 's3': + $this->oFileInterface = new S3_files ($oProperties); + break; + default: + writeToErrorLog("The file system " . $oProperties["filesystem"] . " is not available on this server"); + break; + } + // create + } + + /*private upload_file($sNomObjet, $sFileType, $sServerPath, $sMaxSize, $aFileValues){ + + }*/ + + /** + *This method return the extension of a file. + *@file vmlib/phpUtil.inc + *@param $sString Full name of a file. + *@return Retourne une chaine. + */ + public function extension($sPath) { + $aTemp = explode(".", $sString); + $sString = strtolower($aTemp[count($aTemp) - 1]); + + return $sString; + } + + /** + *This method return the name of a file from its full path. + *@file vmlib/phpUtil.inc + *@param $sFullFileName Full path of a file. + *@return $aTemp2 The file name. + */ + public function getFileName($sFullFileName) { + $aTemp = explode("/", $sFullFileName); + $aTemp2 = explode("\\", $aTemp[(count($aTemp) - 1)]); + + return $aTemp2[(count($aTemp2) - 1)]; + } + + public function uploadInLocalFs ($sFilePath) { + + } +} + +?> diff --git a/vas/rest/class/vmlib/files/Local_files.class.inc b/vas/rest/class/vmlib/files/Local_files.class.inc new file mode 100644 index 00000000..6f456308 --- /dev/null +++ b/vas/rest/class/vmlib/files/Local_files.class.inc @@ -0,0 +1,123 @@ +<?php + +require_once ("Files.interface.inc"); + +class Local_files implements Files{ + + var $oProperties; + + function __construct($properties){ + $this->oProperties = $properties; + } + + /** + *This method return the extension of a file. + *@param string $sFilePath File's path. + *@return string|boolean hash sha1 of file content or false if the file doesn't exist. + */ + public function getFileEtag($sFilePath){ + if(file_exists($sFilePath)){ + return sha1(file_get_contents($sFilePath), false); + } else { + return false; + } + + } + + /** + *verify if a file exists. + *@param string $sFilePath File's path. + *@return boolean true if file exists, false else. + */ + public function file_exists ($sFilePath){ + return file_exists($sFilePath); + } + /** + *Write a string in a file. + *@param string $sFilePath File's path. + *@param string $sData string to write in this file. + *@param integer $iFlags flags to write in file (http://php.net/manual/fr/function.file-put-contents.php) Default : 0. + *@param resource $mContext result de stream_context_create(). + *@return integer|boolean number of byte written, or false if the process encounter an error. + */ + public function file_put_contents ($sFilePath, $sData, $iFlags = 0, $mContext = null){ + return file_put_contents($sFilePath, $sData, $iFlags, $mContext); + } + public function file_get_contents ($sFilePath, $bUseIncludePath = FALSE, $mContext = null, $iOffset = 0, $iMaxLen = -1){ + if ($iMaxLen == -1){ + return file_get_contents ($sFilePath, $bUseIncludePath, null, $iOffset); + } else { + return file_get_contents ($sFilePath, $bUseIncludePath, null, $iOffset, $iMaxLen); + } + } + public function file_get_contents_with_etag ($sFilePath, $sEtag ,$bUseIncludePath = FALSE, $mContext = null, $iOffset = 0, $iMaxLen = -1){ + $sProcessedEtag = $this->getFileEtag($sFilePath); + if ($sEtag == $sProcessedEtag){ + return $this->file_get_contents($sFilePath, $bUseIncludePath, $mContext, $iOffset, $iMaxLen); + } else { + return false; + } + } + public function copy ($sSourceFilePath, $sDestFilePath){ + return copy ($sSourceFilePath, $sDestFilePath); + } + public function filesize ($sFilePath){ + return filesize($sFilePath); + } + public function readfile ($sFilePath, $bUseIncludePath = FALSE, $mContext = null){ + return readfile($sFilePath, $bUseIncludePath, $mContext); + } + public function rename ($sSourceFilePath, $sDestFilePath, $mContext = null){ + if(empty($mContext)){ + return rename($sSourceFilePath, $sDestFilePath); + }else{ + return rename($sSourceFilePath, $sDestFilePath, $mContext); + } + } + public function unlink ($sFilePath, $mContext = null){ + if(empty($mContext)){ + return unlink($sFilePath); + } else { + return unlink($sFilePath, $mContext); + } + } + public function filemtime ($sFilePath){ + return filemtime($sFilePath); + } + + public function filemtime_formated($sFilePath, $sFormat){ + return date ($sFormat, $this->filemtime($sFilePath)); + } + public function getFileInfos ($sFilePath){ + $iFileSize = $this->filesize($sFilePath); + + $sFileSize = $iFileSize . " octets"; + if($iFileSize > 1024){ + if($iFileSize > 1024*1024){ + if($iFileSize > 1024*1024*1024){ + $sFileSize = "-"; + } else { + $sFileSize = (ceil($iFileSize/(1024*1024))) . "Mo"; + } + }else{ + $sFileSize = (ceil($iFileSize/1024)) . "Ko"; + } + } + + $aFileName = explode("/", $sFilePath); + + return array("filename"=>$aFileName[count($aFileName) - 1], "size"=>$sFileSize, "lastModification"=> $this->filemtime_formated ($sFilePath, "d/m/Y H:i:s"), "href" => $this->getProxyPassUrl($sFilePath)); + } + public function getProxyPassUrl ($sFilePath){ + $date = new DateTime(); + $sDataUrl = $this->oProperties['web_server_name'] . "/rest/vitis/file_downloader?key=[KEY]&eTag=[ETAG]&d=" . $date->getTimestamp(); + + $sFileUrl = str_replace("[KEY]", str_replace($this->oProperties['ws_data_dir'], "ws_data" , $sFilePath), $sDataUrl); + $sFileUrl = str_replace("[ETAG]", sha1(file_get_contents($sFilePath), false), $sFileUrl); + + return $sFileUrl; + } +} + + +?> diff --git a/vas/rest/class/vmlib/files/S3_files.class.inc b/vas/rest/class/vmlib/files/S3_files.class.inc new file mode 100644 index 00000000..470b94a1 --- /dev/null +++ b/vas/rest/class/vmlib/files/S3_files.class.inc @@ -0,0 +1,308 @@ +<?php + +require_once ("Files.interface.inc"); +require_once ("vmlib/phpUtil.inc"); +require_once ("vmlib/logUtil.inc"); +require_once ("aws_sdk/aws-autoloader.php"); + +class S3_files implements Files{ + + var $oProperties; + var $oS3Client; + + function __construct($properties){ + $this->oProperties = $properties; + // create client s3 + $this->oS3Client = new Aws\S3\S3Client(array( + 'version'=>'latest', + 'region'=> $properties['fileS3UploaderRegion'], + 'profile'=> $properties['fileS3UploaderProfil'], + 'debug' => false + )); + } + + private function getBucketConst(){ + $sBucket = $this->oProperties['fileS3UploaderBucket']; + $sPrefix = ""; + + if (strpos($sBucket, "/") > -1){ + $aBucket = explode("/", $sBucket ); + $sBucket = $aBucket[0]; + $sPrefix = implode("/", array_slice($aBucket, 1)); + } + + return array($sBucket, $sPrefix); + } + public function getFileEtag($sFilePath){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + + try { + $oResult = $this->oS3Client->headObject(array( + 'Bucket' => $sBucket, + 'Key' => $sFilePath) + ); + + return str_replace('"', '', $oResult["ETag"]); + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + } + public function file_exists ($sFilePath){ + + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + + try { + $this->oS3Client->headObject(array( + 'Bucket' => $sBucket, + 'Key' => $sFilePath) + ); + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + return true; + } + public function file_put_contents ($sFilePath, $sData, $iFlags = 0, $mContext = null){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + + try { + $this->oS3Client->putObject(array( + 'Bucket' => $sBucket, + 'Key' => $sFilePath, + 'Body' => $sData, + ) + ); + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + return strlen($sData); + } + + public function file_get_contents ($sFilePath, $bUseIncludePath = FALSE, $mContext = null, $iOffset = 0, $iMaxLen = -1){ + + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + + $sPath = $this->oProperties['extract_dir'] . "/" . getUniqRandomId(); + + try{ + $this->oS3Client->getObject(array( + 'Bucket' => $sBucket, + 'Key' => $sFilePath, + 'SaveAs' => $sPath + )); + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + unlink($sPath); + return false; + } + + $sFile = ""; + + if ($iMaxLen == -1){ + $sFile = file_get_contents ($sPath, $bUseIncludePath, null, $iOffset); + } else { + $sFile = file_get_contents ($sPath, $bUseIncludePath, null, $iOffset, $iMaxLen); + } + unlink($sPath); + return $sFile; + } + + public function file_get_contents_with_etag ($sFilePath, $sEtag ,$bUseIncludePath = FALSE, $mContext = null, $iOffset = 0, $iMaxLen = -1){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + + $sPath = $this->oProperties['extract_dir'] . "/" . getUniqRandomId(); + + try{ + $this->oS3Client->getObject(array( + 'Bucket' => $sBucket, + 'Key' => $sFilePath, + 'IfMatch' => $sEtag, + 'SaveAs' => $sPath + )); + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + unlink($sPath); + return false; + } + $sFile = ""; + + if ($iMaxLen == -1){ + $sFile = file_get_contents ($sPath, $bUseIncludePath, null, $iOffset); + } else { + $sFile = file_get_contents ($sPath, $bUseIncludePath, null, $iOffset, $iMaxLen); + } + unlink($sPath); + return $sFile; + } + + public function copy ($sSourceFilePath, $sDestFilePath){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sDestFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sDestFilePath); + $sSourceFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sSourceFilePath); + + try{ + $this->oS3Client->copyObject(array( + 'Bucket' => $sBucket, + 'Key' => $sDestFilePath, + 'CopySource' => $sBucket . "/" . $sSourceFilePath + )); + return true; + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + } + public function copyInAnotherBucket ($sSourceFilePath, $sDests3Key, $sDestBucket){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sSourceFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sSourceFilePath); + + try{ + $this->oS3Client->copyObject(array( + 'Bucket' => $sDestBucket, + 'Key' => $sDests3Key, + 'CopySource' => $sBucket . "/" . $sSourceFilePath + )); + return true; + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + } + public function copyLocalToS3 ($sSourceFilePath, $sDestFilePath){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sDestFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sDestFilePath); + + try { + $this->oS3Client->putObject(array( + 'Bucket' => $sBucket, + 'Key' => $sDestFilePath, + 'Body' => file_get_contents($sSourceFilePath), + ) + ); + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + return true; + } + public function filesize ($sFilePath){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + + try { + $oResult = $this->oS3Client->headObject(array( + 'Bucket' => $sBucket, + 'Key' => $sFilePath) + ); + + return $oResult["ContentLength"]; + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + } + public function readfile ($sFilePath, $bUseIncludePath = FALSE, $mContext = null){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + + $sPath = $this->oProperties['extract_dir'] . "/" . getUniqRandomId(); + + try{ + $this->oS3Client->getObject(array( + 'Bucket' => $sBucket, + 'Key' => $sFilePath, + 'SaveAs' => $sPath + )); + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + unlink($sPath); + return false; + } + + $iLength = readfile($sPath, $bUseIncludePath, $mContext); + unlink($sPath); + return $iLength; + } + public function rename ($sSourceFilePath, $sDestFilePath, $mContext = null){ + $bReturn = $this->copy($sSourceFilePath, $sDestFilePath); + if ($bReturn) + $bReturn = $this->unlink($sSourceFilePath, $mContext); + return $bReturn; + } + public function unlink ($sFilePath, $mContext = null){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + + try{ + $this->oS3Client->deleteObject(array( + 'Bucket' => $sBucket, + 'Key' => $sFilePath + )); + return true; + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + } + public function filemtime ($sFilePath){ + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + + try { + $oResult = $this->oS3Client->headObject(array( + 'Bucket' => $sBucket, + 'Key' => $sFilePath) + ); + + return $oResult["LastModified"]->format("U"); + }catch (Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + } + public function filemtime_formated ($sFilePath, $sFormat){ + return date ($sFormat, $this->filemtime($sFilePath)); + } + public function getFileInfos ($sFilePath){ + $iFileSize = $this->filesize($sFilePath); + + $sFileSize = $iFileSize . " octets"; + if($iFileSize > 1024){ + if($iFileSize > 1024*1024){ + if($iFileSize > 1024*1024*1024){ + $sFileSize = "-"; + } else { + $sFileSize = (ceil($iFileSize/(1024*1024))) . "Mo"; + } + }else{ + $sFileSize = (ceil($iFileSize/1024)) . "Ko"; + } + } + + $aFileName = explode("/", $sFilePath); + + return array("filename"=>$aFileName[count($aFileName) - 1], "size"=>$sFileSize, "lastModification"=> $this->filemtime_formated ($sFilePath, "d/m/Y H:i:s"), "href" => $this->getProxyPassUrl($sFilePath)); + } + public function getProxyPassUrl ($sFilePath){ + $sEtag = $this->getFileEtag($sFilePath); + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sFilePath = str_replace($this->oProperties["vas_home"], $sPrefix , $sFilePath); + $date = new DateTime(); + $sDataUrl = $this->oProperties['web_server_name'] . "/rest/vitis/file_downloader?key=[KEY]&eTag=[ETAG]&d=" . $date->getTimestamp(); + + $sFileUrl = str_replace("[KEY]", str_replace($this->oProperties['ws_data_dir'], "ws_data" , $sFilePath), $sDataUrl); + $sFileUrl = str_replace("[ETAG]", $sEtag, $sFileUrl); + + return $sFileUrl; + } +} + + +?> -- GitLab