PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-theme/Helper/Storage.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 316 lines | 146 code | 31 blank | 139 comment | 15 complexity | 081e40558dad2aabe19bde4b62f802de MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Theme storage helper
  8. */
  9. namespace Magento\Theme\Helper;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. class Storage extends \Magento\Framework\App\Helper\AbstractHelper
  12. {
  13. /**
  14. * Parameter name of node
  15. */
  16. const PARAM_NODE = 'node';
  17. /**
  18. * Parameter name of content type
  19. */
  20. const PARAM_CONTENT_TYPE = 'content_type';
  21. /**
  22. * Parameter name of theme identification number
  23. */
  24. const PARAM_THEME_ID = 'theme_id';
  25. /**
  26. * Parameter name of filename
  27. */
  28. const PARAM_FILENAME = 'filename';
  29. /**
  30. * Root node value identification number
  31. */
  32. const NODE_ROOT = 'root';
  33. /**
  34. * Display name for images storage type
  35. */
  36. const IMAGES = 'Images';
  37. /**
  38. * Display name for fonts storage type
  39. */
  40. const FONTS = 'Fonts';
  41. /**
  42. * Current directory path
  43. *
  44. * @var string
  45. */
  46. protected $_currentPath;
  47. /**
  48. * Current storage root path
  49. *
  50. * @var string
  51. */
  52. protected $_storageRoot;
  53. /**
  54. * Magento filesystem
  55. *
  56. * @var \Magento\Framework\Filesystem
  57. */
  58. protected $filesystem;
  59. /**
  60. * @var \Magento\Backend\Model\Session
  61. */
  62. protected $_session;
  63. /**
  64. * @var \Magento\Framework\View\Design\Theme\FlyweightFactory
  65. */
  66. protected $_themeFactory;
  67. /**
  68. * @var \Magento\Framework\Filesystem\Directory\Write
  69. */
  70. protected $mediaDirectoryWrite;
  71. /**
  72. * @param \Magento\Framework\App\Helper\Context $context
  73. * @param \Magento\Framework\Filesystem $filesystem
  74. * @param \Magento\Backend\Model\Session $session
  75. * @param \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory
  76. */
  77. public function __construct(
  78. \Magento\Framework\App\Helper\Context $context,
  79. \Magento\Framework\Filesystem $filesystem,
  80. \Magento\Backend\Model\Session $session,
  81. \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory
  82. ) {
  83. parent::__construct($context);
  84. $this->filesystem = $filesystem;
  85. $this->_session = $session;
  86. $this->_themeFactory = $themeFactory;
  87. $this->mediaDirectoryWrite = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  88. $this->mediaDirectoryWrite->create($this->mediaDirectoryWrite->getRelativePath($this->getStorageRoot()));
  89. }
  90. /**
  91. * Convert path to id
  92. *
  93. * @param string $path
  94. * @return string
  95. */
  96. public function convertPathToId($path)
  97. {
  98. $path = str_replace($this->getStorageRoot(), '', $path);
  99. return $this->urlEncoder->encode($path);
  100. }
  101. /**
  102. * Convert id to path
  103. *
  104. * @param string $value
  105. * @return string
  106. */
  107. public function convertIdToPath($value)
  108. {
  109. $path = $this->urlDecoder->decode($value);
  110. if (!strstr($path, $this->getStorageRoot())) {
  111. $path = $this->getStorageRoot() . $path;
  112. }
  113. return $path;
  114. }
  115. /**
  116. * Get short file name
  117. *
  118. * @param string $filename
  119. * @param int $maxLength
  120. * @return string
  121. */
  122. public function getShortFilename($filename, $maxLength = 20)
  123. {
  124. return strlen($filename) <= $maxLength ? $filename : substr($filename, 0, $maxLength) . '...';
  125. }
  126. /**
  127. * Get storage root directory
  128. *
  129. * @return string
  130. */
  131. public function getStorageRoot()
  132. {
  133. if (null === $this->_storageRoot) {
  134. $this->_storageRoot = implode(
  135. '/',
  136. [$this->_getTheme()->getCustomization()->getCustomizationPath(), $this->getStorageType()]
  137. );
  138. }
  139. return $this->_storageRoot;
  140. }
  141. /**
  142. * Get theme module for custom static files
  143. *
  144. * @return \Magento\Theme\Model\Theme
  145. * @throws \InvalidArgumentException
  146. */
  147. protected function _getTheme()
  148. {
  149. $themeId = $this->_getRequest()->getParam(self::PARAM_THEME_ID);
  150. $theme = $this->_themeFactory->create($themeId);
  151. if (!$themeId || !$theme) {
  152. throw new \InvalidArgumentException('Theme was not found.');
  153. }
  154. return $theme;
  155. }
  156. /**
  157. * Get storage type
  158. *
  159. * @return string
  160. * @throws \Magento\Framework\Exception\LocalizedException
  161. */
  162. public function getStorageType()
  163. {
  164. $allowedTypes = [
  165. \Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT,
  166. \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
  167. ];
  168. $type = (string)$this->_getRequest()->getParam(self::PARAM_CONTENT_TYPE);
  169. if (!in_array($type, $allowedTypes)) {
  170. throw new \Magento\Framework\Exception\LocalizedException(__('Invalid type'));
  171. }
  172. return $type;
  173. }
  174. /**
  175. * Relative url to static content
  176. *
  177. * @return string
  178. */
  179. public function getRelativeUrl()
  180. {
  181. $pathPieces = ['..', $this->getStorageType()];
  182. $node = $this->_getRequest()->getParam(self::PARAM_NODE);
  183. if ($node !== self::NODE_ROOT) {
  184. $node = $this->urlDecoder->decode($node);
  185. $nodes = explode('/', trim($node, '/'));
  186. $pathPieces = array_merge($pathPieces, $nodes);
  187. }
  188. $pathPieces[] = $this->urlDecoder->decode($this->_getRequest()->getParam(self::PARAM_FILENAME));
  189. return implode('/', $pathPieces);
  190. }
  191. /**
  192. * Get current path
  193. *
  194. * @return string
  195. */
  196. public function getCurrentPath()
  197. {
  198. if (!$this->_currentPath) {
  199. $currentPath = $this->getStorageRoot();
  200. $path = $this->_getRequest()->getParam(self::PARAM_NODE);
  201. if ($path && $path !== self::NODE_ROOT) {
  202. $path = $this->convertIdToPath($path);
  203. if ($this->mediaDirectoryWrite->isDirectory($path) && 0 === strpos($path, $currentPath)) {
  204. $currentPath = $this->mediaDirectoryWrite->getRelativePath($path);
  205. }
  206. }
  207. $this->_currentPath = $currentPath;
  208. }
  209. return $this->_currentPath;
  210. }
  211. /**
  212. * Get thumbnail directory for path
  213. *
  214. * @param string $path
  215. * @return string
  216. */
  217. public function getThumbnailDirectory($path)
  218. {
  219. return pathinfo($path, PATHINFO_DIRNAME) . '/' . \Magento\Theme\Model\Wysiwyg\Storage::THUMBNAIL_DIRECTORY;
  220. }
  221. /**
  222. * Get thumbnail path in current directory by image name
  223. *
  224. * @param string $imageName
  225. * @return string
  226. * @throws \InvalidArgumentException
  227. */
  228. public function getThumbnailPath($imageName)
  229. {
  230. $imagePath = $this->getCurrentPath() . '/' . $imageName;
  231. if (!$this->mediaDirectoryWrite->isExist($imagePath) || 0 !== strpos($imagePath, $this->getStorageRoot())) {
  232. throw new \InvalidArgumentException('The image not found.');
  233. }
  234. return $this->getThumbnailDirectory($imagePath) . '/' . pathinfo($imageName, PATHINFO_BASENAME);
  235. }
  236. /**
  237. * Request params for selected theme
  238. *
  239. * @return array
  240. */
  241. public function getRequestParams()
  242. {
  243. $themeId = $this->_getRequest()->getParam(self::PARAM_THEME_ID);
  244. $contentType = $this->_getRequest()->getParam(self::PARAM_CONTENT_TYPE);
  245. $node = $this->_getRequest()->getParam(self::PARAM_NODE);
  246. return [
  247. self::PARAM_THEME_ID => $themeId,
  248. self::PARAM_CONTENT_TYPE => $contentType,
  249. self::PARAM_NODE => $node
  250. ];
  251. }
  252. /**
  253. * Get allowed extensions by type
  254. *
  255. * @return string[]
  256. * @throws \Magento\Framework\Exception\LocalizedException
  257. */
  258. public function getAllowedExtensionsByType()
  259. {
  260. return $this->getStorageType() == \Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT
  261. ? ['ttf', 'otf', 'eot', 'svg', 'woff']
  262. : ['jpg', 'jpeg', 'gif', 'png', 'xbm', 'wbmp'];
  263. }
  264. /**
  265. * Get storage type name for display.
  266. *
  267. * @return string
  268. * @throws \Magento\Framework\Exception\LocalizedException
  269. */
  270. public function getStorageTypeName()
  271. {
  272. return $this->getStorageType() == \Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT
  273. ? self::FONTS
  274. : self::IMAGES;
  275. }
  276. /**
  277. * Get session model
  278. *
  279. * @return \Magento\Backend\Model\Session
  280. */
  281. public function getSession()
  282. {
  283. return $this->_session;
  284. }
  285. }