diff --git a/vas/rest/class/vmlib/phpUtil.inc b/vas/rest/class/vmlib/phpUtil.inc index 2435bfbb6bcfa8dbdf0c2e430d9f847db8a86801..67f9e08bfba1b1dd094123134f7ae4673c3c663a 100755 --- a/vas/rest/class/vmlib/phpUtil.inc +++ b/vas/rest/class/vmlib/phpUtil.inc @@ -1498,4 +1498,72 @@ function createEmptyFileInWsDataDir($sModule, $sObject, $mId, $sField = '', $sFi } return $sErrorMsg; } + + /** + *This method delete a directory 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 $sDirectoryName Name of the directory to delete. + *@return $sErrorMsg The error message. + */ +function deleteDirectoryInWsDataDir($sModule, $sObject, $mId, $sField = '', $sDirectoryName = '') { + global $properties; + $sErrorMsg = ''; + $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($sDirectoryName)) + $sServerPath .= '/' . $sDirectoryName; + // Suppression du répertoire. + try { + $oAwsResult = $s3->listObjects(array( + 'Bucket' => $sBucket, + 'Prefix' => $sServerPath + )); + // Suppression de chaque fichier. + foreach($oAwsResult->get('Contents') as $aKey) { + $aResult = $s3->deleteObject(array( + 'Bucket' => $sBucket, + 'Key' => $aKey['Key'] + )); + } + } + catch(Aws\S3\Exception\S3Exception $e) { + writeToErrorLog($e->getMessage()); + } + } + else { + // Suppression du répertoire. + $sDirectoryPath = $sDestDir . '/' . $sDirectoryName; + if (is_dir($sDirectoryPath)) { + if (!cleardir($sDirectoryPath)) { + writeToErrorLog(ERROR_DELETING_DIRECTORY . $sDestDir); + return ERROR_DELETING_DIRECTORY . $sDestDir; + } + } + } + return $sErrorMsg; +} ?>