diff --git a/src/vitis/vas/rest/class/vmlib/files/Local_files.class.inc b/src/vitis/vas/rest/class/vmlib/files/Local_files.class.inc index d70ae3ba70ada4412fcc31916c4e7cd8160e83c8..644f8052f1a38426d7f637e9373b7a7afc3cfe53 100644 --- a/src/vitis/vas/rest/class/vmlib/files/Local_files.class.inc +++ b/src/vitis/vas/rest/class/vmlib/files/Local_files.class.inc @@ -187,6 +187,13 @@ class Local_files implements Files{ return $sFileUrl; } + /** + *scan a directory with a depth of 1. + *@param string $sDirPath Directory's path. + *@param integer $iSortOrder flag (http://php.net/manual/fr/function.scandir.php). + *@param resource $mContext result de stream_context_create(). + *@return array list of file and subdirectoy . + */ public function scandir($sDirPath, $iSortOrder = SCANDIR_SORT_ASCENDING, $mContext = null){ if(empty($mContext)){ return scandir($sDirPath, $iSortOrder); @@ -194,6 +201,14 @@ class Local_files implements Files{ return scandir($sDirPath, $iSortOrder, $mContext); } } + /** + *create a directory. + *@param string $sDirPath directory's path. + *@param integer $iMode directory's rigths. + *@param boolean $bRecursive creation of the parents directories. + *@param resource $mContext result de stream_context_create(). + *@return boolean true if it's ok, false else. + */ public function mkdir($sDirPath, $iMode = 0777, $bRecursive = FALSE, $mContext = null){ if(empty($mContext)){ return mkdir($sDirPath, $iMode, $bRecursive); @@ -201,6 +216,12 @@ class Local_files implements Files{ return mkdir($sDirPath, $iMode, $bRecursive, $mContext); } } + /** + *remove an empty directory. + *@param string $sDirPath directory's path. + *@param resource $mContext result de stream_context_create(). + *@return boolean true if it's ok, false else. + */ public function rmdir($sDirPath, $mContext = null){ if(empty($mContext)){ return rmdir($sDirPath); @@ -208,9 +229,19 @@ class Local_files implements Files{ return rmdir($sDirPath, $mContext); } } + /** + *define if the path is a directory or a file . + *@param string $sDirPath directory's path. + *@return boolean true if it's a dir, false else. + */ public function is_dir($sDirPath){ return is_dir($sDirPath); } + /** + *scan a directory without depth limitation and return metadata usefull for treeview. + *@param string $sDirPath Directory's path. + *@return array list of file and subdirectory . + */ public function getFolderInfos($sDirPath){ if($this->is_dir($sDirPath)){ $aContentDir = $this->scandir($sDirPath); @@ -229,6 +260,11 @@ class Local_files implements Files{ return $this->getFileInfos($sDirPath); } } + /** + *remove a directory and it content. + *@param string $sDirPath directory's path. + *@return boolean true if it's ok, false else. + */ public function clearDir($sDirPath){ $ouverture = @opendir($sDossier); if (!$ouverture) @@ -254,6 +290,12 @@ class Local_files implements Files{ return false; return true; } + /** + *copy a directory and it content. + *@param string $sDirPathSrc Source directory's path. + *@param string $sDirPathDest Destination directory's path. + *@return boolean true if it's ok, false else. + */ public function copyDirectory($sDirPathSrc, $sDirPathDest){ $dir = opendir($src); @mkdir($dst); diff --git a/src/vitis/vas/rest/class/vmlib/files/S3_files.class.inc b/src/vitis/vas/rest/class/vmlib/files/S3_files.class.inc index 8287713ad3db09a0b78c73e2b510919391255113..17e297a8a89f7e939f7c8dde15755aac3ae2f25c 100644 --- a/src/vitis/vas/rest/class/vmlib/files/S3_files.class.inc +++ b/src/vitis/vas/rest/class/vmlib/files/S3_files.class.inc @@ -457,13 +457,14 @@ class S3_files implements Files{ return false; } } + // ne pas documenter ces deux fonctions public function mkdir($sDirPath, $iMode = 0777, $bRecursive = FALSE, $mContext = null){ // ne sert à rien dans un stockage d'objet - return false; + return true; } public function rmdir($sDirPath, $mContext = null){ // ne sert à rien dans un stockage d'objet (pas de dossier vide ou peu favoriser clearDir) - return false; + return true; } /** *define if the path is a directory or a file . @@ -528,6 +529,90 @@ class S3_files implements Files{ return $this->getFileInfos($sDirPath); } } + /** + *remove a directory and it content. + *@param string $sDirPath directory's path. + *@return boolean true if it's ok, false else. + */ + public function clearDir($sDirPath){ + $oDeleteParams = array( + 'Objects' => array() + ); + // traitement du bucket et de ses sous-dossiers + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sListPrefix = str_replace($this->oProperties["vas_home"], $sPrefix , $sDirPath); + // Suppression du slash de début de ligne (sinon création d'un répertoire vide sur S3). + if (strpos($sListPrefix, '/') === 0){ + $sListPrefix = substr($sListPrefix, 1); + } + if (substr($sListPrefix, -1) !== "/"){ + $sListPrefix .= "/"; + } + try{ + $aList = $this->oS3Client->listObjectsV2(array( + 'Bucket' => $sBucket, + 'Prefix' => $sListPrefix + )); + + if ($aList["KeyCount"] > 0){ + foreach ($aList["Contents"] as $aObject){ + array_push($oDeleteParams["Objects"], array("Key"=>$aObject["Key"])); + } + } + + $this->oS3Client->deleteObjects(array( + 'Bucket' => $sBucket, + 'Delete' => $oDeleteParams + )); + + return true; + }catch(Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + } + /** + *copy a directory and it content. + *@param string $sDirPathSrc Source directory's path. + *@param string $sDirPathDest Destination directory's path. + *@return boolean true if it's ok, false else. + */ + public function copyDirectory($sDirPathSrc, $sDirPathDest){ + // traitement du bucket et de ses sous-dossiers + list($sBucket, $sPrefix) = $this->getBucketConst(); + $sListPrefix = str_replace($this->oProperties["vas_home"], $sPrefix , $sDirPathSrc); + $sListPrefixDest = str_replace($this->oProperties["vas_home"], $sPrefix , $sDirPathDest); + // Suppression du slash de début de ligne (sinon création d'un répertoire vide sur S3). + if (strpos($sListPrefix, '/') === 0){ + $sListPrefix = substr($sListPrefix, 1); + } + if (substr($sListPrefix, -1) !== "/"){ + $sListPrefix .= "/"; + } + try{ + $aList = $this->oS3Client->listObjectsV2(array( + 'Bucket' => $sBucket, + 'Prefix' => $sListPrefix + )); + + if ($aList["KeyCount"] > 0){ + foreach ($aList["Contents"] as $aObject){ + array_push($oDeleteParams["Objects"], array("Key"=>$aObject["Key"])); + + $this->oS3Client->copyObject(array( + 'Bucket' => $sBucket, + 'Key' => str_replace($sListPrefix, $sListPrefixDest, $aObject["Key"]), + 'CopySource' => $sBucket . "/" . $aObject["Key"] + )); + } + } + + return true; + }catch(Aws\S3\Exception\S3Exception $e){ + writeToErrorLog($e->getMessage()); + return false; + } + } }