Skip to content

Commit

Permalink
B2B-1876: New directories in pub/media do not get synced back to loca…
Browse files Browse the repository at this point in the history
…l filesystem from S3 Remote Storage
  • Loading branch information
joanhe committed Aug 3, 2021
1 parent 588c5ad commit 33aaa42
Showing 1 changed file with 18 additions and 36 deletions.
54 changes: 18 additions & 36 deletions app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public function getDirsCollection($path)
->setCollectRecursively(false)
->setOrder('basename', \Magento\Framework\Data\Collection\Filesystem::SORT_ORDER_ASC);

if (preg_match($this->getAllowedTargetDirPattern(), $path) != 1) {
if (!$this->isPathAllowed($path)) {
$collection->setDirsFilter($this->getAllowedDirMask($path));
}

Expand Down Expand Up @@ -461,7 +461,7 @@ public function getCollection($path = null)
*/
public function createDirectory($name, $path)
{
if (!($this->isPathAllowed($path) || preg_match($this->getAllowedTargetDirPattern(), $path))) {
if (!($this->isPathAllowed($path . DIRECTORY_SEPARATOR . $name))) {
throw new \Magento\Framework\Exception\LocalizedException(
__('We cannot create the folder under the selected directory.')
);
Expand Down Expand Up @@ -514,7 +514,7 @@ public function createDirectory($name, $path)
*/
public function deleteDirectory($path)
{
if (!$this->isPathAllowed($path)) {
if (!$this->isPathAllowed(dirname($path))) {
throw new \Magento\Framework\Exception\LocalizedException(
__('We cannot delete the selected directory.')
);
Expand Down Expand Up @@ -563,7 +563,7 @@ protected function _deleteByPath($path)
*/
public function deleteFile($target)
{
if (!$this->isPathAllowed($target)) {
if (!$this->isPathAllowed(dirname($target))) {
throw new \Magento\Framework\Exception\LocalizedException(
__('We can\'t delete the file right now.')
);
Expand Down Expand Up @@ -595,7 +595,7 @@ public function deleteFile($target)
*/
public function uploadFile($targetPath, $type = null)
{
if (!($this->isPathAllowed($targetPath) || preg_match($this->getAllowedTargetDirPattern(), $targetPath))) {
if (!($this->isPathAllowed($targetPath))) {
throw new \Magento\Framework\Exception\LocalizedException(
__('We can\'t upload the file to the current folder right now. Please try another folder.')
);
Expand Down Expand Up @@ -954,7 +954,14 @@ private function getExtensionsList($type = null): array
*/
private function isPathAllowed($path): bool
{
return preg_match($this->getAllowedPathPattern(), $path) == 1;
$storageRoot = $this->_cmsWysiwygImages->getStorageRoot();
$storageRootLength = strlen($storageRoot);
$mediaSubPathname = substr($path, $storageRootLength) ?: '';
if (!$mediaSubPathname) {
return false;
}
$mediaSubPathname = ltrim($mediaSubPathname, DIRECTORY_SEPARATOR);
return preg_match($this->getAllowedPathPattern(), $mediaSubPathname) == 1;
}

/**
Expand All @@ -969,44 +976,19 @@ private function getAllowedPathPattern()
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
'default'
);
$regExp = '~';
$regExp = '/^(';
$or = '';
foreach($mediaGalleryImageFolders as $folder) {
$folderPattern = str_replace('/', '[/\\\]+', $folder);
$regExp .= $or . 'media[/\\\]+' . $folderPattern . '[/\\\]+[a-z0-9\.\-\_]+';
$folderPattern = str_replace('/', '[\/]+', $folder);
$regExp .= $or . $folderPattern . '\b(?:\/?[^\/]+)*\/?$';
$or = '|';
}
$regExp .= '~i';
$regExp .= ')/';
$this->allowedPathPattern = $regExp;
}
return $this->allowedPathPattern;
}

/**
* Get allowed target dir pattern
*
* @return string
*/
private function getAllowedTargetDirPattern()
{
if (null === $this->allowedTargetDirPattern) {
$mediaGalleryImageFolders = $this->coreConfig->getValue(
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
'default'
);
$regExp = '~';
$or = '';
foreach($mediaGalleryImageFolders as $folder) {
$folderPattern = str_replace('/', '[/\\\]+', $folder);
$regExp .= $or . 'media[/\\\]+' . $folderPattern . '[/\\\]*$';
$or = '|';
}
$regExp .= '~i';
$this->allowedTargetDirPattern = $regExp;
}
return $this->allowedTargetDirPattern;
}

/**
* Get allowed media gallery image folders
*
Expand All @@ -1028,7 +1010,7 @@ private function getAllowedDirs(): array

$this->allowedDirs = [];
foreach ($imageFolders as $folder) {
$this->allowedDirs[] = explode('/', $folder);
$this->allowedDirs[] = explode(DIRECTORY_SEPARATOR, $folder);
}
}
return $this->allowedDirs;
Expand Down

0 comments on commit 33aaa42

Please sign in to comment.