Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Storage.php
Go to the documentation of this file.
1 <?php
11 
13 
19 {
23  const PARAM_NODE = 'node';
24 
28  const PARAM_CONTENT_TYPE = 'content_type';
29 
33  const PARAM_THEME_ID = 'theme_id';
34 
38  const PARAM_FILENAME = 'filename';
39 
43  const NODE_ROOT = 'root';
44 
48  const IMAGES = 'Images';
49 
53  const FONTS = 'Fonts';
54 
60  protected $_currentPath;
61 
67  protected $_storageRoot;
68 
74  protected $filesystem;
75 
79  protected $_session;
80 
84  protected $_themeFactory;
85 
90 
97  public function __construct(
98  \Magento\Framework\App\Helper\Context $context,
99  \Magento\Framework\Filesystem $filesystem,
100  \Magento\Backend\Model\Session $session,
101  \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory
102  ) {
103  parent::__construct($context);
104  $this->filesystem = $filesystem;
105  $this->_session = $session;
106  $this->_themeFactory = $themeFactory;
107  $this->mediaDirectoryWrite = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
108  $this->mediaDirectoryWrite->create($this->mediaDirectoryWrite->getRelativePath($this->getStorageRoot()));
109  }
110 
117  public function convertPathToId($path)
118  {
119  $path = str_replace($this->getStorageRoot(), '', $path);
120  return $this->urlEncoder->encode($path);
121  }
122 
129  public function convertIdToPath($value)
130  {
131  $path = $this->urlDecoder->decode($value);
132  if (!strstr($path, $this->getStorageRoot())) {
133  $path = $this->getStorageRoot() . $path;
134  }
135  return $path;
136  }
137 
145  public function getShortFilename($filename, $maxLength = 20)
146  {
147  return strlen($filename) <= $maxLength ? $filename : substr($filename, 0, $maxLength) . '...';
148  }
149 
155  public function getStorageRoot()
156  {
157  if (null === $this->_storageRoot) {
158  $this->_storageRoot = implode(
159  '/',
160  [$this->_getTheme()->getCustomization()->getCustomizationPath(), $this->getStorageType()]
161  );
162  }
163  return $this->_storageRoot;
164  }
165 
172  protected function _getTheme()
173  {
174  $themeId = $this->_getRequest()->getParam(self::PARAM_THEME_ID);
175  $theme = $this->_themeFactory->create($themeId);
176  if (!$themeId || !$theme) {
177  throw new \InvalidArgumentException('Theme was not found.');
178  }
179  return $theme;
180  }
181 
188  public function getStorageType()
189  {
190  $allowedTypes = [
193  ];
194  $type = (string)$this->_getRequest()->getParam(self::PARAM_CONTENT_TYPE);
195  if (!in_array($type, $allowedTypes)) {
196  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid type'));
197  }
198  return $type;
199  }
200 
206  public function getRelativeUrl()
207  {
208  $pathPieces = ['..', $this->getStorageType()];
209  $node = $this->_getRequest()->getParam(self::PARAM_NODE);
210  if ($node !== self::NODE_ROOT) {
211  $node = $this->urlDecoder->decode($node);
212  $nodes = explode('/', trim($node, '/'));
213  $pathPieces = array_merge($pathPieces, $nodes);
214  }
215  $pathPieces[] = $this->urlDecoder->decode($this->_getRequest()->getParam(self::PARAM_FILENAME));
216  return implode('/', $pathPieces);
217  }
218 
224  public function getCurrentPath()
225  {
226  if (!$this->_currentPath) {
227  $currentPath = $this->getStorageRoot();
228  $path = $this->_getRequest()->getParam(self::PARAM_NODE);
229  if ($path && $path !== self::NODE_ROOT) {
230  $path = $this->convertIdToPath($path);
231 
232  if ($this->mediaDirectoryWrite->isDirectory($path) && 0 === strpos($path, $currentPath)) {
233  $currentPath = $this->mediaDirectoryWrite->getRelativePath($path);
234  }
235  }
236  $this->_currentPath = $currentPath;
237  }
238  return $this->_currentPath;
239  }
240 
247  public function getThumbnailDirectory($path)
248  {
249  return pathinfo($path, PATHINFO_DIRNAME) . '/' . \Magento\Theme\Model\Wysiwyg\Storage::THUMBNAIL_DIRECTORY;
250  }
251 
259  public function getThumbnailPath($imageName)
260  {
261  $imagePath = $this->getCurrentPath() . '/' . $imageName;
262  if (!$this->mediaDirectoryWrite->isExist($imagePath) || 0 !== strpos($imagePath, $this->getStorageRoot())) {
263  throw new \InvalidArgumentException('The image not found.');
264  }
265  return $this->getThumbnailDirectory($imagePath) . '/' . pathinfo($imageName, PATHINFO_BASENAME);
266  }
267 
273  public function getRequestParams()
274  {
275  $themeId = $this->_getRequest()->getParam(self::PARAM_THEME_ID);
276  $contentType = $this->_getRequest()->getParam(self::PARAM_CONTENT_TYPE);
277  $node = $this->_getRequest()->getParam(self::PARAM_NODE);
278  return [
279  self::PARAM_THEME_ID => $themeId,
280  self::PARAM_CONTENT_TYPE => $contentType,
281  self::PARAM_NODE => $node
282  ];
283  }
284 
291  public function getAllowedExtensionsByType()
292  {
294  ? ['ttf', 'otf', 'eot', 'svg', 'woff']
295  : ['jpg', 'jpeg', 'gif', 'png', 'xbm', 'wbmp'];
296  }
297 
304  public function getStorageTypeName()
305  {
307  ? self::FONTS
308  : self::IMAGES;
309  }
310 
316  public function getSession()
317  {
318  return $this->_session;
319  }
320 }
__()
Definition: __.php:13
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\Filesystem $filesystem, \Magento\Backend\Model\Session $session, \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory)
Definition: Storage.php:97
getShortFilename($filename, $maxLength=20)
Definition: Storage.php:145
$theme
getThumbnailPath($imageName)
Definition: Storage.php:259