| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?php
- /**
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Cloud_StorageService
- * @subpackage Adapter
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- require_once 'Zend/Cloud/StorageService/Adapter.php';
- require_once 'Zend/Cloud/StorageService/Exception.php';
- require_once 'Zend/Service/Rackspace/Files.php';
- require_once 'Zend/Service/Rackspace/Exception.php';
- /**
- * Adapter for Rackspace cloud storage
- *
- * @category Zend
- * @package Zend_Cloud_StorageService
- * @subpackage Adapter
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Cloud_StorageService_Adapter_Rackspace
- implements Zend_Cloud_StorageService_Adapter
- {
- const USER = 'user';
- const API_KEY = 'key';
- const REMOTE_CONTAINER = 'container';
- const DELETE_METADATA_KEY = 'ZF_metadata_deleted';
-
- /**
- * The Rackspace adapter
- * @var Zend_Service_Rackspace_Files
- */
- protected $_rackspace;
- /**
- * Container in which files are stored
- * @var string
- */
- protected $_container = 'default';
-
- /**
- * Constructor
- *
- * @param array|Traversable $options
- * @return void
- */
- function __construct($options = array())
- {
- if ($options instanceof Zend_Config) {
- $options = $options->toArray();
- }
- if (!is_array($options) || empty($options)) {
- throw new Zend_Cloud_StorageService_Exception('Invalid options provided');
- }
- try {
- $this->_rackspace = new Zend_Service_Rackspace_Files($options[self::USER], $options[self::API_KEY]);
- } catch (Zend_Service_Rackspace_Exception $e) {
- throw new Zend_Cloud_StorageService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
- }
-
- if (isset($options[self::HTTP_ADAPTER])) {
- $this->_rackspace->getHttpClient()->setAdapter($options[self::HTTP_ADAPTER]);
- }
- if (!empty($options[self::REMOTE_CONTAINER])) {
- $this->_container = $options[self::REMOTE_CONTAINER];
- }
- }
- /**
- * Get an item from the storage service.
- *
- * @param string $path
- * @param array $options
- * @return mixed
- */
- public function fetchItem($path, $options = null)
- {
- $item = $this->_rackspace->getObject($this->_container,$path, $options);
- if (!$this->_rackspace->isSuccessful() && ($this->_rackspace->getErrorCode()!='404')) {
- throw new Zend_Cloud_StorageService_Exception('Error on fetch: '.$this->_rackspace->getErrorMsg());
- }
- if (!empty($item)) {
- return $item->getContent();
- } else {
- return false;
- }
- }
- /**
- * Store an item in the storage service.
- *
- * @param string $destinationPath
- * @param mixed $data
- * @param array $options
- * @return void
- */
- public function storeItem($destinationPath, $data, $options = null)
- {
- $this->_rackspace->storeObject($this->_container,$destinationPath,$data,$options);
- if (!$this->_rackspace->isSuccessful()) {
- throw new Zend_Cloud_StorageService_Exception('Error on store: '.$this->_rackspace->getErrorMsg());
- }
- }
- /**
- * Delete an item in the storage service.
- *
- * @param string $path
- * @param array $options
- * @return void
- */
- public function deleteItem($path, $options = null)
- {
- $this->_rackspace->deleteObject($this->_container,$path);
- if (!$this->_rackspace->isSuccessful()) {
- throw new Zend_Cloud_StorageService_Exception('Error on delete: '.$this->_rackspace->getErrorMsg());
- }
- }
- /**
- * Copy an item in the storage service to a given path.
- *
- * @param string $sourcePath
- * @param string $destination path
- * @param array $options
- * @return void
- */
- public function copyItem($sourcePath, $destinationPath, $options = null)
- {
- $this->_rackspace->copyObject($this->_container,$sourcePath,$this->_container,$destinationPath,$options);
- if (!$this->_rackspace->isSuccessful()) {
- throw new Zend_Cloud_StorageService_Exception('Error on copy: '.$this->_rackspace->getErrorMsg());
- }
- }
- /**
- * Move an item in the storage service to a given path.
- * WARNING: This operation is *very* expensive for services that do not
- * support moving an item natively.
- *
- * @param string $sourcePath
- * @param string $destination path
- * @param array $options
- * @return void
- */
- public function moveItem($sourcePath, $destinationPath, $options = null)
- {
- try {
- $this->copyItem($sourcePath, $destinationPath, $options);
- } catch (Zend_Service_Rackspace_Exception $e) {
- throw new Zend_Cloud_StorageService_Exception('Error on move: '.$e->getMessage());
- }
- try {
- $this->deleteItem($sourcePath);
- } catch (Zend_Service_Rackspace_Exception $e) {
- $this->deleteItem($destinationPath);
- throw new Zend_Cloud_StorageService_Exception('Error on move: '.$e->getMessage());
- }
- }
- /**
- * Rename an item in the storage service to a given name.
- *
- * @param string $path
- * @param string $name
- * @param array $options
- * @return void
- */
- public function renameItem($path, $name, $options = null)
- {
- require_once 'Zend/Cloud/OperationNotAvailableException.php';
- throw new Zend_Cloud_OperationNotAvailableException('Renaming not implemented');
- }
- /**
- * Get a key/value array of metadata for the given path.
- *
- * @param string $path
- * @param array $options
- * @return array An associative array of key/value pairs specifying the metadata for this object.
- * If no metadata exists, an empty array is returned.
- */
- public function fetchMetadata($path, $options = null)
- {
- $result = $this->_rackspace->getMetadataObject($this->_container,$path);
- if (!$this->_rackspace->isSuccessful()) {
- throw new Zend_Cloud_StorageService_Exception('Error on fetch metadata: '.$this->_rackspace->getErrorMsg());
- }
- $metadata = array();
- if (isset($result['metadata'])) {
- $metadata = $result['metadata'];
- }
- // delete the self::DELETE_METADATA_KEY - this is a trick to remove all
- // the metadata information of an object (see deleteMetadata).
- // Rackspace doesn't have an API to remove the metadata of an object
- unset($metadata[self::DELETE_METADATA_KEY]);
- return $metadata;
- }
- /**
- * Store a key/value array of metadata at the given path.
- * WARNING: This operation overwrites any metadata that is located at
- * $destinationPath.
- *
- * @param string $destinationPath
- * @param array $metadata associative array specifying the key/value pairs for the metadata.
- * @param array $options
- * @return void
- */
- public function storeMetadata($destinationPath, $metadata, $options = null)
- {
- $this->_rackspace->setMetadataObject($this->_container, $destinationPath, $metadata);
- if (!$this->_rackspace->isSuccessful()) {
- throw new Zend_Cloud_StorageService_Exception('Error on store metadata: '.$this->_rackspace->getErrorMsg());
- }
- }
- /**
- * Delete a key/value array of metadata at the given path.
- *
- * @param string $path
- * @param array $metadata - An associative array specifying the key/value pairs for the metadata
- * to be deleted. If null, all metadata associated with the object will
- * be deleted.
- * @param array $options
- * @return void
- */
- public function deleteMetadata($path, $metadata = null, $options = null)
- {
- if (empty($metadata)) {
- $newMetadata = array(self::DELETE_METADATA_KEY => true);
- try {
- $this->storeMetadata($path, $newMetadata);
- } catch (Zend_Service_Rackspace_Exception $e) {
- throw new Zend_Cloud_StorageService_Exception('Error on delete metadata: '.$e->getMessage());
- }
- } else {
- try {
- $oldMetadata = $this->fetchMetadata($path);
- } catch (Zend_Service_Rackspace_Exception $e) {
- throw new Zend_Cloud_StorageService_Exception('Error on delete metadata: '.$e->getMessage());
- }
- $newMetadata = array_diff_assoc($oldMetadata, $metadata);
- try {
- $this->storeMetadata($path, $newMetadata);
- } catch (Zend_Service_Rackspace_Exception $e) {
- throw new Zend_Cloud_StorageService_Exception('Error on delete metadata: '.$e->getMessage());
- }
- }
- }
- /*
- * Recursively traverse all the folders and build an array that contains
- * the path names for each folder.
- *
- * @param string $path folder path to get the list of folders from.
- * @param array& $resultArray reference to the array that contains the path names
- * for each folder.
- * @return void
- */
- private function getAllFolders($path, &$resultArray)
- {
- if (!empty($path)) {
- $options = array (
- 'prefix' => $path
- );
- }
- $files = $this->_rackspace->getObjects($this->_container,$options);
- if (!$this->_rackspace->isSuccessful()) {
- throw new Zend_Cloud_StorageService_Exception('Error on get all folders: '.$this->_rackspace->getErrorMsg());
- }
- $resultArray = array();
- foreach ($files as $file) {
- $resultArray[dirname($file->getName())] = true;
- }
- $resultArray = array_keys($resultArray);
- }
- /**
- * Return an array of the items contained in the given path. The items
- * returned are the files or objects that in the specified path.
- *
- * @param string $path
- * @param array $options
- * @return array
- */
- public function listItems($path, $options = null)
- {
- if (!empty($path)) {
- $options = array (
- 'prefix' => $path
- );
- }
-
- $files = $this->_rackspace->getObjects($this->_container,$options);
- if (!$this->_rackspace->isSuccessful()) {
- throw new Zend_Cloud_StorageService_Exception('Error on list items: '.$this->_rackspace->getErrorMsg());
- }
- $resultArray = array();
- if (!empty($files)) {
- foreach ($files as $file) {
- $resultArray[] = $file->getName();
- }
- }
- return $resultArray;
- }
- /**
- * Get the concrete client.
- *
- * @return Zend_Service_Rackspace_File
- */
- public function getClient()
- {
- return $this->_rackspace;
- }
- }
|