156 \
Magento\Backend\Model\UrlInterface $backendUrl,
158 \
Magento\MediaStorage\Helper\File\
Storage\Database $coreFileStorageDb,
160 \
Magento\Framework\Image\AdapterFactory $imageFactory,
161 \
Magento\Framework\View\Asset\Repository $assetRepo,
163 \
Magento\MediaStorage\Model\File\
Storage\FileFactory $storageFileFactory,
164 \
Magento\MediaStorage\Model\File\
Storage\DatabaseFactory $storageDatabaseFactory,
165 \
Magento\MediaStorage\Model\File\
Storage\Directory\DatabaseFactory $directoryDatabaseFactory,
166 \
Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
167 array $resizeParameters = [],
168 array $extensions = [],
173 $this->_backendUrl = $backendUrl;
174 $this->_cmsWysiwygImages = $cmsWysiwygImages;
175 $this->_coreFileStorageDb = $coreFileStorageDb;
177 $this->_imageFactory = $imageFactory;
178 $this->_assetRepo = $assetRepo;
179 $this->_storageCollectionFactory = $storageCollectionFactory;
180 $this->_storageFileFactory = $storageFileFactory;
181 $this->_storageDatabaseFactory = $storageDatabaseFactory;
182 $this->_directoryDatabaseFactory = $directoryDatabaseFactory;
183 $this->_uploaderFactory = $uploaderFactory;
184 $this->_resizeParameters = $resizeParameters;
185 $this->_extensions = $extensions;
186 $this->_dirs = $dirs;
187 parent::__construct(
$data);
196 protected function createSubDirectories(
$path)
198 if ($this->_coreFileStorageDb->checkDbUsage()) {
200 $subDirectories = $this->_directoryDatabaseFactory->create();
201 $directories = $subDirectories->getSubdirectories(
$path);
202 foreach ($directories as $directory) {
203 $fullPath = rtrim(
$path,
'/') .
'/' . $directory[
'name'];
204 $this->_directory->create($fullPath);
216 $conditions = [
'reg_exp' => [],
'plain' => []];
218 if ($this->_dirs[
'exclude']) {
219 foreach ($this->_dirs[
'exclude'] as $dir) {
220 $conditions[!empty($dir[
'regexp']) ?
'reg_exp' :
'plain'][$dir[
'name']] =
true;
225 if ($this->_dirs[
'include']) {
226 foreach ($this->_dirs[
'include'] as $dir) {
227 unset($conditions[
'reg_exp'][$dir[
'name']], $conditions[
'plain'][$dir[
'name']]);
243 $regExp = $conditions[
'reg_exp'] ?
'~' . implode(
'|', array_keys($conditions[
'reg_exp'])) .
'~i' :
null;
244 $storageRoot = $this->_cmsWysiwygImages->getStorageRoot();
245 $storageRootLength = strlen($storageRoot);
248 $mediaSubPathname = substr(
$value->getFilename(), $storageRootLength);
249 $rootChildParts = explode(
'/',
'/' . ltrim($mediaSubPathname,
'/'));
251 if (array_key_exists($rootChildParts[1], $conditions[
'plain'])
252 || ($regExp && preg_match($regExp,
$value->getFilename()))) {
268 $this->createSubDirectories(
$path);
271 ->setCollectDirs(
true)
272 ->setCollectFiles(
false)
273 ->setCollectRecursively(
false);
287 public function getFilesCollection(
$path,
$type =
null)
289 if ($this->_coreFileStorageDb->checkDbUsage()) {
290 $files = $this->_storageDatabaseFactory->create()->getDirectoryFiles(
$path);
293 $fileStorageModel = $this->_storageFileFactory->create();
294 foreach (
$files as $file) {
295 $fileStorageModel->saveFile($file);
305 )->setCollectRecursively(
314 $collection->setFilesFilter(
'/\.(' . implode(
'|', $allowed) .
')$/i');
319 $item->setId($this->_cmsWysiwygImages->idEncode(
$item->getBasename()));
321 $item->setShortName($this->_cmsWysiwygImages->getShortFilename(
$item->getBasename()));
322 $item->setUrl($this->_cmsWysiwygImages->getCurrentUrl() .
$item->getBasename());
326 if ($this->
isImage($item->getBasename())) {
330 $thumbUrl = $this->_backendUrl->getUrl(
'cms/*/thumbnail', [
'file' =>
$item->getId()]);
335 if (is_array($size)) {
336 $item->setWidth($size[0]);
337 $item->setHeight($size[1]);
340 $thumbUrl = $this->_assetRepo->getUrl(self::THUMB_PLACEHOLDER_PATH_SUFFIX);
343 $item->setThumbUrl($thumbUrl);
355 public function getCollection(
$path =
null)
358 $collection = $this->_storageCollectionFactory->create();
359 if (
$path !==
null) {
375 if (!preg_match(self::DIRECTORY_NAME_REGEXP,
$name)) {
376 throw new \Magento\Framework\Exception\LocalizedException(
377 __(
'Please rename the folder using only letters, numbers, underscores and dashes.')
383 $path = $this->_cmsWysiwygImages->getStorageRoot();
387 $relativeNewPath = $this->_directory->getRelativePath($newPath);
388 if ($this->_directory->isDirectory($relativeNewPath)) {
389 throw new \Magento\Framework\Exception\LocalizedException(
390 __(
'We found a directory with the same name. Please try another folder name.')
394 $this->_directory->create($relativeNewPath);
396 if ($this->_coreFileStorageDb->checkDbUsage()) {
397 $relativePath = $this->_coreFileStorageDb->getMediaRelativePath($newPath);
398 $this->_directoryDatabaseFactory->create()->createRecursive(
$relativePath);
403 'short_name' => $this->_cmsWysiwygImages->getShortFilename(
$name),
405 'id' => $this->_cmsWysiwygImages->convertPathToId($newPath),
409 throw new \Magento\Framework\Exception\LocalizedException(
__(
'We cannot create a new directory.'));
422 if ($this->_coreFileStorageDb->checkDbUsage()) {
423 $this->_directoryDatabaseFactory->create()->deleteDirectory(
$path);
430 throw new \Magento\Framework\Exception\LocalizedException(
__(
'We cannot delete directory %1.',
$path));
445 $this->_directory->delete($this->_directory->getRelativePath(
$path));
461 $this->_coreFileStorageDb->deleteFile(
$target);
464 $relativePathThumb = $this->_directory->getRelativePath($thumb);
466 if ($this->_directory->isFile($relativePathThumb)) {
467 $this->_directory->delete($relativePathThumb);
469 $this->_coreFileStorageDb->deleteFile($thumb);
482 public function uploadFile($targetPath,
$type =
null)
485 $uploader = $this->_uploaderFactory->create([
'fileId' =>
'image']);
488 $uploader->setAllowedExtensions($allowed);
490 $uploader->setAllowRenameFiles(
true);
491 $uploader->setFilesDispersion(
false);
492 if (!$uploader->checkMimeType($this->getAllowedMimeTypes(
$type))) {
493 throw new \Magento\Framework\Exception\LocalizedException(
__(
'File validation failed.'));
495 $result = $uploader->save($targetPath);
498 throw new \Magento\Framework\Exception\LocalizedException(
__(
'We can\'t upload the file right now.'));
502 $this->
resizeFile($targetPath .
'/' . $uploader->getUploadedFileName(),
true);
516 $mediaRootDir = $this->_cmsWysiwygImages->getStorageRoot();
518 if (strpos($filePath, $mediaRootDir) === 0) {
519 $thumbPath = $this->
getThumbnailRoot() . substr($filePath, strlen($mediaRootDir));
521 if (!$checkFile || $this->_directory->isExist($this->_directory->getRelativePath($thumbPath))) {
538 $mediaRootDir = $this->_cmsWysiwygImages->getStorageRoot();
540 if (strpos($filePath, $mediaRootDir) === 0) {
541 $thumbSuffix = self::THUMBS_DIRECTORY_NAME . substr($filePath, strlen($mediaRootDir));
542 if (!$checkFile || $this->_directory->isExist(
543 $this->_directory->getRelativePath($mediaRootDir .
'/' . $thumbSuffix)
546 $thumbSuffix = substr(
548 strlen($this->_directory->getAbsolutePath())
549 ) .
'/' . $thumbSuffix;
550 $randomIndex =
'?rand=' .
time();
551 return str_replace(
'\\',
'/', $this->_cmsWysiwygImages->getBaseUrl() . $thumbSuffix) . $randomIndex;
567 $realPath = $this->_directory->getRelativePath(
$source);
568 if (!$this->_directory->isFile($realPath) || !$this->_directory->isExist($realPath)) {
573 $pathTargetDir = $this->_directory->getRelativePath($targetDir);
574 if (!$this->_directory->isExist($pathTargetDir)) {
575 $this->_directory->create($pathTargetDir);
577 if (!$this->_directory->isExist($pathTargetDir)) {
580 $image = $this->_imageFactory->create();
582 $image->keepAspectRatio($keepRatio);
583 $image->resize($this->_resizeParameters[
'width'], $this->_resizeParameters[
'height']);
584 $dest = $targetDir .
'/' . pathinfo(
$source, PATHINFO_BASENAME);
586 if ($this->_directory->isFile($this->_directory->getRelativePath($dest))) {
602 $path = $this->_cmsWysiwygImages->getCurrentPath();
615 $mediaRootDir = $this->_cmsWysiwygImages->getStorageRoot();
618 if ($filePath && strpos($filePath, $mediaRootDir) === 0) {
619 $thumbnailDir .= dirname(substr($filePath, strlen($mediaRootDir)));
622 return $thumbnailDir;
643 $allowed = $this->getExtensionsList(
$type);
645 return array_keys(array_filter($allowed));
666 if (!$this->
hasData(
'_image_extensions')) {
669 $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
670 return in_array($ext, $this->
_getData(
'_image_extensions'));
680 return $this->_resizeParameters[
'width'];
690 return $this->_resizeParameters[
'height'];
712 $root = $this->
_sanitizePath($this->_cmsWysiwygImages->getStorageRoot());
713 if ($root ==
$path) {
714 throw new \Magento\Framework\Exception\LocalizedException(
715 __(
'We can\'t delete root directory %1 right now.',
$path)
718 if (strpos(
$path, $root) !== 0) {
719 throw new \Magento\Framework\Exception\LocalizedException(
720 __(
'Directory %1 is not under storage root path.',
$path)
733 return rtrim(preg_replace(
'~[/\\\]+~',
'/', $this->_directory->getDriver()->getRealPathSafety(
$path)),
'/');
746 strlen($this->
_sanitizePath($this->_cmsWysiwygImages->getStorageRoot()))
756 private function getAllowedMimeTypes(
$type =
null): array
758 $allowed = $this->getExtensionsList(
$type);
760 return array_values(array_filter($allowed));
769 private function getExtensionsList(
$type =
null): array
771 if (is_string(
$type) && array_key_exists(
"{$type}_allowed", $this->_extensions)) {
772 $allowed = $this->_extensions[
"{$type}_allowed"];
774 $allowed = $this->_extensions[
'allowed'];
const THUMB_PLACEHOLDER_PATH_SUFFIX
resizeOnTheFly($filename)
__construct(\Magento\Backend\Model\Session $session, \Magento\Backend\Model\UrlInterface $backendUrl, \Magento\Cms\Helper\Wysiwyg\Images $cmsWysiwygImages, \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Image\AdapterFactory $imageFactory, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Cms\Model\Wysiwyg\Images\Storage\CollectionFactory $storageCollectionFactory, \Magento\MediaStorage\Model\File\Storage\FileFactory $storageFileFactory, \Magento\MediaStorage\Model\File\Storage\DatabaseFactory $storageDatabaseFactory, \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory $directoryDatabaseFactory, \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory, array $resizeParameters=[], array $extensions=[], array $dirs=[], array $data=[])
getThumbsPath($filePath=false)
getThumbnailPath($filePath, $checkFile=false)
getAllowedExtensions($type=null)
const THUMBS_DIRECTORY_NAME
const DIRECTORY_NAME_REGEXP
removeItemFromCollection($collection, $conditions)
resizeFile($source, $keepRatio=true)
createDirectory($name, $path)
$_storageCollectionFactory
$_directoryDatabaseFactory
_getRelativePathToRoot($path)
getThumbnailUrl($filePath, $checkFile=false)
setData($key, $value=null)
foreach($appDirs as $dir) $files
getConditionsForExcludeDirs()
if(!isset($_GET['name'])) $name