web/lib/Zend/Cloud/StorageService/Adapter/Rackspace.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     1 <?php
       
     2 /**
       
     3  * LICENSE
       
     4  *
       
     5  * This source file is subject to the new BSD license that is bundled
       
     6  * with this package in the file LICENSE.txt.
       
     7  * It is also available through the world-wide-web at this URL:
       
     8  * http://framework.zend.com/license/new-bsd
       
     9  * If you did not receive a copy of the license and are unable to
       
    10  * obtain it through the world-wide-web, please send an email
       
    11  * to license@zend.com so we can send you a copy immediately.
       
    12  *
       
    13  * @category   Zend
       
    14  * @package    Zend_Cloud_StorageService
       
    15  * @subpackage Adapter
       
    16  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    17  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    18  */
       
    19 
       
    20 require_once 'Zend/Cloud/StorageService/Adapter.php';
       
    21 require_once 'Zend/Cloud/StorageService/Exception.php';
       
    22 require_once 'Zend/Service/Rackspace/Files.php';
       
    23 require_once 'Zend/Service/Rackspace/Exception.php';
       
    24 
       
    25 /**
       
    26  * Adapter for Rackspace cloud storage
       
    27  *
       
    28  * @category   Zend
       
    29  * @package    Zend_Cloud_StorageService
       
    30  * @subpackage Adapter
       
    31  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    33  */
       
    34 class Zend_Cloud_StorageService_Adapter_Rackspace
       
    35     implements Zend_Cloud_StorageService_Adapter
       
    36 {
       
    37     const USER                = 'user';
       
    38     const API_KEY             = 'key';
       
    39     const REMOTE_CONTAINER    = 'container';
       
    40     const DELETE_METADATA_KEY = 'ZF_metadata_deleted';
       
    41     
       
    42     /**
       
    43      * The Rackspace adapter
       
    44      * @var Zend_Service_Rackspace_Files
       
    45      */
       
    46     protected $_rackspace;
       
    47 
       
    48     /**
       
    49      * Container in which files are stored
       
    50      * @var string
       
    51      */
       
    52     protected $_container = 'default';
       
    53     
       
    54     /**
       
    55      * Constructor
       
    56      *
       
    57      * @param  array|Traversable $options
       
    58      * @return void
       
    59      */
       
    60     function __construct($options = array())
       
    61     {
       
    62         if ($options instanceof Zend_Config) {
       
    63             $options = $options->toArray();
       
    64         }
       
    65 
       
    66         if (!is_array($options) || empty($options)) {
       
    67             throw new Zend_Cloud_StorageService_Exception('Invalid options provided');
       
    68         }
       
    69 
       
    70         try {
       
    71             $this->_rackspace = new Zend_Service_Rackspace_Files($options[self::USER], $options[self::API_KEY]);
       
    72         } catch (Zend_Service_Rackspace_Exception $e) {
       
    73             throw new Zend_Cloud_StorageService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
       
    74         }
       
    75         
       
    76         if (isset($options[self::HTTP_ADAPTER])) {
       
    77             $this->_rackspace->getHttpClient()->setAdapter($options[self::HTTP_ADAPTER]);
       
    78         }
       
    79         if (!empty($options[self::REMOTE_CONTAINER])) {
       
    80             $this->_container = $options[self::REMOTE_CONTAINER];
       
    81         }    
       
    82     }
       
    83 
       
    84      /**
       
    85      * Get an item from the storage service.
       
    86      *
       
    87      * @param  string $path
       
    88      * @param  array $options
       
    89      * @return mixed
       
    90      */
       
    91     public function fetchItem($path, $options = null)
       
    92     {
       
    93         $item = $this->_rackspace->getObject($this->_container,$path, $options);
       
    94         if (!$this->_rackspace->isSuccessful() && ($this->_rackspace->getErrorCode()!='404')) {
       
    95             throw new Zend_Cloud_StorageService_Exception('Error on fetch: '.$this->_rackspace->getErrorMsg());
       
    96         }
       
    97         if (!empty($item)) {
       
    98             return $item->getContent();
       
    99         } else {
       
   100             return false;
       
   101         }
       
   102     }
       
   103 
       
   104     /**
       
   105      * Store an item in the storage service.
       
   106      * 
       
   107      * @param  string $destinationPath
       
   108      * @param  mixed $data
       
   109      * @param  array $options
       
   110      * @return void
       
   111      */
       
   112     public function storeItem($destinationPath, $data, $options = null)
       
   113     {
       
   114         $this->_rackspace->storeObject($this->_container,$destinationPath,$data,$options);
       
   115         if (!$this->_rackspace->isSuccessful()) {
       
   116             throw new Zend_Cloud_StorageService_Exception('Error on store: '.$this->_rackspace->getErrorMsg());
       
   117         }
       
   118     }
       
   119 
       
   120     /**
       
   121      * Delete an item in the storage service.
       
   122      *
       
   123      * @param  string $path
       
   124      * @param  array $options
       
   125      * @return void
       
   126      */
       
   127     public function deleteItem($path, $options = null)
       
   128     {
       
   129         $this->_rackspace->deleteObject($this->_container,$path);
       
   130         if (!$this->_rackspace->isSuccessful()) {
       
   131             throw new Zend_Cloud_StorageService_Exception('Error on delete: '.$this->_rackspace->getErrorMsg());
       
   132         }
       
   133     }
       
   134 
       
   135     /**
       
   136      * Copy an item in the storage service to a given path.
       
   137      *
       
   138      * @param  string $sourcePath
       
   139      * @param  string $destination path
       
   140      * @param  array $options
       
   141      * @return void
       
   142      */
       
   143     public function copyItem($sourcePath, $destinationPath, $options = null)
       
   144     {
       
   145         $this->_rackspace->copyObject($this->_container,$sourcePath,$this->_container,$destinationPath,$options);
       
   146         if (!$this->_rackspace->isSuccessful()) {
       
   147             throw new Zend_Cloud_StorageService_Exception('Error on copy: '.$this->_rackspace->getErrorMsg());
       
   148         }
       
   149     }
       
   150 
       
   151     /**
       
   152      * Move an item in the storage service to a given path.
       
   153      * WARNING: This operation is *very* expensive for services that do not
       
   154      * support moving an item natively.
       
   155      *
       
   156      * @param  string $sourcePath
       
   157      * @param  string $destination path
       
   158      * @param  array $options
       
   159      * @return void
       
   160      */
       
   161     public function moveItem($sourcePath, $destinationPath, $options = null)
       
   162     {
       
   163         try {
       
   164             $this->copyItem($sourcePath, $destinationPath, $options);
       
   165         } catch (Zend_Service_Rackspace_Exception $e) {
       
   166             throw new Zend_Cloud_StorageService_Exception('Error on move: '.$e->getMessage());
       
   167         }    
       
   168         try {
       
   169             $this->deleteItem($sourcePath);
       
   170         } catch (Zend_Service_Rackspace_Exception $e) {
       
   171             $this->deleteItem($destinationPath);
       
   172             throw new Zend_Cloud_StorageService_Exception('Error on move: '.$e->getMessage());
       
   173         }    
       
   174     }
       
   175 
       
   176     /**
       
   177      * Rename an item in the storage service to a given name.
       
   178      * 
       
   179      * @param  string $path
       
   180      * @param  string $name
       
   181      * @param  array $options
       
   182      * @return void
       
   183      */
       
   184     public function renameItem($path, $name, $options = null)
       
   185     {
       
   186         require_once 'Zend/Cloud/OperationNotAvailableException.php';
       
   187         throw new Zend_Cloud_OperationNotAvailableException('Renaming not implemented');
       
   188     }
       
   189 
       
   190     /**
       
   191      * Get a key/value array of metadata for the given path.
       
   192      *
       
   193      * @param  string $path
       
   194      * @param  array $options
       
   195      * @return array An associative array of key/value pairs specifying the metadata for this object.
       
   196      *                  If no metadata exists, an empty array is returned.
       
   197      */
       
   198     public function fetchMetadata($path, $options = null)
       
   199     {
       
   200         $result = $this->_rackspace->getMetadataObject($this->_container,$path);
       
   201         if (!$this->_rackspace->isSuccessful()) {
       
   202             throw new Zend_Cloud_StorageService_Exception('Error on fetch metadata: '.$this->_rackspace->getErrorMsg());
       
   203         }
       
   204         $metadata = array();
       
   205         if (isset($result['metadata'])) {
       
   206             $metadata =  $result['metadata'];
       
   207         }
       
   208         // delete the self::DELETE_METADATA_KEY - this is a trick to remove all
       
   209         // the metadata information of an object (see deleteMetadata). 
       
   210         // Rackspace doesn't have an API to remove the metadata of an object
       
   211         unset($metadata[self::DELETE_METADATA_KEY]);
       
   212         return $metadata;
       
   213     }
       
   214 
       
   215     /**
       
   216      * Store a key/value array of metadata at the given path.
       
   217      * WARNING: This operation overwrites any metadata that is located at
       
   218      * $destinationPath.
       
   219      *
       
   220      * @param  string $destinationPath
       
   221      * @param  array  $metadata        associative array specifying the key/value pairs for the metadata.
       
   222      * @param  array  $options
       
   223      * @return void
       
   224      */
       
   225     public function storeMetadata($destinationPath, $metadata, $options = null)
       
   226     {
       
   227         $this->_rackspace->setMetadataObject($this->_container, $destinationPath, $metadata);
       
   228         if (!$this->_rackspace->isSuccessful()) {
       
   229             throw new Zend_Cloud_StorageService_Exception('Error on store metadata: '.$this->_rackspace->getErrorMsg());
       
   230         }
       
   231      }
       
   232 
       
   233     /**
       
   234      * Delete a key/value array of metadata at the given path.
       
   235      *
       
   236      * @param  string $path
       
   237      * @param  array $metadata - An associative array specifying the key/value pairs for the metadata
       
   238      *                           to be deleted.  If null, all metadata associated with the object will
       
   239      *                           be deleted.
       
   240      * @param  array $options
       
   241      * @return void
       
   242      */
       
   243     public function deleteMetadata($path, $metadata = null, $options = null)
       
   244     {
       
   245         if (empty($metadata)) {
       
   246             $newMetadata = array(self::DELETE_METADATA_KEY => true);
       
   247             try {
       
   248                 $this->storeMetadata($path, $newMetadata);
       
   249             } catch (Zend_Service_Rackspace_Exception $e) {
       
   250                 throw new Zend_Cloud_StorageService_Exception('Error on delete metadata: '.$e->getMessage());
       
   251             }
       
   252         } else {
       
   253             try {
       
   254                 $oldMetadata = $this->fetchMetadata($path);
       
   255             } catch (Zend_Service_Rackspace_Exception $e) {
       
   256                 throw new Zend_Cloud_StorageService_Exception('Error on delete metadata: '.$e->getMessage());
       
   257             }
       
   258             $newMetadata = array_diff_assoc($oldMetadata, $metadata);
       
   259             try {
       
   260                 $this->storeMetadata($path, $newMetadata);
       
   261             } catch (Zend_Service_Rackspace_Exception $e) {
       
   262                 throw new Zend_Cloud_StorageService_Exception('Error on delete metadata: '.$e->getMessage());
       
   263             }
       
   264         }
       
   265     }
       
   266 
       
   267     /*
       
   268      * Recursively traverse all the folders and build an array that contains
       
   269      * the path names for each folder.
       
   270      *
       
   271      * @param  string $path        folder path to get the list of folders from.
       
   272      * @param  array& $resultArray reference to the array that contains the path names
       
   273      *                             for each folder.
       
   274      * @return void
       
   275      */
       
   276     private function getAllFolders($path, &$resultArray)
       
   277     {
       
   278         if (!empty($path)) {
       
   279             $options = array (
       
   280                 'prefix'    => $path
       
   281             );
       
   282         }    
       
   283         $files = $this->_rackspace->getObjects($this->_container,$options);
       
   284         if (!$this->_rackspace->isSuccessful()) {
       
   285             throw new Zend_Cloud_StorageService_Exception('Error on get all folders: '.$this->_rackspace->getErrorMsg());
       
   286         }
       
   287         $resultArray = array();
       
   288         foreach ($files as $file) {
       
   289             $resultArray[dirname($file->getName())] = true;
       
   290         }
       
   291         $resultArray = array_keys($resultArray);
       
   292     }
       
   293 
       
   294     /**
       
   295      * Return an array of the items contained in the given path.  The items
       
   296      * returned are the files or objects that in the specified path.
       
   297      *
       
   298      * @param  string $path
       
   299      * @param  array  $options
       
   300      * @return array
       
   301      */
       
   302     public function listItems($path, $options = null)
       
   303     {
       
   304         if (!empty($path)) {
       
   305             $options = array (
       
   306                 'prefix'    => $path
       
   307             );
       
   308         }   
       
   309         
       
   310         $files = $this->_rackspace->getObjects($this->_container,$options);
       
   311         if (!$this->_rackspace->isSuccessful()) {
       
   312             throw new Zend_Cloud_StorageService_Exception('Error on list items: '.$this->_rackspace->getErrorMsg());
       
   313         }
       
   314         $resultArray = array();
       
   315         if (!empty($files)) {
       
   316             foreach ($files as $file) {
       
   317                 $resultArray[] = $file->getName();
       
   318             }
       
   319         }    
       
   320         return $resultArray;
       
   321     }
       
   322 
       
   323     /**
       
   324      * Get the concrete client.
       
   325      *
       
   326      * @return Zend_Service_Rackspace_File
       
   327      */
       
   328     public function getClient()
       
   329     {
       
   330          return $this->_rackspace;
       
   331     }
       
   332 }