123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Theme storage helper
- */
- namespace Magento\Theme\Helper;
- use Magento\Framework\App\Filesystem\DirectoryList;
- /**
- * @api
- * @since 100.0.2
- */
- class Storage extends \Magento\Framework\App\Helper\AbstractHelper
- {
- /**
- * Parameter name of node
- */
- const PARAM_NODE = 'node';
- /**
- * Parameter name of content type
- */
- const PARAM_CONTENT_TYPE = 'content_type';
- /**
- * Parameter name of theme identification number
- */
- const PARAM_THEME_ID = 'theme_id';
- /**
- * Parameter name of filename
- */
- const PARAM_FILENAME = 'filename';
- /**
- * Root node value identification number
- */
- const NODE_ROOT = 'root';
- /**
- * Display name for images storage type
- */
- const IMAGES = 'Images';
- /**
- * Display name for fonts storage type
- */
- const FONTS = 'Fonts';
- /**
- * Current directory path
- *
- * @var string
- */
- protected $_currentPath;
- /**
- * Current storage root path
- *
- * @var string
- */
- protected $_storageRoot;
- /**
- * Magento filesystem
- *
- * @var \Magento\Framework\Filesystem
- */
- protected $filesystem;
- /**
- * @var \Magento\Backend\Model\Session
- */
- protected $_session;
- /**
- * @var \Magento\Framework\View\Design\Theme\FlyweightFactory
- */
- protected $_themeFactory;
- /**
- * @var \Magento\Framework\Filesystem\Directory\Write
- */
- protected $mediaDirectoryWrite;
- /**
- * @param \Magento\Framework\App\Helper\Context $context
- * @param \Magento\Framework\Filesystem $filesystem
- * @param \Magento\Backend\Model\Session $session
- * @param \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory
- */
- public function __construct(
- \Magento\Framework\App\Helper\Context $context,
- \Magento\Framework\Filesystem $filesystem,
- \Magento\Backend\Model\Session $session,
- \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory
- ) {
- parent::__construct($context);
- $this->filesystem = $filesystem;
- $this->_session = $session;
- $this->_themeFactory = $themeFactory;
- $this->mediaDirectoryWrite = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
- $this->mediaDirectoryWrite->create($this->mediaDirectoryWrite->getRelativePath($this->getStorageRoot()));
- }
- /**
- * Convert path to id
- *
- * @param string $path
- * @return string
- */
- public function convertPathToId($path)
- {
- $path = str_replace($this->getStorageRoot(), '', $path);
- return $this->urlEncoder->encode($path);
- }
- /**
- * Convert id to path
- *
- * @param string $value
- * @return string
- */
- public function convertIdToPath($value)
- {
- $path = $this->urlDecoder->decode($value);
- if (!strstr($path, $this->getStorageRoot())) {
- $path = $this->getStorageRoot() . $path;
- }
- return $path;
- }
- /**
- * Get short file name
- *
- * @param string $filename
- * @param int $maxLength
- * @return string
- */
- public function getShortFilename($filename, $maxLength = 20)
- {
- return strlen($filename) <= $maxLength ? $filename : substr($filename, 0, $maxLength) . '...';
- }
- /**
- * Get storage root directory
- *
- * @return string
- */
- public function getStorageRoot()
- {
- if (null === $this->_storageRoot) {
- $this->_storageRoot = implode(
- '/',
- [$this->_getTheme()->getCustomization()->getCustomizationPath(), $this->getStorageType()]
- );
- }
- return $this->_storageRoot;
- }
- /**
- * Get theme module for custom static files
- *
- * @return \Magento\Theme\Model\Theme
- * @throws \InvalidArgumentException
- */
- protected function _getTheme()
- {
- $themeId = $this->_getRequest()->getParam(self::PARAM_THEME_ID);
- $theme = $this->_themeFactory->create($themeId);
- if (!$themeId || !$theme) {
- throw new \InvalidArgumentException('Theme was not found.');
- }
- return $theme;
- }
- /**
- * Get storage type
- *
- * @return string
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getStorageType()
- {
- $allowedTypes = [
- \Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT,
- \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
- ];
- $type = (string)$this->_getRequest()->getParam(self::PARAM_CONTENT_TYPE);
- if (!in_array($type, $allowedTypes)) {
- throw new \Magento\Framework\Exception\LocalizedException(__('Invalid type'));
- }
- return $type;
- }
- /**
- * Relative url to static content
- *
- * @return string
- */
- public function getRelativeUrl()
- {
- $pathPieces = ['..', $this->getStorageType()];
- $node = $this->_getRequest()->getParam(self::PARAM_NODE);
- if ($node !== self::NODE_ROOT) {
- $node = $this->urlDecoder->decode($node);
- $nodes = explode('/', trim($node, '/'));
- $pathPieces = array_merge($pathPieces, $nodes);
- }
- $pathPieces[] = $this->urlDecoder->decode($this->_getRequest()->getParam(self::PARAM_FILENAME));
- return implode('/', $pathPieces);
- }
- /**
- * Get current path
- *
- * @return string
- */
- public function getCurrentPath()
- {
- if (!$this->_currentPath) {
- $currentPath = $this->getStorageRoot();
- $path = $this->_getRequest()->getParam(self::PARAM_NODE);
- if ($path && $path !== self::NODE_ROOT) {
- $path = $this->convertIdToPath($path);
- if ($this->mediaDirectoryWrite->isDirectory($path) && 0 === strpos($path, $currentPath)) {
- $currentPath = $this->mediaDirectoryWrite->getRelativePath($path);
- }
- }
- $this->_currentPath = $currentPath;
- }
- return $this->_currentPath;
- }
- /**
- * Get thumbnail directory for path
- *
- * @param string $path
- * @return string
- */
- public function getThumbnailDirectory($path)
- {
- return pathinfo($path, PATHINFO_DIRNAME) . '/' . \Magento\Theme\Model\Wysiwyg\Storage::THUMBNAIL_DIRECTORY;
- }
- /**
- * Get thumbnail path in current directory by image name
- *
- * @param string $imageName
- * @return string
- * @throws \InvalidArgumentException
- */
- public function getThumbnailPath($imageName)
- {
- $imagePath = $this->getCurrentPath() . '/' . $imageName;
- if (!$this->mediaDirectoryWrite->isExist($imagePath) || 0 !== strpos($imagePath, $this->getStorageRoot())) {
- throw new \InvalidArgumentException('The image not found.');
- }
- return $this->getThumbnailDirectory($imagePath) . '/' . pathinfo($imageName, PATHINFO_BASENAME);
- }
- /**
- * Request params for selected theme
- *
- * @return array
- */
- public function getRequestParams()
- {
- $themeId = $this->_getRequest()->getParam(self::PARAM_THEME_ID);
- $contentType = $this->_getRequest()->getParam(self::PARAM_CONTENT_TYPE);
- $node = $this->_getRequest()->getParam(self::PARAM_NODE);
- return [
- self::PARAM_THEME_ID => $themeId,
- self::PARAM_CONTENT_TYPE => $contentType,
- self::PARAM_NODE => $node
- ];
- }
- /**
- * Get allowed extensions by type
- *
- * @return string[]
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getAllowedExtensionsByType()
- {
- return $this->getStorageType() == \Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT
- ? ['ttf', 'otf', 'eot', 'svg', 'woff']
- : ['jpg', 'jpeg', 'gif', 'png', 'xbm', 'wbmp'];
- }
- /**
- * Get storage type name for display.
- *
- * @return string
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function getStorageTypeName()
- {
- return $this->getStorageType() == \Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT
- ? self::FONTS
- : self::IMAGES;
- }
- /**
- * Get session model
- *
- * @return \Magento\Backend\Model\Session
- */
- public function getSession()
- {
- return $this->_session;
- }
- }
|