diff --git a/vas/rest/class/vmlib/lang_vmlib/en-lang.inc b/vas/rest/class/vmlib/lang_vmlib/en-lang.inc index 006365fec1f7e23e8f0f64c221df4cdd04c8d8f7..f6ac1d030504f47ef99f52e22b390a99b9ddcd68 100755 --- a/vas/rest/class/vmlib/lang_vmlib/en-lang.inc +++ b/vas/rest/class/vmlib/lang_vmlib/en-lang.inc @@ -106,8 +106,9 @@ 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('ERROR_WRITING_FILE_CONTENT', 'Error when writing data to the file '); +define('ERROR_FILE_NOT_FOUND', 'Error, file not found : '); +define('ERROR_UNZIPPING_FILE', 'Error when unzipping file : '); 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 b6081f918f8e766ff77716cddfb0d687bae4f464..45bf724a02f956839743f7cef25e35d0144c117f 100755 --- a/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc +++ b/vas/rest/class/vmlib/lang_vmlib/fr-lang.inc @@ -108,6 +108,7 @@ 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('ERROR_UNZIPPING_FILE', 'Erreur lors de la décompression du fichier : '); define('YES', 'Oui'); define('NO', 'Non'); diff --git a/vas/rest/class/vmlib/phpUtil.inc b/vas/rest/class/vmlib/phpUtil.inc index 25b9eaea0fbe9b5ec82c7a9a7d412cd19db0ec20..fc0694c8c7db23de6e0cf63cdb466a5945a91185 100755 --- a/vas/rest/class/vmlib/phpUtil.inc +++ b/vas/rest/class/vmlib/phpUtil.inc @@ -1395,7 +1395,7 @@ function getDatePattern($sUserDateFormat, $sDateType) { return $Pattern; } - /** + /** *This method copy a file in ws_data. *@file vmlib/phpUtil.inc *@param $sModule Name of the module. @@ -1513,7 +1513,9 @@ function deleteDirectoryInWsDataDir($sModule, $sObject, $mId, $sField = '', $sDi } else { // Suppression du répertoire. - $sDirectoryPath = $sDestDir . '/' . $sDirectoryName; + $sDirectoryPath = $sDestDir; + if (!empty($sDirectoryName)) + $sDirectoryPath = '/' . $sDirectoryName; if (is_dir($sDirectoryPath)) { if (!cleardir($sDirectoryPath)) { writeToErrorLog(ERROR_DELETING_DIRECTORY . $sDestDir); @@ -1626,12 +1628,14 @@ function putFileContentInWsDataDir($sModule, $sObject, $mId = '', $sField = '', // 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); + $sServerPath = preg_replace('/\/{2,}/', '/', $sServerPath . '/' . $sFileName); // Suppression du répertoire. try { $aResult = $s3->putObject(array( 'Bucket' => $sBucket, - 'Key' => $sServerPath . '/' . $sFileName, - 'Body' => $sFileContent + 'Key' => $sServerPath, + 'Body' => $sFileContent, + 'ACL' => 'public-read' )); } catch(Aws\S3\Exception\S3Exception $e) { @@ -1987,5 +1991,89 @@ function uploadFileToLocal($sNomObjet, $sFileType, $sServerPath, $sMaxSize) { } return $sErrorMsg; -} +} + + /** + *This method unzip 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 $sZipFilePath Name of the file to unzip. + *@return File content or false. + */ +function unzipInWsDataDir($sModule, $sObject, $mId, $sField = '', $sZipFilePath) { + 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); + // Décompression de l'archive. + $TempDirPath = str_replace('\\', '/', $properties['extract_dir'] . '/' . getUniqRandomId()); + if (file_exists($TempDirPath)) + cleardir($TempDirPath); + if (mkdir($TempDirPath, 0777, true)) { + if (file_exists($sZipFilePath)) { + if (unZip($sZipFilePath, $TempDirPath)) { + // Copie les fichiers sur S3. + $oIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($TempDirPath)); + foreach ($oIterator as $oFileinfo) { + if (!$oFileinfo->isDir()) { + $sFilePath = str_replace('\\', '/', $oFileinfo->getPathname()); + putFileContentInWsDataDir($sModule, $sObject, $mId, $sField, str_replace($TempDirPath, '', $sFilePath), file_get_contents($sFilePath)); + } + } + clearDir($TempDirPath); + return true; + } + else { + writeToErrorLog(ERROR_UNZIPPING_FILE . $sZipFilePath); + return false; + } + } + else { + writeToErrorLog(ERROR_FILE_NOT_FOUND . $sZipFilePath); + return false; + } + } + else { + writeToErrorLog('ERROR_CREATING_DIRECTORY ' . $sDestDir); + return false; + } + } + else { + // Décompression de l'archive. + if (file_exists($sZipFilePath)) { + if (unZip($sZipFilePath, $sDestDir)) + return true; + else { + writeToErrorLog(ERROR_UNZIPPING_FILE . $sZipFilePath); + return false; + } + } + else { + writeToErrorLog(ERROR_FILE_NOT_FOUND . $sZipFilePath); + return false; + } + } +} ?>