Uploader.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Backend\Block\Media;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Serialize\Serializer\Json;
  10. use Magento\Framework\Image\Adapter\UploadConfigInterface;
  11. use Magento\Backend\Model\Image\UploadResizeConfigInterface;
  12. /**
  13. * Adminhtml media library uploader
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Uploader extends \Magento\Backend\Block\Widget
  18. {
  19. /**
  20. * @var \Magento\Framework\DataObject
  21. */
  22. protected $_config;
  23. /**
  24. * @var string
  25. */
  26. protected $_template = 'Magento_Backend::media/uploader.phtml';
  27. /**
  28. * @var \Magento\Framework\File\Size
  29. */
  30. protected $_fileSizeService;
  31. /**
  32. * @var Json
  33. */
  34. private $jsonEncoder;
  35. /**
  36. * @var UploadResizeConfigInterface
  37. */
  38. private $imageUploadConfig;
  39. /**
  40. * @var UploadConfigInterface
  41. * @deprecated 101.0.1
  42. * @see \Magento\Backend\Model\Image\UploadResizeConfigInterface
  43. */
  44. private $imageConfig;
  45. /**
  46. * @param \Magento\Backend\Block\Template\Context $context
  47. * @param \Magento\Framework\File\Size $fileSize
  48. * @param array $data
  49. * @param Json $jsonEncoder
  50. * @param UploadConfigInterface $imageConfig
  51. * @param UploadResizeConfigInterface $imageUploadConfig
  52. */
  53. public function __construct(
  54. \Magento\Backend\Block\Template\Context $context,
  55. \Magento\Framework\File\Size $fileSize,
  56. array $data = [],
  57. Json $jsonEncoder = null,
  58. UploadConfigInterface $imageConfig = null,
  59. UploadResizeConfigInterface $imageUploadConfig = null
  60. ) {
  61. $this->_fileSizeService = $fileSize;
  62. $this->jsonEncoder = $jsonEncoder ?: ObjectManager::getInstance()->get(Json::class);
  63. $this->imageConfig = $imageConfig
  64. ?: ObjectManager::getInstance()->get(UploadConfigInterface::class);
  65. $this->imageUploadConfig = $imageUploadConfig
  66. ?: ObjectManager::getInstance()->get(UploadResizeConfigInterface::class);
  67. parent::__construct($context, $data);
  68. }
  69. /**
  70. * Initialize block.
  71. *
  72. * @return void
  73. */
  74. protected function _construct()
  75. {
  76. parent::_construct();
  77. $this->setId($this->getId() . '_Uploader');
  78. $uploadUrl = $this->_urlBuilder->addSessionParam()->getUrl('adminhtml/*/upload');
  79. $this->getConfig()->setUrl($uploadUrl);
  80. $this->getConfig()->setParams(['form_key' => $this->getFormKey()]);
  81. $this->getConfig()->setFileField('file');
  82. $this->getConfig()->setFilters(
  83. [
  84. 'images' => [
  85. 'label' => __('Images (.gif, .jpg, .png)'),
  86. 'files' => ['*.gif', '*.jpg', '*.png'],
  87. ],
  88. 'media' => [
  89. 'label' => __('Media (.avi, .flv, .swf)'),
  90. 'files' => ['*.avi', '*.flv', '*.swf'],
  91. ],
  92. 'all' => ['label' => __('All Files'), 'files' => ['*.*']],
  93. ]
  94. );
  95. }
  96. /**
  97. * Get file size
  98. *
  99. * @return \Magento\Framework\File\Size
  100. */
  101. public function getFileSizeService()
  102. {
  103. return $this->_fileSizeService;
  104. }
  105. /**
  106. * Get Image Upload Maximum Width Config.
  107. *
  108. * @return int
  109. * @since 100.2.7
  110. */
  111. public function getImageUploadMaxWidth()
  112. {
  113. return $this->imageUploadConfig->getMaxWidth();
  114. }
  115. /**
  116. * Get Image Upload Maximum Height Config.
  117. *
  118. * @return int
  119. * @since 100.2.7
  120. */
  121. public function getImageUploadMaxHeight()
  122. {
  123. return $this->imageUploadConfig->getMaxHeight();
  124. }
  125. /**
  126. * Prepares layout and set element renderer
  127. *
  128. * @return $this
  129. */
  130. protected function _prepareLayout()
  131. {
  132. $this->pageConfig->addPageAsset('jquery/fileUploader/css/jquery.fileupload-ui.css');
  133. return parent::_prepareLayout();
  134. }
  135. /**
  136. * Retrieve uploader js object name
  137. *
  138. * @return string
  139. */
  140. public function getJsObjectName()
  141. {
  142. return $this->getHtmlId() . 'JsObject';
  143. }
  144. /**
  145. * Retrieve config json
  146. *
  147. * @return string
  148. */
  149. public function getConfigJson()
  150. {
  151. return $this->jsonEncoder->encode($this->getConfig()->getData());
  152. }
  153. /**
  154. * Retrieve config object
  155. *
  156. * @return \Magento\Framework\DataObject
  157. */
  158. public function getConfig()
  159. {
  160. if (null === $this->_config) {
  161. $this->_config = new \Magento\Framework\DataObject();
  162. }
  163. return $this->_config;
  164. }
  165. /**
  166. * Retrieve full uploader SWF's file URL
  167. * Implemented to solve problem with cross domain SWFs
  168. * Now uploader can be only in the same URL where backend located
  169. *
  170. * @param string $url url to uploader in current theme
  171. * @return string full URL
  172. */
  173. public function getUploaderUrl($url)
  174. {
  175. return $this->_assetRepo->getUrl($url);
  176. }
  177. }