Service.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Theme file uploader service
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Theme\Model\Uploader;
  9. use Magento\Framework\Convert\DataSize;
  10. use Magento\Framework\Filesystem;
  11. use Magento\Framework\Filesystem\DirectoryList;
  12. class Service
  13. {
  14. /**
  15. * Uploaded file path
  16. *
  17. * @var string|null
  18. */
  19. protected $_filePath;
  20. /**
  21. * @var \Magento\Framework\Filesystem\Directory\ReadInterface
  22. */
  23. protected $_tmpDirectory;
  24. /**
  25. * File size
  26. *
  27. * @var \Magento\Framework\File\Size
  28. */
  29. protected $_fileSize;
  30. /**
  31. * Data size converter
  32. *
  33. * @var \Magento\Framework\Convert\DataSize
  34. */
  35. protected $dataSize;
  36. /**
  37. * File uploader
  38. *
  39. * @var \Magento\MediaStorage\Model\File\Uploader
  40. */
  41. protected $_uploader;
  42. /**
  43. * @var \Magento\MediaStorage\Model\File\Uploader
  44. */
  45. protected $_uploaderFactory;
  46. /**
  47. * @var string|null
  48. */
  49. protected $_cssUploadLimit;
  50. /**
  51. * @var string|null
  52. */
  53. protected $_jsUploadLimit;
  54. /**
  55. * Constructor
  56. *
  57. * @param \Magento\Framework\Filesystem $filesystem
  58. * @param \Magento\Framework\File\Size $fileSize
  59. * @param \Magento\Framework\Convert\DataSize $dataSize
  60. * @param \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory
  61. * @param array $uploadLimits keys are 'css' and 'js' for file type, values defines maximum file size, example: 2M
  62. */
  63. public function __construct(
  64. \Magento\Framework\Filesystem $filesystem,
  65. \Magento\Framework\File\Size $fileSize,
  66. \Magento\Framework\Convert\DataSize $dataSize,
  67. \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
  68. array $uploadLimits = []
  69. ) {
  70. $this->_tmpDirectory = $filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
  71. $this->_fileSize = $fileSize;
  72. $this->_uploaderFactory = $uploaderFactory;
  73. $this->dataSize = $dataSize;
  74. if (isset($uploadLimits['css'])) {
  75. $this->_cssUploadLimit = $uploadLimits['css'];
  76. }
  77. if (isset($uploadLimits['js'])) {
  78. $this->_jsUploadLimit = $uploadLimits['js'];
  79. }
  80. }
  81. /**
  82. * Upload css file
  83. *
  84. * @param string $file - Key in the $_FILES array
  85. * @return array
  86. * @throws \Magento\Framework\Exception\LocalizedException
  87. */
  88. public function uploadCssFile($file)
  89. {
  90. /** @var $fileUploader \Magento\MediaStorage\Model\File\Uploader */
  91. $fileUploader = $this->_uploaderFactory->create(['fileId' => $file]);
  92. $fileUploader->setAllowedExtensions(['css']);
  93. $fileUploader->setAllowRenameFiles(true);
  94. $fileUploader->setAllowCreateFolders(true);
  95. $isValidFileSize = $this->_validateFileSize($fileUploader->getFileSize(), $this->getCssUploadMaxSize());
  96. if (!$isValidFileSize) {
  97. throw new \Magento\Framework\Exception\LocalizedException(
  98. __('The CSS file must be less than %1M.', $this->getCssUploadMaxSizeInMb())
  99. );
  100. }
  101. $file = $fileUploader->validateFile();
  102. return ['filename' => $file['name'], 'content' => $this->getFileContent($file['tmp_name'])];
  103. }
  104. /**
  105. * Upload js file
  106. *
  107. * @param string $file - Key in the $_FILES array
  108. * @return array
  109. * @throws \Magento\Framework\Exception\LocalizedException
  110. */
  111. public function uploadJsFile($file)
  112. {
  113. /** @var $fileUploader \Magento\MediaStorage\Model\File\Uploader */
  114. $fileUploader = $this->_uploaderFactory->create(['fileId' => $file]);
  115. $fileUploader->setAllowedExtensions(['js']);
  116. $fileUploader->setAllowRenameFiles(true);
  117. $fileUploader->setAllowCreateFolders(true);
  118. $isValidFileSize = $this->_validateFileSize($fileUploader->getFileSize(), $this->getJsUploadMaxSize());
  119. if (!$isValidFileSize) {
  120. throw new \Magento\Framework\Exception\LocalizedException(
  121. __('The JS file must be less than %1M.', $this->getJsUploadMaxSizeInMb())
  122. );
  123. }
  124. $file = $fileUploader->validateFile();
  125. return ['filename' => $file['name'], 'content' => $this->getFileContent($file['tmp_name'])];
  126. }
  127. /**
  128. * Get uploaded file content
  129. *
  130. * @param string $filePath
  131. * @return string
  132. */
  133. public function getFileContent($filePath)
  134. {
  135. return $this->_tmpDirectory->readFile($this->_tmpDirectory->getRelativePath($filePath));
  136. }
  137. /**
  138. * Get css upload max size
  139. *
  140. * @return int
  141. */
  142. public function getCssUploadMaxSize()
  143. {
  144. return $this->_getMaxUploadSize($this->_cssUploadLimit);
  145. }
  146. /**
  147. * Get js upload max size
  148. *
  149. * @return int
  150. */
  151. public function getJsUploadMaxSize()
  152. {
  153. return $this->_getMaxUploadSize($this->_jsUploadLimit);
  154. }
  155. /**
  156. * Get max upload size
  157. *
  158. * @param string $configuredLimit
  159. * @return int
  160. */
  161. private function _getMaxUploadSize($configuredLimit)
  162. {
  163. $maxIniUploadSize = $this->_fileSize->getMaxFileSize();
  164. if ($configuredLimit === null) {
  165. return $maxIniUploadSize;
  166. }
  167. $maxUploadSize = $this->dataSize->convertSizeToBytes($configuredLimit);
  168. return min($maxUploadSize, $maxIniUploadSize);
  169. }
  170. /**
  171. * Get css upload max size in megabytes
  172. *
  173. * @return float
  174. */
  175. public function getCssUploadMaxSizeInMb()
  176. {
  177. return $this->_fileSize->getFileSizeInMb($this->getCssUploadMaxSize());
  178. }
  179. /**
  180. * Get js upload max size in megabytes
  181. *
  182. * @return float
  183. */
  184. public function getJsUploadMaxSizeInMb()
  185. {
  186. return $this->_fileSize->getFileSizeInMb($this->getJsUploadMaxSize());
  187. }
  188. /**
  189. * Validate max file size
  190. *
  191. * @param int $fileSize
  192. * @param int $maxFileSize
  193. * @return bool
  194. */
  195. protected function _validateFileSize($fileSize, $maxFileSize)
  196. {
  197. if ($fileSize > $maxFileSize) {
  198. return false;
  199. }
  200. return true;
  201. }
  202. }