Rackspace.php 11 KB

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