From 916bfb9e2a9d79d370ef02e63ddf2fb1a9697283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Carretero?= <frederic.carretero@veremes.com> Date: Wed, 21 Nov 2018 10:49:19 +0100 Subject: [PATCH] fileExistsInWsDataDir() -> Teste si un fichier existe dans "ws_data" --- vas/rest/class/vmlib/phpUtil.inc | 59 ++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/vas/rest/class/vmlib/phpUtil.inc b/vas/rest/class/vmlib/phpUtil.inc index b099bf54..be016ed8 100755 --- a/vas/rest/class/vmlib/phpUtil.inc +++ b/vas/rest/class/vmlib/phpUtil.inc @@ -1709,4 +1709,63 @@ function isDirInWsDataDir($sModule, $sObject, $mId, $sDirectoryName = '') { return false; } } + + /** + *This method return the content of a file in ws_data. + *@file vmlib/phpUtil.inc + *@param $sModule Name of the module. + *@param $sObject Name of the object. + *@param $mId Id of the current object. + *@param $sDirectory directory name (optionnal). + *@param $sFileName Name of the file to read. + *@return true or false. + */ +function fileExistsInWsDataDir($sModule, $sObject, $mId, $sDirectory = '', $sFileName) { + global $properties; + $sDestDir = $properties['ws_data_dir'] . "/" . $sModule . "/" . $sObject . "/" . $mId; + if(!empty($sDirectory)) + $sDestDir .= "/" . $sDirectory; + if ($properties['fileS3Uploader'] === true) { + require_once ("aws_sdk/aws-autoloader.php"); + $s3 = new Aws\S3\S3Client(array( + 'version'=>'latest', + 'region'=> $properties['fileS3UploaderRegion'], + 'profile'=> $properties['fileS3UploaderProfil'], + 'debug' => false + )); + $sBucket = $properties['fileS3UploaderBucket']; + $sPrefix = ""; + if (strpos($sBucket, "/") > -1){ + $aBucket = explode("/", $sBucket ); + $sBucket = $aBucket[0]; + $sPrefix = implode("/", array_slice($aBucket, 1)); + } + $sServerPath = str_replace($properties["vas_home"], $sPrefix, $sDestDir); + // Suppression du slash de début de ligne (sinon création d'un répertoire vide sur S3). + if (strpos($sServerPath, '/') === 0) + $sServerPath = substr($sServerPath, 1); + if (!empty($sFileName)) + $sServerPath .= '/' . $sFileName; + // Lecture du fichier. + try { + $oAwsResult = $s3->getObject(array( + 'Bucket' => $sBucket, + 'Key' => $sServerPath + )); + return true; + } + catch(Aws\S3\Exception\S3Exception $e) { + writeToErrorLog($e->getMessage()); + return false; + } + } + else { + // Teste si le fichier existe. + $sFilePath = $sDestDir . '/' . $sFileName; + if (file_exists($sFilePath)) + return true; + else + return false; + } +} ?> -- GitLab