StorageController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_Cloud
  17. * @subpackage examples
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Cloud
  24. * @subpackage examples
  25. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class StorageController extends Zend_Controller_Action
  29. {
  30. public $dependencies = array('config');
  31. /**
  32. * @var Zend_Cloud_StorageService_Adapter
  33. */
  34. protected $_storage = null;
  35. public function preDispatch()
  36. {
  37. $this->_storage = Zend_Cloud_StorageService_Factory::getAdapter($this->config->storage);
  38. }
  39. public function indexAction()
  40. {
  41. $this->view->items = $this->_storage->listItems("/");
  42. }
  43. public function getAction()
  44. {
  45. if (!$name = $this->_getParam('item', false)) {
  46. return $this->_helper->redirector('index');
  47. }
  48. $item = $this->_storage->fetchItem($name, array(
  49. Zend_Cloud_StorageService_Adapter_S3::FETCH_STREAM => true,
  50. Zend_Cloud_StorageService_Adapter_WindowsAzure::RETURN_TYPE => Zend_Cloud_StorageService_Adapter_WindowsAzure::RETURN_STREAM
  51. ));
  52. if (!$item) {
  53. $this->getResponse()->setHttpResponseCode(404);
  54. return;
  55. }
  56. $meta = $this->_storage->fetchMetadata($name);
  57. if (isset($meta["type"])) {
  58. $this->getResponse()->setHeader('Content-Type', $meta["type"]);
  59. }
  60. // don't render the view, send the item instead
  61. $this->_helper->viewRenderer->setNoRender(true);
  62. if ($item instanceof Zend_Http_Response_Stream) {
  63. fpassthru($item->getStream());
  64. } elseif (is_resource($item)) {
  65. fpassthru($item);
  66. } else {
  67. $this->getResponse()->setBody($item);
  68. }
  69. }
  70. public function uploadAction()
  71. {
  72. $request = $this->getRequest();
  73. if (!$request->isPost()) {
  74. return;
  75. }
  76. $name = $this->_getParam('name', false);
  77. $upload = new Zend_File_Transfer();
  78. $upload->addValidator('Count', false, 1);
  79. if (!$upload->isValid()) {
  80. return;
  81. }
  82. $upload->receive();
  83. $file = $upload->getFileName();
  84. $fp = fopen($file, "r");
  85. if (!$fp) {
  86. return;
  87. }
  88. $mime = $upload->getMimeType();
  89. if (!$name) {
  90. // get short name
  91. $name = $upload->getFileName(null, false);
  92. }
  93. $this->_storage->storeItem($name, $fp, array(
  94. Zend_Cloud_StorageService_Adapter_S3::METADATA => array("type" => $mime)
  95. ));
  96. try {
  97. $this->_storage->storeMetadata($name, array("type" => $mime));
  98. } catch(Zend_Cloud_OperationNotAvailableException $e) {
  99. // ignore it
  100. }
  101. return $this->_helper->redirector('index');
  102. }
  103. }