Storage.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Theme wysiwyg storage model
  8. */
  9. namespace Magento\Theme\Model\Wysiwyg;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. /**
  12. * Class Storage
  13. *
  14. * @package Magento\Theme\Model\Wysiwyg
  15. */
  16. class Storage
  17. {
  18. /**
  19. * Type font
  20. */
  21. const TYPE_FONT = 'font';
  22. /**
  23. * Type image
  24. */
  25. const TYPE_IMAGE = 'image';
  26. /**
  27. * \Directory for image thumbnail
  28. */
  29. const THUMBNAIL_DIRECTORY = '.thumbnail';
  30. /**
  31. * Image thumbnail width
  32. */
  33. const THUMBNAIL_WIDTH = 100;
  34. /**
  35. * Image thumbnail height
  36. */
  37. const THUMBNAIL_HEIGHT = 100;
  38. /**
  39. * \Directory name regular expression
  40. */
  41. const DIRECTORY_NAME_REGEXP = '/^[a-z0-9\-\_]+$/si';
  42. /**
  43. * Storage helper
  44. *
  45. * @var \Magento\Theme\Helper\Storage
  46. */
  47. protected $_helper;
  48. /**
  49. * @var \Magento\Framework\ObjectManagerInterface
  50. */
  51. protected $_objectManager;
  52. /**
  53. * @var \Magento\Framework\Image\AdapterFactory
  54. */
  55. protected $_imageFactory;
  56. /**
  57. * @var \Magento\Framework\Filesystem\Directory\Write
  58. */
  59. protected $mediaWriteDirectory;
  60. /**
  61. * @var \Magento\Framework\Url\EncoderInterface
  62. */
  63. protected $urlEncoder;
  64. /**
  65. * @var \Magento\Framework\Url\DecoderInterface
  66. */
  67. protected $urlDecoder;
  68. /**
  69. * Initialize dependencies
  70. *
  71. * @param \Magento\Framework\Filesystem $filesystem
  72. * @param \Magento\Theme\Helper\Storage $helper
  73. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  74. * @param \Magento\Framework\Image\AdapterFactory $imageFactory
  75. * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
  76. * @param \Magento\Framework\Url\DecoderInterface $urlDecoder
  77. */
  78. public function __construct(
  79. \Magento\Framework\Filesystem $filesystem,
  80. \Magento\Theme\Helper\Storage $helper,
  81. \Magento\Framework\ObjectManagerInterface $objectManager,
  82. \Magento\Framework\Image\AdapterFactory $imageFactory,
  83. \Magento\Framework\Url\EncoderInterface $urlEncoder,
  84. \Magento\Framework\Url\DecoderInterface $urlDecoder
  85. ) {
  86. $this->mediaWriteDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  87. $this->_helper = $helper;
  88. $this->_objectManager = $objectManager;
  89. $this->_imageFactory = $imageFactory;
  90. $this->urlEncoder = $urlEncoder;
  91. $this->urlDecoder = $urlDecoder;
  92. }
  93. /**
  94. * Upload file
  95. *
  96. * @param string $targetPath
  97. * @return bool
  98. * @throws \Magento\Framework\Exception\LocalizedException
  99. */
  100. public function uploadFile($targetPath)
  101. {
  102. /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
  103. $uploader = $this->_objectManager->create(
  104. \Magento\MediaStorage\Model\File\Uploader::class,
  105. ['fileId' => 'file']
  106. );
  107. $uploader->setAllowedExtensions($this->_helper->getAllowedExtensionsByType());
  108. $uploader->setAllowRenameFiles(true);
  109. $uploader->setFilesDispersion(false);
  110. $result = $uploader->save($targetPath);
  111. unset($result['path']);
  112. if (!$result) {
  113. throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t upload the file right now.'));
  114. }
  115. $this->_createThumbnail($targetPath . '/' . $uploader->getUploadedFileName());
  116. return $result;
  117. }
  118. /**
  119. * Create thumbnail for image and save it to thumbnails directory
  120. *
  121. * @param string $source
  122. * @return bool|string Resized filepath or false if errors were occurred
  123. */
  124. public function _createThumbnail($source)
  125. {
  126. if (self::TYPE_IMAGE != $this->_helper->getStorageType() || !$this->mediaWriteDirectory->isFile(
  127. $source
  128. ) || !$this->mediaWriteDirectory->isReadable(
  129. $source
  130. )
  131. ) {
  132. return false;
  133. }
  134. $thumbnailDir = $this->_helper->getThumbnailDirectory($source);
  135. $thumbnailPath = $thumbnailDir . '/' . pathinfo($source, PATHINFO_BASENAME);
  136. try {
  137. $this->mediaWriteDirectory->isExist($thumbnailDir);
  138. $image = $this->_imageFactory->create();
  139. $image->open($this->mediaWriteDirectory->getAbsolutePath($source));
  140. $image->keepAspectRatio(true);
  141. $image->resize(self::THUMBNAIL_WIDTH, self::THUMBNAIL_HEIGHT);
  142. $image->save($this->mediaWriteDirectory->getAbsolutePath($thumbnailPath));
  143. } catch (\Magento\Framework\Exception\FileSystemException $e) {
  144. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  145. return false;
  146. }
  147. if ($this->mediaWriteDirectory->isFile($thumbnailPath)) {
  148. return $thumbnailPath;
  149. }
  150. return false;
  151. }
  152. /**
  153. * Create folder
  154. *
  155. * @param string $name
  156. * @param string $path
  157. * @return array
  158. * @throws \Magento\Framework\Exception\LocalizedException
  159. */
  160. public function createFolder($name, $path)
  161. {
  162. if (!preg_match(self::DIRECTORY_NAME_REGEXP, $name)) {
  163. throw new \Magento\Framework\Exception\LocalizedException(
  164. __('Use only standard alphanumeric, dashes and underscores.')
  165. );
  166. }
  167. if (!$this->mediaWriteDirectory->isWritable($path)) {
  168. $path = $this->_helper->getStorageRoot();
  169. }
  170. $newPath = $path . '/' . $name;
  171. if ($this->mediaWriteDirectory->isExist($newPath)) {
  172. throw new \Magento\Framework\Exception\LocalizedException(__('We found a directory with the same name.'));
  173. }
  174. $this->mediaWriteDirectory->create($newPath);
  175. $result = [
  176. 'name' => $name,
  177. 'short_name' => $this->_helper->getShortFilename($name),
  178. 'path' => str_replace($this->_helper->getStorageRoot(), '', $newPath),
  179. 'id' => $this->_helper->convertPathToId($newPath)
  180. ];
  181. return $result;
  182. }
  183. /**
  184. * Delete file
  185. *
  186. * @param string $file
  187. * @return \Magento\Theme\Model\Wysiwyg\Storage
  188. */
  189. public function deleteFile($file)
  190. {
  191. $file = $this->urlDecoder->decode($file);
  192. $path = $this->mediaWriteDirectory->getRelativePath($this->_helper->getCurrentPath());
  193. $filePath = $this->mediaWriteDirectory->getRelativePath($path . '/' . $file);
  194. $thumbnailPath = $this->_helper->getThumbnailDirectory($filePath) . '/' . $file;
  195. if (0 === strpos($filePath, $path) && 0 === strpos($filePath, $this->_helper->getStorageRoot())) {
  196. $this->mediaWriteDirectory->delete($filePath);
  197. $this->mediaWriteDirectory->delete($thumbnailPath);
  198. }
  199. return $this;
  200. }
  201. /**
  202. * Get directory collection
  203. *
  204. * @param string $currentPath
  205. * @return array
  206. * @throws \Magento\Framework\Exception\LocalizedException
  207. */
  208. public function getDirsCollection($currentPath)
  209. {
  210. if (!$this->mediaWriteDirectory->isExist($currentPath)) {
  211. throw new \Magento\Framework\Exception\LocalizedException(__('We cannot find a directory with this name.'));
  212. }
  213. $paths = $this->mediaWriteDirectory->search('.*', $currentPath);
  214. $directories = [];
  215. foreach ($paths as $path) {
  216. if ($this->mediaWriteDirectory->isDirectory($path)) {
  217. $directories[] = $path;
  218. }
  219. }
  220. return $directories;
  221. }
  222. /**
  223. * Get files collection
  224. *
  225. * @return array
  226. */
  227. public function getFilesCollection()
  228. {
  229. $paths = $this->mediaWriteDirectory->search('.*', $this->_helper->getCurrentPath());
  230. $files = [];
  231. $requestParams = $this->_helper->getRequestParams();
  232. $storageType = $this->_helper->getStorageType();
  233. foreach ($paths as $path) {
  234. if (!$this->mediaWriteDirectory->isFile($path)) {
  235. continue;
  236. }
  237. $fileName = pathinfo($path, PATHINFO_BASENAME);
  238. $file = ['text' => $fileName, 'id' => $this->urlEncoder->encode($fileName)];
  239. if (self::TYPE_IMAGE == $storageType) {
  240. $requestParams['file'] = $fileName;
  241. $file['thumbnailParams'] = $requestParams;
  242. $size = @getimagesize($path);
  243. if (is_array($size)) {
  244. $file['width'] = $size[0];
  245. $file['height'] = $size[1];
  246. }
  247. }
  248. $files[] = $file;
  249. }
  250. return $files;
  251. }
  252. /**
  253. * Get directories tree array
  254. *
  255. * @return array
  256. */
  257. public function getTreeArray()
  258. {
  259. $directories = $this->getDirsCollection($this->_helper->getCurrentPath());
  260. $resultArray = [];
  261. foreach ($directories as $path) {
  262. $resultArray[] = [
  263. 'text' => $this->_helper->getShortFilename(pathinfo($path, PATHINFO_BASENAME), 20),
  264. 'id' => $this->_helper->convertPathToId($path),
  265. 'cls' => 'folder'
  266. ];
  267. }
  268. return $resultArray;
  269. }
  270. /**
  271. * Delete directory
  272. *
  273. * @param string $path
  274. * @return bool
  275. * @throws \Magento\Framework\Exception\LocalizedException
  276. */
  277. public function deleteDirectory($path)
  278. {
  279. $rootCmp = rtrim($this->_helper->getStorageRoot(), '/');
  280. $pathCmp = rtrim($path, '/');
  281. if ($rootCmp == $pathCmp) {
  282. throw new \Magento\Framework\Exception\LocalizedException(
  283. __('We can\'t delete root directory %1 right now.', $path)
  284. );
  285. }
  286. return $this->mediaWriteDirectory->delete($path);
  287. }
  288. }