File.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Design\Backend;
  7. use Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface;
  8. use Magento\Config\Model\Config\Backend\File as BackendFile;
  9. use Magento\Framework\App\Cache\TypeListInterface;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\App\ObjectManager;
  12. use Magento\Framework\Data\Collection\AbstractDb;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Magento\Framework\File\Mime;
  15. use Magento\Framework\Filesystem;
  16. use Magento\Framework\Model\Context;
  17. use Magento\Framework\Model\ResourceModel\AbstractResource;
  18. use Magento\Framework\Registry;
  19. use Magento\Framework\UrlInterface;
  20. use Magento\MediaStorage\Model\File\UploaderFactory;
  21. use Magento\Theme\Model\Design\Config\FileUploader\FileProcessor;
  22. /**
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. */
  25. class File extends BackendFile
  26. {
  27. /**
  28. * @var UrlInterface
  29. */
  30. protected $urlBuilder;
  31. /**
  32. * @var Mime
  33. */
  34. private $mime;
  35. /**
  36. * @param Context $context
  37. * @param Registry $registry
  38. * @param ScopeConfigInterface $config
  39. * @param TypeListInterface $cacheTypeList
  40. * @param UploaderFactory $uploaderFactory
  41. * @param RequestDataInterface $requestData
  42. * @param Filesystem $filesystem
  43. * @param UrlInterface $urlBuilder
  44. * @param AbstractResource|null $resource
  45. * @param AbstractDb|null $resourceCollection
  46. * @param array $data
  47. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  48. */
  49. public function __construct(
  50. Context $context,
  51. Registry $registry,
  52. ScopeConfigInterface $config,
  53. TypeListInterface $cacheTypeList,
  54. UploaderFactory $uploaderFactory,
  55. RequestDataInterface $requestData,
  56. Filesystem $filesystem,
  57. UrlInterface $urlBuilder,
  58. AbstractResource $resource = null,
  59. AbstractDb $resourceCollection = null,
  60. array $data = []
  61. ) {
  62. parent::__construct(
  63. $context,
  64. $registry,
  65. $config,
  66. $cacheTypeList,
  67. $uploaderFactory,
  68. $requestData,
  69. $filesystem,
  70. $resource,
  71. $resourceCollection,
  72. $data
  73. );
  74. $this->urlBuilder = $urlBuilder;
  75. }
  76. /**
  77. * Save uploaded file and remote temporary file before saving config value
  78. *
  79. * @return $this
  80. * @throws LocalizedException
  81. */
  82. public function beforeSave()
  83. {
  84. $values = $this->getValue();
  85. $value = reset($values) ?: [];
  86. if (!isset($value['file'])) {
  87. throw new LocalizedException(
  88. __('%1 does not contain field \'file\'', $this->getData('field_config/field'))
  89. );
  90. }
  91. if (isset($value['exists'])) {
  92. $this->setValue($value['file']);
  93. return $this;
  94. }
  95. $filename = basename($value['file']);
  96. $result = $this->_mediaDirectory->copyFile(
  97. $this->getTmpMediaPath($filename),
  98. $this->_getUploadDir() . '/' . $filename
  99. );
  100. if ($result) {
  101. $this->_mediaDirectory->delete($this->getTmpMediaPath($filename));
  102. if ($this->_addWhetherScopeInfo()) {
  103. $filename = $this->_prependScopeInfo($filename);
  104. }
  105. $this->setValue($filename);
  106. } else {
  107. $this->unsValue();
  108. }
  109. return $this;
  110. }
  111. /**
  112. * @return array
  113. */
  114. public function afterLoad()
  115. {
  116. $value = $this->getValue();
  117. if ($value && !is_array($value)) {
  118. $fileName = $this->_getUploadDir() . '/' . basename($value);
  119. $fileInfo = null;
  120. if ($this->_mediaDirectory->isExist($fileName)) {
  121. $stat = $this->_mediaDirectory->stat($fileName);
  122. $url = $this->getStoreMediaUrl($value);
  123. $fileInfo = [
  124. [
  125. 'url' => $url,
  126. 'file' => $value,
  127. 'size' => is_array($stat) ? $stat['size'] : 0,
  128. 'name' => basename($value),
  129. 'type' => $this->getMimeType($fileName),
  130. 'exists' => true,
  131. ]
  132. ];
  133. }
  134. $this->setValue($fileInfo);
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Getter for allowed extensions of uploaded files
  140. *
  141. * @return array
  142. */
  143. public function getAllowedExtensions()
  144. {
  145. return [];
  146. }
  147. /**
  148. * Retrieve upload directory path
  149. *
  150. * @param string $uploadDir
  151. * @return string
  152. */
  153. protected function getUploadDirPath($uploadDir)
  154. {
  155. return $this->_mediaDirectory->getRelativePath($uploadDir);
  156. }
  157. /**
  158. * @return array
  159. */
  160. public function getValue()
  161. {
  162. return $this->getData('value') ?: [];
  163. }
  164. /**
  165. * Retrieve store media url
  166. *
  167. * @param string $fileName
  168. * @return mixed
  169. */
  170. protected function getStoreMediaUrl($fileName)
  171. {
  172. $fieldConfig = $this->getFieldConfig();
  173. $baseUrl = '';
  174. $urlType = ['_type' => UrlInterface::URL_TYPE_MEDIA];
  175. if (isset($fieldConfig['base_url'])) {
  176. $baseUrl = $fieldConfig['base_url'];
  177. $urlType = ['_type' => empty($baseUrl['type']) ? 'link' : (string)$baseUrl['type']];
  178. $baseUrl = $baseUrl['value'] . '/';
  179. }
  180. return $this->urlBuilder->getBaseUrl($urlType) . $baseUrl . $fileName;
  181. }
  182. /**
  183. * Retrieve temp media path
  184. *
  185. * @param string $filename
  186. * @return string
  187. */
  188. protected function getTmpMediaPath($filename)
  189. {
  190. return 'tmp/' . FileProcessor::FILE_DIR . '/' . $filename;
  191. }
  192. /**
  193. * Retrieve MIME type of requested file
  194. *
  195. * @param string $fileName
  196. * @return string
  197. */
  198. private function getMimeType($fileName)
  199. {
  200. $absoluteFilePath = $this->_mediaDirectory->getAbsolutePath($fileName);
  201. $result = $this->getMime()->getMimeType($absoluteFilePath);
  202. return $result;
  203. }
  204. /**
  205. * Get Mime instance
  206. *
  207. * @return Mime
  208. *
  209. * @deprecated 100.2.0
  210. */
  211. private function getMime()
  212. {
  213. if ($this->mime === null) {
  214. $this->mime = ObjectManager::getInstance()->get(Mime::class);
  215. }
  216. return $this->mime;
  217. }
  218. }