Imfs.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Service_Nirvanix_Namespace_Base
  23. */
  24. require_once 'Zend/Service/Nirvanix/Namespace/Base.php';
  25. /**
  26. * Namespace proxy with additional convenience methods for the IMFS namespace.
  27. *
  28. * @category Zend
  29. * @package Zend_Service
  30. * @subpackage Nirvanix
  31. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Service_Nirvanix_Namespace_Imfs extends Zend_Service_Nirvanix_Namespace_Base
  35. {
  36. /**
  37. * Convenience function to get the contents of a file on
  38. * the Nirvanix IMFS. Analog to PHP's file_get_contents().
  39. *
  40. * @param string $filePath Remote path and filename
  41. * @param integer $expiration Number of seconds that Nirvanix
  42. * make the file available for download.
  43. * @return string Contents of file
  44. */
  45. public function getContents($filePath, $expiration = 3600)
  46. {
  47. // get url to download the file
  48. $params = array('filePath' => $filePath,
  49. 'expiration' => $expiration);
  50. $resp = $this->getOptimalUrls($params);
  51. $url = (string)$resp->Download->DownloadURL;
  52. // download the file
  53. $this->_httpClient->resetParameters();
  54. $this->_httpClient->setUri($url);
  55. $resp = $this->_httpClient->request(Zend_Http_Client::GET);
  56. return $resp->getBody();
  57. }
  58. /**
  59. * Convenience function to put the contents of a string into
  60. * the Nirvanix IMFS. Analog to PHP's file_put_contents().
  61. *
  62. * @param string $filePath Remote path and filename
  63. * @param integer $data Data to store in the file
  64. * @param string $mimeType Mime type of data
  65. * @return Zend_Service_Nirvanix_Response
  66. */
  67. public function putContents($filePath, $data, $mimeType = null)
  68. {
  69. // get storage node for upload
  70. $params = array('sizeBytes' => strlen($data));
  71. $resp = $this->getStorageNode($params);
  72. $host = (string)$resp->GetStorageNode->UploadHost;
  73. $uploadToken = (string)$resp->GetStorageNode->UploadToken;
  74. // http upload data into remote file
  75. $this->_httpClient->resetParameters();
  76. $this->_httpClient->setUri("http://{$host}/Upload.ashx");
  77. $this->_httpClient->setParameterPost('uploadToken', $uploadToken);
  78. $this->_httpClient->setParameterPost('destFolderPath', dirname($filePath));
  79. $this->_httpClient->setFileUpload(basename($filePath), 'uploadFile', $data, $mimeType);
  80. $response = $this->_httpClient->request(Zend_Http_Client::POST);
  81. return new Zend_Service_Nirvanix_Response($response->getBody());
  82. }
  83. /**
  84. * Convenience function to remove a file from the Nirvanix IMFS.
  85. * Analog to PHP's unlink().
  86. *
  87. * @param string $filePath Remove path and filename
  88. * @return Zend_Service_Nirvanix_Response
  89. */
  90. public function unlink($filePath)
  91. {
  92. $params = array('filePath' => $filePath);
  93. return $this->deleteFiles($params);
  94. }
  95. }