Skip to content

Commit 99a9405

Browse files
committed
Create missing directories in imageuploader tree if they don't already exist.
1 parent 6df825a commit 99a9405

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

app/code/Magento/Cms/Helper/Wysiwyg/Images.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ public function getImageHtmlDeclaration($filename, $renderAsTag = false)
224224
}
225225

226226
/**
227-
* Return path of the current selected directory or root directory for startup
228-
* Try to create target directory if it doesn't exist
227+
* Return path of the root directory for startup. Also try to create target directory if it doesn't exist
229228
*
230229
* @return string
231230
* @throws \Magento\Framework\Exception\LocalizedException
@@ -241,18 +240,34 @@ public function getCurrentPath()
241240
$currentPath = $path;
242241
}
243242
}
243+
244+
$currentTreePath = $this->_getRequest()->getParam('current_tree_path');
245+
if ($currentTreePath) {
246+
$currentTreePath = $this->convertIdToPath($currentTreePath);
247+
$this->createSubDirIfNotExist($currentTreePath);
248+
}
249+
250+
$this->_currentPath = $currentPath;
251+
}
252+
253+
return $this->_currentPath;
254+
}
255+
256+
private function createSubDirIfNotExist(string $absPath)
257+
{
258+
$relPath = $this->_directory->getRelativePath($absPath);
259+
if (!$this->_directory->isExist($relPath)) {
244260
try {
245-
$currentDir = $this->_directory->getRelativePath($currentPath);
246-
if (!$this->_directory->isExist($currentDir)) {
247-
$this->_directory->create($currentDir);
248-
}
261+
$this->_directory->create($relPath);
249262
} catch (\Magento\Framework\Exception\FileSystemException $e) {
250-
$message = __('The directory %1 is not writable by server.', $currentPath);
263+
$message = __(
264+
'Can\'t create %1 as subdirectory of %2, you might have some permission issue.',
265+
$relPath,
266+
$this->_directory->getAbsolutePath()
267+
);
251268
throw new \Magento\Framework\Exception\LocalizedException($message);
252269
}
253-
$this->_currentPath = $currentPath;
254270
}
255-
return $this->_currentPath;
256271
}
257272

258273
/**
@@ -294,6 +309,8 @@ public function idEncode($string)
294309
public function idDecode($string)
295310
{
296311
$string = strtr($string, ':_-', '+/=');
312+
313+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
297314
return base64_decode($string);
298315
}
299316

@@ -315,7 +332,7 @@ public function getShortFilename($filename, $maxLength = 20)
315332
/**
316333
* Set user-traversable image directory subpath relative to media directory and relative to nested storage root
317334
*
318-
* @var string $subpath
335+
* @param string $subpath
319336
* @return void
320337
*/
321338
public function setImageDirectorySubpath($subpath)

app/code/Magento/Cms/Test/Unit/Helper/Wysiwyg/ImagesTest.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,14 @@ public function providerIsUsingStaticUrlsAllowed()
339339
* @param bool $isExist
340340
* @dataProvider providerGetCurrentPath
341341
*/
342-
public function testGetCurrentPath($pathId, $expectedPath, $isExist)
342+
public function testGetCurrentPath($pathId, $subDir, $expectedPath, $isExist)
343343
{
344344
$this->requestMock->expects($this->any())
345345
->method('getParam')
346346
->willReturnMap(
347347
[
348348
['node', null, $pathId],
349+
['current_tree_path', null, $subDir],
349350
]
350351
);
351352

@@ -367,21 +368,33 @@ public function testGetCurrentPath($pathId, $expectedPath, $isExist)
367368
['PATH', '.'],
368369
]
369370
);
370-
$this->directoryWriteMock->expects($this->once())
371-
->method('isExist')
372-
->willReturn($isExist);
373-
$this->directoryWriteMock->expects($this->any())
374-
->method('create')
375-
->with($this->directoryWriteMock->getRelativePath($expectedPath));
371+
372+
if ($subDir) {
373+
$this->directoryWriteMock->expects($this->once())
374+
->method('isExist')
375+
->willReturn($isExist);
376+
$this->directoryWriteMock->expects($this->any())
377+
->method('create')
378+
->with($this->directoryWriteMock->getRelativePath($expectedPath));
379+
}
376380

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

380384
public function testGetCurrentPathThrowException()
381385
{
386+
$this->requestMock->expects($this->any())
387+
->method('getParam')
388+
->willReturn('PATH');
389+
382390
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
383-
$this->expectExceptionMessage('The directory PATH is not writable by server.');
391+
$this->expectExceptionMessage(
392+
'Can\'t create SUBDIR as subdirectory of PATH, you might have some permission issue.'
393+
);
384394

395+
$this->directoryWriteMock->expects($this->any())
396+
->method('getRelativePath')
397+
->willReturn('SUBDIR');
385398
$this->directoryWriteMock->expects($this->once())
386399
->method('isExist')
387400
->willReturn(false);
@@ -402,12 +415,12 @@ public function testGetCurrentPathThrowException()
402415
public function providerGetCurrentPath()
403416
{
404417
return [
405-
['L3Rlc3RfcGF0aA--', 'PATH/test_path', true],
406-
['L215LmpwZw--', 'PATH', true],
407-
[null, 'PATH', true],
408-
['L3Rlc3RfcGF0aA--', 'PATH/test_path', false],
409-
['L215LmpwZw--', 'PATH', false],
410-
[null, 'PATH', false],
418+
['L3Rlc3RfcGF0aA--', 'L3Rlc3RfcGF0aA--', 'PATH/test_path', true],
419+
['L215LmpwZw--', '', 'PATH', true],
420+
[null, '', 'PATH', true],
421+
['L3Rlc3RfcGF0aA--', 'L3Rlc3RfcGF0aA--', 'PATH/test_path', false],
422+
['L215LmpwZw--', '', 'PATH', false],
423+
[null, '', 'PATH', false],
411424
];
412425
}
413426

0 commit comments

Comments
 (0)