PageRenderTime 77ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Cloud/StorageService/Adapter/Nirvanix.php

https://bitbucket.org/mengqing/magento-mirror
PHP | 399 lines | 217 code | 25 blank | 157 comment | 26 complexity | 9c4bfd70fac30ae3ce5f81e94906fe24 MD5 | raw file
  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
  15. * @subpackage StorageService
  16. * @copyright Copyright (c) 2005-2010 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/Nirvanix.php';
  22. /**
  23. * Adapter for Nirvanix cloud storage
  24. *
  25. * @category Zend
  26. * @package Zend_Cloud
  27. * @subpackage StorageService
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Cloud_StorageService_Adapter_Nirvanix
  32. implements Zend_Cloud_StorageService_Adapter
  33. {
  34. const USERNAME = 'auth_username';
  35. const PASSWORD = 'auth_password';
  36. const APP_KEY = 'auth_accesskey';
  37. const REMOTE_DIRECTORY = 'remote_directory';
  38. /**
  39. * The Nirvanix adapter
  40. * @var Zend_Service_Nirvanix
  41. */
  42. protected $_nirvanix;
  43. protected $_imfNs;
  44. protected $_metadataNs;
  45. protected $_remoteDirectory;
  46. private $maxPageSize = 500;
  47. /**
  48. * Constructor
  49. *
  50. * @param array|Zend_Config $options
  51. * @return void
  52. */
  53. function __construct($options = array())
  54. {
  55. if ($options instanceof Zend_Config) {
  56. $options = $options->toArray();
  57. }
  58. if (!is_array($options)) {
  59. throw new Zend_Cloud_StorageService_Exception('Invalid options provided');
  60. }
  61. $auth = array(
  62. 'username' => $options[self::USERNAME],
  63. 'password' => $options[self::PASSWORD],
  64. 'appKey' => $options[self::APP_KEY],
  65. );
  66. $nirvanix_options = array();
  67. if (isset($options[self::HTTP_ADAPTER])) {
  68. $httpc = new Zend_Http_Client();
  69. $httpc->setAdapter($options[self::HTTP_ADAPTER]);
  70. $nirvanix_options['httpClient'] = $httpc;
  71. }
  72. try {
  73. $this->_nirvanix = new Zend_Service_Nirvanix($auth, $nirvanix_options);
  74. $this->_remoteDirectory = $options[self::REMOTE_DIRECTORY];
  75. $this->_imfNs = $this->_nirvanix->getService('IMFS');
  76. $this->_metadataNs = $this->_nirvanix->getService('Metadata');
  77. } catch (Zend_Service_Nirvanix_Exception $e) {
  78. throw new Zend_Cloud_StorageService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
  79. }
  80. }
  81. /**
  82. * Get an item from the storage service.
  83. *
  84. * @param string $path
  85. * @param array $options
  86. * @return mixed
  87. */
  88. public function fetchItem($path, $options = null)
  89. {
  90. $path = $this->_getFullPath($path);
  91. try {
  92. $item = $this->_imfNs->getContents($path);
  93. } catch (Zend_Service_Nirvanix_Exception $e) {
  94. throw new Zend_Cloud_StorageService_Exception('Error on fetch: '.$e->getMessage(), $e->getCode(), $e);
  95. }
  96. return $item;
  97. }
  98. /**
  99. * Store an item in the storage service.
  100. * WARNING: This operation overwrites any item that is located at
  101. * $destinationPath.
  102. * @param string $destinationPath
  103. * @param mixed $data
  104. * @param array $options
  105. * @return void
  106. */
  107. public function storeItem($destinationPath, $data, $options = null)
  108. {
  109. try {
  110. $path = $this->_getFullPath($destinationPath);
  111. $this->_imfNs->putContents($path, $data);
  112. } catch (Zend_Service_Nirvanix_Exception $e) {
  113. throw new Zend_Cloud_StorageService_Exception('Error on store: '.$e->getMessage(), $e->getCode(), $e);
  114. }
  115. return true;
  116. }
  117. /**
  118. * Delete an item in the storage service.
  119. *
  120. * @param string $path
  121. * @param array $options
  122. * @return void
  123. */
  124. public function deleteItem($path, $options = null)
  125. {
  126. try {
  127. $path = $this->_getFullPath($path);
  128. $this->_imfNs->unlink($path);
  129. } catch(Zend_Service_Nirvanix_Exception $e) {
  130. // if (trim(strtoupper($e->getMessage())) != 'INVALID PATH') {
  131. // // TODO Differentiate among errors in the Nirvanix adapter
  132. throw new Zend_Cloud_StorageService_Exception('Error on delete: '.$e->getMessage(), $e->getCode(), $e);
  133. }
  134. }
  135. /**
  136. * Copy an item in the storage service to a given path.
  137. * WARNING: This operation is *very* expensive for services that do not
  138. * support copying an item natively.
  139. *
  140. * @param string $sourcePath
  141. * @param string $destination path
  142. * @param array $options
  143. * @return void
  144. */
  145. public function copyItem($sourcePath, $destinationPath, $options = null)
  146. {
  147. try {
  148. $sourcePath = $this->_getFullPath($sourcePath);
  149. $destinationPath = $this->_getFullPath($destinationPath);
  150. $this->_imfNs->CopyFiles(array('srcFilePath' => $sourcePath,
  151. 'destFolderPath' => $destinationPath));
  152. } catch (Zend_Service_Nirvanix_Exception $e) {
  153. throw new Zend_Cloud_StorageService_Exception('Error on copy: '.$e->getMessage(), $e->getCode(), $e);
  154. }
  155. }
  156. /**
  157. * Move an item in the storage service to a given path.
  158. * WARNING: This operation is *very* expensive for services that do not
  159. * support moving an item natively.
  160. *
  161. * @param string $sourcePath
  162. * @param string $destination path
  163. * @param array $options
  164. * @return void
  165. */
  166. public function moveItem($sourcePath, $destinationPath, $options = null)
  167. {
  168. try {
  169. $sourcePath = $this->_getFullPath($sourcePath);
  170. $destinationPath = $this->_getFullPath($destinationPath);
  171. $this->_imfNs->RenameFile(array('filePath' => $sourcePath,
  172. 'newFileName' => $destinationPath));
  173. // $this->_imfNs->MoveFiles(array('srcFilePath' => $sourcePath,
  174. // 'destFolderPath' => $destinationPath));
  175. } catch (Zend_Service_Nirvanix_Exception $e) {
  176. throw new Zend_Cloud_StorageService_Exception('Error on move: '.$e->getMessage(), $e->getCode(), $e);
  177. }
  178. }
  179. /**
  180. * Rename an item in the storage service to a given name.
  181. *
  182. *
  183. * @param string $path
  184. * @param string $name
  185. * @param array $options
  186. * @return void
  187. */
  188. public function renameItem($path, $name, $options = null)
  189. {
  190. #require_once 'Zend/Cloud/OperationNotAvailableException.php';
  191. throw new Zend_Cloud_OperationNotAvailableException('Renaming not implemented');
  192. }
  193. /**
  194. * Get a key/value array of metadata for the given path.
  195. *
  196. * @param string $path
  197. * @param array $options
  198. * @return array An associative array of key/value pairs specifying the metadata for this object.
  199. * If no metadata exists, an empty array is returned.
  200. */
  201. public function fetchMetadata($path, $options = null)
  202. {
  203. $path = $this->_getFullPath($path);
  204. try {
  205. $metadataNode = $this->_metadataNs->getMetadata(array('path' => $path));
  206. } catch (Zend_Service_Nirvanix_Exception $e) {
  207. throw new Zend_Cloud_StorageService_Exception('Error on fetching metadata: '.$e->getMessage(), $e->getCode(), $e);
  208. }
  209. $metadata = array();
  210. $length = count($metadataNode->Metadata);
  211. // Need to special case this as Nirvanix returns an array if there is
  212. // more than one, but doesn't return an array if there is only one.
  213. if ($length == 1)
  214. {
  215. $metadata[(string)$metadataNode->Metadata->Type->value] = (string)$metadataNode->Metadata->Value;
  216. }
  217. else if ($length > 1)
  218. {
  219. for ($i=0; $i<$length; $i++)
  220. {
  221. $metadata[(string)$metadataNode->Metadata[$i]->Type] = (string)$metadataNode->Metadata[$i]->Value;
  222. }
  223. }
  224. return $metadata;
  225. }
  226. /**
  227. * Store a key/value array of metadata at the given path.
  228. * WARNING: This operation overwrites any metadata that is located at
  229. * $destinationPath.
  230. *
  231. * @param array $metadata - An associative array specifying the key/value pairs for the metadata.
  232. * @param $destinationPath
  233. * @param array $options
  234. * @return void
  235. */
  236. public function storeMetadata($destinationPath, $metadata, $options = null)
  237. {
  238. $destinationPath = $this->_getFullPath($destinationPath);
  239. if ($metadata != null) {
  240. try {
  241. foreach ($metadata AS $key=>$value) {
  242. $metadataString = $key . ":" . $value;
  243. $this->_metadataNs->SetMetadata(array(
  244. 'path' => $destinationPath,
  245. 'metadata' => $metadataString,
  246. ));
  247. }
  248. } catch (Zend_Service_Nirvanix_Exception $e) {
  249. throw new Zend_Cloud_StorageService_Exception('Error on storing metadata: '.$e->getMessage(), $e->getCode(), $e);
  250. }
  251. }
  252. }
  253. /**
  254. * Delete a key/value array of metadata at the given path.
  255. *
  256. * @param string $path
  257. * @param array $metadata - An associative array specifying the key/value pairs for the metadata
  258. * to be deleted. If null, all metadata associated with the object will
  259. * be deleted.
  260. * @param array $options
  261. * @return void
  262. */
  263. public function deleteMetadata($path, $metadata = null, $options = null)
  264. {
  265. $path = $this->_getFullPath($path);
  266. try {
  267. if ($metadata == null) {
  268. $this->_metadataNs->DeleteAllMetadata(array('path' => $path));
  269. } else {
  270. foreach ($metadata AS $key=>$value) {
  271. $this->_metadataNs->DeleteMetadata(array(
  272. 'path' => $path,
  273. 'metadata' => $key,
  274. ));
  275. }
  276. }
  277. } catch (Zend_Service_Nirvanix_Exception $e) {
  278. throw new Zend_Cloud_StorageService_Exception('Error on deleting metadata: '.$e->getMessage(), $e->getCode(), $e);
  279. }
  280. }
  281. /*
  282. * Recursively traverse all the folders and build an array that contains
  283. * the path names for each folder.
  284. *
  285. * @param $path - The folder path to get the list of folders from.
  286. * @param &$resultArray - reference to the array that contains the path names
  287. * for each folder.
  288. */
  289. private function getAllFolders($path, &$resultArray)
  290. {
  291. $response = $this->_imfNs->ListFolder(array(
  292. 'folderPath' => $path,
  293. 'pageNumber' => 1,
  294. 'pageSize' => $this->maxPageSize,
  295. ));
  296. $numFolders = $response->ListFolder->TotalFolderCount;
  297. if ($numFolders == 0) {
  298. return;
  299. } else {
  300. //Need to special case this as Nirvanix returns an array if there is
  301. //more than one, but doesn't return an array if there is only one.
  302. if ($numFolders == 1) {
  303. $folderPath = $response->ListFolder->Folder->Path;
  304. array_push($resultArray, $folderPath);
  305. $this->getAllFolders('/' . $folderPath, $resultArray);
  306. } else {
  307. foreach ($response->ListFolder->Folder as $arrayElem) {
  308. $folderPath = $arrayElem->Path;
  309. array_push($resultArray, $folderPath);
  310. $this->getAllFolders('/' . $folderPath, $resultArray);
  311. }
  312. }
  313. }
  314. }
  315. /**
  316. * Return an array of the items contained in the given path. The items
  317. * returned are the files or objects that in the specified path.
  318. *
  319. * @param string $path
  320. * @param array $options
  321. * @return array
  322. */
  323. public function listItems($path, $options = null)
  324. {
  325. $path = $this->_getFullPath($path);
  326. $resultArray = array();
  327. if (!isset($path)) {
  328. return false;
  329. } else {
  330. try {
  331. $response = $this->_imfNs->ListFolder(array(
  332. 'folderPath' => $path,
  333. 'pageNumber' => 1,
  334. 'pageSize' => $this->maxPageSize,
  335. ));
  336. } catch (Zend_Service_Nirvanix_Exception $e) {
  337. throw new Zend_Cloud_StorageService_Exception('Error on list: '.$e->getMessage(), $e->getCode(), $e);
  338. }
  339. $numFiles = $response->ListFolder->TotalFileCount;
  340. //Add the file names to the array
  341. if ($numFiles != 0) {
  342. //Need to special case this as Nirvanix returns an array if there is
  343. //more than one, but doesn't return an array if there is only one.
  344. if ($numFiles == 1) {
  345. $resultArray[] = (string)$response->ListFolder->File->Name;
  346. }
  347. else {
  348. foreach ($response->ListFolder->File as $arrayElem) {
  349. $resultArray[] = (string) $arrayElem->Name;
  350. }
  351. }
  352. }
  353. }
  354. return $resultArray;
  355. }
  356. /**
  357. * Get full path to an object
  358. *
  359. * @param string $path
  360. * @return string
  361. */
  362. private function _getFullPath($path)
  363. {
  364. return $this->_remoteDirectory . $path;
  365. }
  366. /**
  367. * Get the concrete client.
  368. * @return Zend_Service_Nirvanix
  369. */
  370. public function getClient()
  371. {
  372. return $this->_nirvanix;
  373. }
  374. }