From 428095bee82f09f35fccc976eaeb1af33c26c1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Carretero?= <frederic.carretero@veremes.com> Date: Mon, 19 Nov 2018 15:52:49 +0100 Subject: [PATCH] getFileContentInWsDataDir() -> lit le contenu d'un fichier dans ws_data --- vas/rest/class/vmlib/lang_vmlib/en-lang.inc | 5 ++ vas/rest/class/vmlib/lang_vmlib/fr-lang.inc | 5 ++ vas/rest/class/vmlib/phpUtil.inc | 64 +++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/vas/rest/class/vmlib/lang_vmlib/en-lang.inc b/vas/rest/class/vmlib/lang_vmlib/en-lang.inc index 1c43c89f..006365fe 100755 --- a/vas/rest/class/vmlib/lang_vmlib/en-lang.inc +++ b/vas/rest/class/vmlib/lang_vmlib/en-lang.inc @@ -103,6 +103,11 @@ define('ERROR_NUMBER_PHPUTIL', 'It can\'t be a number.'); define('ERROR_WEEK_CODE', 'The day code of the week "'); define('ERROR_CODE_3_VALUES', 'The code does not contain three values separated by a space.'); define('ERROR_CREATING_DIRECTORY', 'Error when creating the directory '); +define('ERROR_DELETING_DIRECTORY', 'Error when deleting the directory '); +define('ERROR_DELETING_FILE', 'Error when deleting the file '); +define('ERROR_READING_FILE_CONTENT', 'Error when reading the file '); +define('ERROR_WRITING_FILE_CONTENT', 'Erreor when writing data to the file '); +define('ERROR_FILE_NOT_FOUND', 'Erreur, file not found : '); define('YES', 'Yes'); define('NO', 'No'); diff --git a/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc b/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc index 06b60838..b6081f91 100755 --- a/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc +++ b/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc @@ -103,6 +103,11 @@ define('ERROR_NUMBER_PHPUTIL', 'Il ne peut pas être un nombre.'); define('ERROR_WEEK_CODE', 'Le code du jour de la semaine "'); define('ERROR_CODE_3_VALUES', 'Le code ne contient pas trois valeurs séparées par un espace.'); define('ERROR_CREATING_DIRECTORY', 'Erreur lors de la création du répertoire '); +define('ERROR_DELETING_DIRECTORY', 'Erreur lors de la suppression du répertoire '); +define('ERROR_DELETING_FILE', 'Erreur lors de la suppression du fichier '); +define('ERROR_READING_FILE_CONTENT', 'Erreur lors de la lecture du fichier '); +define('ERROR_WRITING_FILE_CONTENT', 'Erreur lors de l\'écriture des données dans le fichier '); +define('ERROR_FILE_NOT_FOUND', 'Erreur, le fichier n\'existe pas : '); define('YES', 'Oui'); define('NO', 'Non'); diff --git a/vas/rest/class/vmlib/phpUtil.inc b/vas/rest/class/vmlib/phpUtil.inc index 67f9e08b..092902c4 100755 --- a/vas/rest/class/vmlib/phpUtil.inc +++ b/vas/rest/class/vmlib/phpUtil.inc @@ -1566,4 +1566,68 @@ function deleteDirectoryInWsDataDir($sModule, $sObject, $mId, $sField = '', $sDi } return $sErrorMsg; } + + /** + *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 $sField field name (generally DB column name). + *@param $sFileName Name of the file to read. + *@return File content or false. + */ +function getFileContentInWsDataDir($sModule, $sObject, $mId, $sField = '', $sFileName) { + global $properties; + $sDestDir = $properties['ws_data_dir'] . "/" . $sModule . "/" . $sObject . "/" . $mId; + if(!empty($sField)) + $sDestDir .= "/" . $sField; + 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 $oAwsResult->get('Body'); + } + catch(Aws\S3\Exception\S3Exception $e) { + writeToErrorLog($e->getMessage()); + } + } + else { + // Chargment du contenu du fichier. + $sFilePath = $sDestDir . '/' . $sFileName; + if (file_exists($sFilePath)) { + $sFileContent = file_get_contents($sFilePath); + if ($sFileContent === false) + writeToErrorLog(ERROR_READING_FILE_CONTENT . $sFilePath); + return $sFileContent; + } + else { + writeToErrorLog(ERROR_FILE_NOT_FOUND . $sFilePath); + return false; + } + } +} ?> -- GitLab