Skip to content

Commit b5799c4

Browse files
committed
MC-31733: Media gallery breaks in some filesystems
1 parent cccd668 commit b5799c4

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

app/code/Magento/Theme/Helper/Storage.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Magento\Framework\App\Filesystem\DirectoryList;
1313
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\Filesystem\DriverInterface;
1415

1516
/**
1617
* Handles the storage of media files like images and fonts.
@@ -97,6 +98,10 @@ class Storage extends \Magento\Framework\App\Helper\AbstractHelper
9798
* @var \Magento\Framework\Filesystem\Io\File
9899
*/
99100
private $file;
101+
/**
102+
* @var DriverInterface
103+
*/
104+
private $filesystemDriver;
100105

101106
/**
102107
* @param \Magento\Framework\App\Helper\Context $context
@@ -105,6 +110,7 @@ class Storage extends \Magento\Framework\App\Helper\AbstractHelper
105110
* @param \Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory
106111
* @param \Magento\Framework\Filesystem\Io\File|null $file
107112
*
113+
* @param DriverInterface|null $filesystemDriver
108114
* @throws \Magento\Framework\Exception\FileSystemException
109115
* @throws \Magento\Framework\Exception\ValidatorException
110116
*/
@@ -113,7 +119,8 @@ public function __construct(
113119
\Magento\Framework\Filesystem $filesystem,
114120
\Magento\Backend\Model\Session $session,
115121
\Magento\Framework\View\Design\Theme\FlyweightFactory $themeFactory,
116-
\Magento\Framework\Filesystem\Io\File $file = null
122+
\Magento\Framework\Filesystem\Io\File $file = null,
123+
DriverInterface $filesystemDriver = null
117124
) {
118125
parent::__construct($context);
119126
$this->filesystem = $filesystem;
@@ -124,6 +131,7 @@ public function __construct(
124131
$this->file = $file ?: ObjectManager::getInstance()->get(
125132
\Magento\Framework\Filesystem\Io\File::class
126133
);
134+
$this->filesystemDriver = $filesystemDriver ?: ObjectManager::getInstance()->get(DriverInterface::class);
127135
}
128136

129137
/**
@@ -247,7 +255,20 @@ public function getCurrentPath()
247255
if ($path && $path !== self::NODE_ROOT) {
248256
$path = $this->convertIdToPath($path);
249257

250-
if ($this->mediaDirectoryWrite->isDirectory($path) && 0 === strpos($path, (string) $currentPath)) {
258+
$realPath = $this->filesystemDriver->getRealPath(
259+
$this->filesystemDriver->getRealPathSafety($path)
260+
);
261+
262+
$path = $realPath ?: $path;
263+
264+
if (strpos($path, $currentPath) !== 0) {
265+
$path = $currentPath;
266+
}
267+
268+
if ($this->mediaDirectoryWrite->isDirectory($path)
269+
&& strpos($path, $currentPath) === 0
270+
&& $path !== $currentPath
271+
) {
251272
$currentPath = $this->mediaDirectoryWrite->getRelativePath($path);
252273
}
253274
}

app/code/Magento/Theme/Model/Wysiwyg/Storage.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ class Storage
1818
{
1919
/**
2020
* Type font
21+
*
22+
* Represents the font type
2123
*/
2224
const TYPE_FONT = 'font';
2325

2426
/**
2527
* Type image
28+
*
29+
* Represents the image type
2630
*/
2731
const TYPE_IMAGE = 'image';
2832

@@ -328,7 +332,7 @@ public function deleteDirectory($path)
328332
$rootCmp = rtrim($this->_helper->getStorageRoot(), '/');
329333
$pathCmp = rtrim($path, '/');
330334

331-
if ($rootCmp == $pathCmp) {
335+
if ($rootCmp == $pathCmp || $rootCmp === $this->mediaWriteDirectory->getAbsolutePath($path)) {
332336
throw new \Magento\Framework\Exception\LocalizedException(
333337
__('We can\'t delete root directory %1 right now.', $path)
334338
);

app/code/Magento/Theme/Test/Unit/Model/Wysiwyg/StorageTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,30 @@ public function testDeleteRootDirectory()
562562
$this->_storageModel->deleteDirectory($directoryPath);
563563
}
564564

565+
/**
566+
* cover \Magento\Theme\Model\Wysiwyg\Storage::deleteDirectory
567+
* @expectedException \Magento\Framework\Exception\LocalizedException
568+
*/
569+
public function testDeleteRootDirectoryRelative()
570+
{
571+
$directoryPath = $this->_storageRoot;
572+
$fakePath = 'fake/relative/path';
573+
574+
$this->directoryWrite->method('getAbsolutePath')
575+
->with($fakePath)
576+
->willReturn($directoryPath);
577+
578+
$this->_helperStorage->expects(
579+
$this->atLeastOnce()
580+
)->method(
581+
'getStorageRoot'
582+
)->will(
583+
$this->returnValue($directoryPath)
584+
);
585+
586+
$this->_storageModel->deleteDirectory($fakePath);
587+
}
588+
565589
/**
566590
* @return array
567591
*/

app/code/Magento/Theme/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,9 @@
289289
<argument name="identifierName" xsi:type="string">theme_id</argument>
290290
</arguments>
291291
</type>
292+
<type name="Magento\Theme\Helper\Storage">
293+
<arguments>
294+
<argument name="filesystemDriver" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
295+
</arguments>
296+
</type>
292297
</config>

0 commit comments

Comments
 (0)