_backendData = $backendData; $this->_storeManager = $storeManager; $this->escaper = $escaper; $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); $this->_directory->create($this->getStorageRoot()); } /** * Set a specified store ID value * * @param int $store * @return $this */ public function setStoreId($store) { $this->_storeId = $store; return $this; } /** * Images Storage root directory * * @return string */ public function getStorageRoot() { return $this->_directory->getAbsolutePath($this->getStorageRootSubpath()); } /** * Get image storage root subpath. User is unable to traverse outside of this subpath in media gallery * * @return string */ public function getStorageRootSubpath() { return ''; } /** * Images Storage base URL * * @return string */ public function getBaseUrl() { return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); } /** * Ext Tree node key name * * @return string */ public function getTreeNodeName() { return 'node'; } /** * Encode path to HTML element id * * @param string $path Path to file/directory * @return string */ public function convertPathToId($path) { $path = str_replace($this->getStorageRoot(), '', $path); return $this->idEncode($path); } /** * Decode HTML element id. * * @param string $id * @return string * @throws \InvalidArgumentException When path contains restricted symbols. */ public function convertIdToPath($id) { if ($id === \Magento\Theme\Helper\Storage::NODE_ROOT) { return $this->getStorageRoot(); } else { $path = $this->getStorageRoot() . $this->idDecode($id); if (preg_match('/\.\.(\\\|\/)/', $path)) { throw new \InvalidArgumentException('Path is invalid'); } return $path; } } /** * Check whether using static URLs is allowed * * @return bool */ public function isUsingStaticUrlsAllowed() { $checkResult = (object) []; $checkResult->isAllowed = false; $this->_eventManager->dispatch( 'cms_wysiwyg_images_static_urls_allowed', ['result' => $checkResult, 'store_id' => $this->_storeId] ); return $checkResult->isAllowed; } /** * Prepare Image insertion declaration for Wysiwyg or textarea(as_is mode) * * @param string $filename Filename transferred via Ajax * @param bool $renderAsTag Leave image HTML as is or transform it to controller directive * @return string */ public function getImageHtmlDeclaration($filename, $renderAsTag = false) { $fileUrl = $this->getCurrentUrl() . $filename; $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); $mediaPath = str_replace($mediaUrl, '', $fileUrl); $directive = sprintf('{{media url="%s"}}', $mediaPath); if ($renderAsTag) { $src = $this->isUsingStaticUrlsAllowed() ? $fileUrl : $this->escaper->escapeHtml($directive); $html = sprintf('', $src); } else { if ($this->isUsingStaticUrlsAllowed()) { $html = $fileUrl; } else { $directive = $this->urlEncoder->encode($directive); $html = $this->_backendData->getUrl( 'cms/wysiwyg/directive', [ '___directive' => $directive, '_escape_params' => false, ] ); } } return $html; } /** * Return path of the current selected directory or root directory for startup * Try to create target directory if it doesn't exist * * @return string * @throws \Magento\Framework\Exception\LocalizedException */ public function getCurrentPath() { if (!$this->_currentPath) { $currentPath = $this->getStorageRoot(); $path = $this->_getRequest()->getParam($this->getTreeNodeName()); if ($path) { $path = $this->convertIdToPath($path); if ($this->_directory->isDirectory($this->_directory->getRelativePath($path))) { $currentPath = $path; } } try { $currentDir = $this->_directory->getRelativePath($currentPath); if (!$this->_directory->isExist($currentDir)) { $this->_directory->create($currentDir); } } catch (\Magento\Framework\Exception\FileSystemException $e) { $message = __('The directory %1 is not writable by server.', $currentPath); throw new \Magento\Framework\Exception\LocalizedException($message); } $this->_currentPath = $currentPath; } return $this->_currentPath; } /** * Return URL based on current selected directory or root directory for startup * * @return string */ public function getCurrentUrl() { if (!$this->_currentUrl) { $path = $this->getCurrentPath(); $mediaUrl = $this->_storeManager->getStore( $this->_storeId )->getBaseUrl( \Magento\Framework\UrlInterface::URL_TYPE_MEDIA ); $this->_currentUrl = rtrim($mediaUrl . $this->_directory->getRelativePath($path), '/') . '/'; } return $this->_currentUrl; } /** * Encode string to valid HTML id element, based on base64 encoding * * @param string $string * @return string */ public function idEncode($string) { return strtr(base64_encode($string), '+/=', ':_-'); } /** * Revert operation to idEncode * * @param string $string * @return string */ public function idDecode($string) { $string = strtr($string, ':_-', '+/='); return base64_decode($string); } /** * Reduce filename by replacing some characters with dots * * @param string $filename * @param int $maxLength Maximum filename * @return string Truncated filename */ public function getShortFilename($filename, $maxLength = 20) { if (strlen($filename) <= $maxLength) { return $filename; } return substr($filename, 0, $maxLength) . '...'; } /** * Set user-traversable image directory subpath relative to media directory and relative to nested storage root * * @var string $subpath * @return void */ public function setImageDirectorySubpath($subpath) { $this->imageDirectorySubpath = $subpath; } }