Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create missing directories in imageuploader tree if they don't alread… #24878

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions app/code/Magento/Cms/Helper/Wysiwyg/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ public function getImageHtmlDeclaration($filename, $renderAsTag = false)
}

/**
* Return path of the current selected directory or root directory for startup
* Try to create target directory if it doesn't exist
* Return path of the root directory for startup. Also try to create target directory if it doesn't exist
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
Expand All @@ -241,18 +240,34 @@ public function getCurrentPath()
$currentPath = $path;
}
}

$currentTreePath = $this->_getRequest()->getParam('current_tree_path');
if ($currentTreePath) {
$currentTreePath = $this->convertIdToPath($currentTreePath);
$this->createSubDirIfNotExist($currentTreePath);
}

$this->_currentPath = $currentPath;
}

return $this->_currentPath;
}

private function createSubDirIfNotExist(string $absPath)
{
$relPath = $this->_directory->getRelativePath($absPath);
if (!$this->_directory->isExist($relPath)) {
try {
$currentDir = $this->_directory->getRelativePath($currentPath);
if (!$this->_directory->isExist($currentDir)) {
$this->_directory->create($currentDir);
}
$this->_directory->create($relPath);
} catch (\Magento\Framework\Exception\FileSystemException $e) {
$message = __('The directory %1 is not writable by server.', $currentPath);
$message = __(
'Can\'t create %1 as subdirectory of %2, you might have some permission issue.',
$relPath,
$this->_directory->getAbsolutePath()
);
throw new \Magento\Framework\Exception\LocalizedException($message);
}
$this->_currentPath = $currentPath;
}
return $this->_currentPath;
}

/**
Expand Down Expand Up @@ -294,6 +309,8 @@ public function idEncode($string)
public function idDecode($string)
{
$string = strtr($string, ':_-', '+/=');

// phpcs:ignore Magento2.Functions.DiscouragedFunction
return base64_decode($string);
}

Expand All @@ -315,7 +332,7 @@ public function getShortFilename($filename, $maxLength = 20)
/**
* Set user-traversable image directory subpath relative to media directory and relative to nested storage root
*
* @var string $subpath
* @param string $subpath
* @return void
*/
public function setImageDirectorySubpath($subpath)
Expand Down
41 changes: 27 additions & 14 deletions app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,14 @@ public function providerIsUsingStaticUrlsAllowed()
* @param bool $isExist
* @dataProvider providerGetCurrentPath
*/
public function testGetCurrentPath($pathId, $expectedPath, $isExist)
public function testGetCurrentPath($pathId, $subDir, $expectedPath, $isExist)
{
$this->requestMock->expects($this->any())
->method('getParam')
->willReturnMap(
[
['node', null, $pathId],
['current_tree_path', null, $subDir],
]
);

Expand All @@ -367,21 +368,33 @@ public function testGetCurrentPath($pathId, $expectedPath, $isExist)
['PATH', '.'],
]
);
$this->directoryWriteMock->expects($this->once())
->method('isExist')
->willReturn($isExist);
$this->directoryWriteMock->expects($this->any())
->method('create')
->with($this->directoryWriteMock->getRelativePath($expectedPath));

if ($subDir) {
$this->directoryWriteMock->expects($this->once())
->method('isExist')
->willReturn($isExist);
$this->directoryWriteMock->expects($this->any())
->method('create')
->with($this->directoryWriteMock->getRelativePath($expectedPath));
}

$this->assertEquals($expectedPath, $this->imagesHelper->getCurrentPath());
}

public function testGetCurrentPathThrowException()
{
$this->requestMock->expects($this->any())
->method('getParam')
->willReturn('PATH');

$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
$this->expectExceptionMessage('The directory PATH is not writable by server.');
$this->expectExceptionMessage(
'Can\'t create SUBDIR as subdirectory of PATH, you might have some permission issue.'
);

$this->directoryWriteMock->expects($this->any())
->method('getRelativePath')
->willReturn('SUBDIR');
$this->directoryWriteMock->expects($this->once())
->method('isExist')
->willReturn(false);
Expand All @@ -402,12 +415,12 @@ public function testGetCurrentPathThrowException()
public function providerGetCurrentPath()
{
return [
['L3Rlc3RfcGF0aA--', 'PATH/test_path', true],
['L215LmpwZw--', 'PATH', true],
[null, 'PATH', true],
['L3Rlc3RfcGF0aA--', 'PATH/test_path', false],
['L215LmpwZw--', 'PATH', false],
[null, 'PATH', false],
['L3Rlc3RfcGF0aA--', 'L3Rlc3RfcGF0aA--', 'PATH/test_path', true],
['L215LmpwZw--', '', 'PATH', true],
[null, '', 'PATH', true],
['L3Rlc3RfcGF0aA--', 'L3Rlc3RfcGF0aA--', 'PATH/test_path', false],
['L215LmpwZw--', '', 'PATH', false],
[null, '', 'PATH', false],
];
}

Expand Down