Imfs.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Nirvanix
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Service_Nirvanix_Namespace_Base
  24. */
  25. require_once 'Zend/Service/Nirvanix/Namespace/Base.php';
  26. /**
  27. * Namespace proxy with additional convenience methods for the IMFS namespace.
  28. *
  29. * @category Zend
  30. * @package Zend_Service
  31. * @subpackage Nirvanix
  32. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Service_Nirvanix_Namespace_Imfs extends Zend_Service_Nirvanix_Namespace_Base
  36. {
  37. /**
  38. * Convenience function to get the contents of a file on
  39. * the Nirvanix IMFS. Analog to PHP's file_get_contents().
  40. *
  41. * @param string $filePath Remote path and filename
  42. * @param integer $expiration Number of seconds that Nirvanix
  43. * make the file available for download.
  44. * @return string Contents of file
  45. */
  46. public function getContents($filePath, $expiration = 3600)
  47. {
  48. // get url to download the file
  49. $params = array('filePath' => $filePath,
  50. 'expiration' => $expiration);
  51. $resp = $this->getOptimalUrls($params);
  52. $url = (string)$resp->Download->DownloadURL;
  53. // download the file
  54. $this->_httpClient->resetParameters();
  55. $this->_httpClient->setUri($url);
  56. $resp = $this->_httpClient->request(Zend_Http_Client::GET);
  57. return $resp->getBody();
  58. }
  59. /**
  60. * Convenience function to put the contents of a string into
  61. * the Nirvanix IMFS. Analog to PHP's file_put_contents().
  62. *
  63. * @param string $filePath Remote path and filename
  64. * @param integer $data Data to store in the file
  65. * @param string $mimeType Mime type of data
  66. * @return Zend_Service_Nirvanix_Response
  67. */
  68. public function putContents($filePath, $data, $mimeType = null)
  69. {
  70. // get storage node for upload
  71. $params = array('sizeBytes' => strlen($data));
  72. $resp = $this->getStorageNode($params);
  73. $host = (string)$resp->GetStorageNode->UploadHost;
  74. $uploadToken = (string)$resp->GetStorageNode->UploadToken;
  75. // http upload data into remote file
  76. $this->_httpClient->resetParameters();
  77. $this->_httpClient->setUri("http://{$host}/Upload.ashx");
  78. $this->_httpClient->setParameterPost('uploadToken', $uploadToken);
  79. $this->_httpClient->setParameterPost('destFolderPath', str_replace('\\', '/',dirname($filePath)));
  80. $this->_httpClient->setFileUpload(basename($filePath), 'uploadFile', $data, $mimeType);
  81. $response = $this->_httpClient->request(Zend_Http_Client::POST);
  82. return new Zend_Service_Nirvanix_Response($response->getBody());
  83. }
  84. /**
  85. * Convenience function to remove a file from the Nirvanix IMFS.
  86. * Analog to PHP's unlink().
  87. *
  88. * @param string $filePath Remove path and filename
  89. * @return Zend_Service_Nirvanix_Response
  90. */
  91. public function unlink($filePath)
  92. {
  93. $params = array('filePath' => $filePath);
  94. return $this->deleteFiles($params);
  95. }
  96. }