Skip to content

Commit 1a2ee46

Browse files
Merge MC-31227 into 2.3.6-bugfixes-08072020
2 parents 4cff39a + b79aee1 commit 1a2ee46

File tree

7 files changed

+242
-47
lines changed

7 files changed

+242
-47
lines changed

app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/DeleteFolder.php

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,76 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
8+
declare(strict_types=1);
9+
710
namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;
811

12+
use Magento\Backend\App\Action\Context;
13+
use Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;
914
use Magento\Framework\App\Action\HttpPostActionInterface;
10-
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\App\Filesystem\DirectoryResolver;
16+
use Magento\Framework\Controller\Result\Json;
17+
use Magento\Framework\Controller\Result\JsonFactory;
18+
use Magento\Framework\Controller\Result\RawFactory;
19+
use Magento\Framework\Controller\ResultInterface;
20+
use Magento\Framework\Registry;
1121

1222
/**
1323
* Delete image folder.
1424
*/
15-
class DeleteFolder extends \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images implements HttpPostActionInterface
25+
class DeleteFolder extends Images implements HttpPostActionInterface
1626
{
1727
/**
18-
* @var \Magento\Framework\Controller\Result\JsonFactory
28+
* @var JsonFactory
1929
*/
2030
protected $resultJsonFactory;
2131

2232
/**
23-
* @var \Magento\Framework\Controller\Result\RawFactory
33+
* @var RawFactory
2434
*/
2535
protected $resultRawFactory;
2636

2737
/**
28-
* @var \Magento\Framework\App\Filesystem\DirectoryResolver
38+
* @var DirectoryResolver
2939
*/
3040
private $directoryResolver;
3141

3242
/**
33-
* @param \Magento\Backend\App\Action\Context $context
34-
* @param \Magento\Framework\Registry $coreRegistry
35-
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
36-
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
37-
* @param \Magento\Framework\App\Filesystem\DirectoryResolver|null $directoryResolver
43+
* @param Context $context
44+
* @param Registry $coreRegistry
45+
* @param JsonFactory $resultJsonFactory
46+
* @param RawFactory $resultRawFactory
47+
* @param DirectoryResolver|null $directoryResolver
3848
*/
3949
public function __construct(
40-
\Magento\Backend\App\Action\Context $context,
41-
\Magento\Framework\Registry $coreRegistry,
42-
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
43-
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
44-
\Magento\Framework\App\Filesystem\DirectoryResolver $directoryResolver = null
50+
Context $context,
51+
Registry $coreRegistry,
52+
JsonFactory $resultJsonFactory,
53+
RawFactory $resultRawFactory,
54+
DirectoryResolver $directoryResolver = null
4555
) {
4656
parent::__construct($context, $coreRegistry);
4757
$this->resultRawFactory = $resultRawFactory;
4858
$this->resultJsonFactory = $resultJsonFactory;
49-
$this->directoryResolver = $directoryResolver
50-
?: $this->_objectManager->get(\Magento\Framework\App\Filesystem\DirectoryResolver::class);
59+
$this->directoryResolver = $directoryResolver ?? $this->_objectManager->get(DirectoryResolver::class);
5160
}
5261

5362
/**
5463
* Delete folder action.
5564
*
56-
* @return \Magento\Framework\Controller\ResultInterface
57-
* @throws \Magento\Framework\Exception\LocalizedException
65+
* @return ResultInterface
5866
*/
5967
public function execute()
6068
{
6169
try {
6270
$path = $this->getStorage()->getCmsWysiwygImages()->getCurrentPath();
63-
if (!$this->directoryResolver->validatePath($path, DirectoryList::MEDIA)) {
64-
throw new \Magento\Framework\Exception\LocalizedException(
65-
__('Directory %1 is not under storage root path.', $path)
66-
);
67-
}
6871
$this->getStorage()->deleteDirectory($path);
69-
72+
7073
return $this->resultRawFactory->create();
7174
} catch (\Exception $e) {
7275
$result = ['error' => true, 'message' => $e->getMessage()];
73-
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
76+
/** @var Json $resultJson */
7477
$resultJson = $this->resultJsonFactory->create();
7578

7679
return $resultJson->setData($result);

app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ public function __construct(
225225
*
226226
* @param string $path
227227
* @return void
228+
* @throws \Magento\Framework\Exception\FileSystemException
229+
* @throws \Magento\Framework\Exception\ValidatorException
228230
*/
229231
protected function createSubDirectories($path)
230232
{
@@ -295,6 +297,7 @@ protected function removeItemFromCollection($collection, $conditions)
295297
*
296298
* @param string $path Parent directory path
297299
* @return \Magento\Framework\Data\Collection\Filesystem
300+
* @throws \Exception
298301
*/
299302
public function getDirsCollection($path)
300303
{
@@ -393,6 +396,7 @@ public function getFilesCollection($path, $type = null)
393396
*
394397
* @param string $path Path to the directory
395398
* @return \Magento\Cms\Model\Wysiwyg\Images\Storage\Collection
399+
* @throws \Exception
396400
*/
397401
public function getCollection($path = null)
398402
{
@@ -485,6 +489,9 @@ public function deleteDirectory($path)
485489
*
486490
* @param string $path
487491
* @return void
492+
* @throws \Magento\Framework\Exception\FileSystemException
493+
* @throws \Magento\Framework\Exception\LocalizedException
494+
* @throws \Magento\Framework\Exception\ValidatorException
488495
*/
489496
protected function _deleteByPath($path)
490497
{
@@ -500,6 +507,8 @@ protected function _deleteByPath($path)
500507
*
501508
* @param string $target File path to be deleted
502509
* @return $this
510+
* @throws \Magento\Framework\Exception\FileSystemException
511+
* @throws \Magento\Framework\Exception\ValidatorException
503512
*/
504513
public function deleteFile($target)
505514
{
@@ -561,9 +570,11 @@ public function uploadFile($targetPath, $type = null)
561570
/**
562571
* Thumbnail path getter
563572
*
564-
* @param string $filePath original file path
565-
* @param bool $checkFile OPTIONAL is it necessary to check file availability
573+
* @param string $filePath original file path
574+
* @param bool $checkFile OPTIONAL is it necessary to check file availability
566575
* @return string|false
576+
* @throws \Magento\Framework\Exception\FileSystemException
577+
* @throws \Magento\Framework\Exception\ValidatorException
567578
*/
568579
public function getThumbnailPath($filePath, $checkFile = false)
569580
{
@@ -587,9 +598,11 @@ public function getThumbnailPath($filePath, $checkFile = false)
587598
/**
588599
* Thumbnail URL getter
589600
*
590-
* @param string $filePath original file path
591-
* @param bool $checkFile OPTIONAL is it necessary to check file availability
601+
* @param string $filePath original file path
602+
* @param bool $checkFile OPTIONAL is it necessary to check file availability
592603
* @return string|false
604+
* @throws \Magento\Framework\Exception\FileSystemException
605+
* @throws \Magento\Framework\Exception\ValidatorException
593606
*/
594607
public function getThumbnailUrl($filePath, $checkFile = false)
595608
{
@@ -610,6 +623,8 @@ public function getThumbnailUrl($filePath, $checkFile = false)
610623
* @param string $source Image path to be resized
611624
* @param bool $keepRatio Keep aspect ratio or not
612625
* @return bool|string Resized filepath or false if errors were occurred
626+
* @throws \Magento\Framework\Exception\FileSystemException
627+
* @throws \Magento\Framework\Exception\ValidatorException
613628
*/
614629
public function resizeFile($source, $keepRatio = true)
615630
{
@@ -643,6 +658,9 @@ public function resizeFile($source, $keepRatio = true)
643658
*
644659
* @param string $filename File basename
645660
* @return bool|string Thumbnail path or false for errors
661+
* @throws \Magento\Framework\Exception\FileSystemException
662+
* @throws \Magento\Framework\Exception\LocalizedException
663+
* @throws \Magento\Framework\Exception\ValidatorException
646664
*/
647665
public function resizeOnTheFly($filename)
648666
{
@@ -658,6 +676,8 @@ public function resizeOnTheFly($filename)
658676
*
659677
* @param bool|string $filePath Path to the file
660678
* @return string
679+
* @throws \Magento\Framework\Exception\FileSystemException
680+
* @throws \Magento\Framework\Exception\ValidatorException
661681
*/
662682
public function getThumbsPath($filePath = false)
663683
{
@@ -782,17 +802,28 @@ protected function _validatePath($path)
782802
*
783803
* @param string $path
784804
* @return string
805+
* @throws \Magento\Framework\Exception\ValidatorException
785806
*/
786807
protected function _sanitizePath($path)
787808
{
788-
return rtrim(preg_replace('~[/\\\]+~', '/', $this->_directory->getDriver()->getRealPathSafety($path)), '/');
809+
return rtrim(
810+
preg_replace(
811+
'~[/\\\]+~',
812+
'/',
813+
$this->_directory->getDriver()->getRealPathSafety(
814+
$this->_directory->getAbsolutePath($path)
815+
)
816+
),
817+
'/'
818+
);
789819
}
790820

791821
/**
792822
* Get path in root storage dir
793823
*
794824
* @param string $path
795825
* @return string|bool
826+
* @throws \Magento\Framework\Exception\ValidatorException
796827
*/
797828
protected function _getRelativePathToRoot($path)
798829
{

app/code/Magento/Cms/Test/Unit/Model/Wysiwyg/Images/StorageTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function setUp()
138138

139139
$this->directoryMock = $this->createPartialMock(
140140
\Magento\Framework\Filesystem\Directory\Write::class,
141-
['delete', 'getDriver', 'create', 'getRelativePath', 'isExist', 'isFile']
141+
['delete', 'getDriver', 'create', 'getRelativePath', 'getAbsolutePath', 'isExist', 'isFile']
142142
);
143143
$this->directoryMock->expects(
144144
$this->any()
@@ -282,6 +282,7 @@ public function testGetResizeHeight()
282282
public function testDeleteDirectoryOverRoot()
283283
{
284284
$this->driverMock->expects($this->atLeastOnce())->method('getRealPathSafety')->will($this->returnArgument(0));
285+
$this->directoryMock->expects($this->atLeastOnce())->method('getAbsolutePath')->will($this->returnArgument(0));
285286
$this->imagesStorage->deleteDirectory(self::INVALID_DIRECTORY_OVER_ROOT);
286287
}
287288

@@ -293,6 +294,7 @@ public function testDeleteDirectoryOverRoot()
293294
public function testDeleteRootDirectory()
294295
{
295296
$this->driverMock->expects($this->atLeastOnce())->method('getRealPathSafety')->will($this->returnArgument(0));
297+
$this->directoryMock->expects($this->atLeastOnce())->method('getAbsolutePath')->will($this->returnArgument(0));
296298
$this->imagesStorage->deleteDirectory(self::STORAGE_ROOT_DIR);
297299
}
298300

app/code/Magento/Theme/Model/Design/Backend/File.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\Theme\Model\Design\Backend;
810

9-
use Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface;
1011
use Magento\Config\Model\Config\Backend\File as BackendFile;
12+
use Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface;
1113
use Magento\Framework\App\Cache\TypeListInterface;
1214
use Magento\Framework\App\Config\ScopeConfigInterface;
1315
use Magento\Framework\App\ObjectManager;
1416
use Magento\Framework\Data\Collection\AbstractDb;
1517
use Magento\Framework\Exception\LocalizedException;
1618
use Magento\Framework\File\Mime;
1719
use Magento\Framework\Filesystem;
20+
use Magento\Framework\Filesystem\Directory\ReadFactory;
21+
use Magento\Framework\Filesystem\Io\File as IoFile;
1822
use Magento\Framework\Model\Context;
1923
use Magento\Framework\Model\ResourceModel\AbstractResource;
2024
use Magento\Framework\Registry;
2125
use Magento\Framework\UrlInterface;
26+
use Magento\MediaStorage\Helper\File\Storage\Database;
2227
use Magento\MediaStorage\Model\File\UploaderFactory;
2328
use Magento\Theme\Model\Design\Config\FileUploader\FileProcessor;
24-
use Magento\MediaStorage\Helper\File\Storage\Database;
25-
use Magento\Framework\Filesystem\Io\File as IoFile;
26-
use Magento\Framework\Filesystem\Directory\ReadFactory;
2729

2830
/**
2931
* File Backend
@@ -108,7 +110,7 @@ public function __construct(
108110
/** @var ReadFactory $readFactory */
109111
$readFactory = ObjectManager::getInstance()->get(ReadFactory::class);
110112
$this->tmpDirectory = $tmpDirectory ?: $readFactory->create(
111-
$this->_mediaDirectory->getAbsolutePath() .'tmp/' . FileProcessor::FILE_DIR
113+
$this->_mediaDirectory->getAbsolutePath() . 'tmp/' . FileProcessor::FILE_DIR
112114
);
113115
}
114116

@@ -227,7 +229,7 @@ protected function getStoreMediaUrl($fileName)
227229
$urlType = ['_type' => empty($baseUrl['type']) ? 'link' : (string)$baseUrl['type']];
228230
$baseUrl = $baseUrl['value'] . '/';
229231
}
230-
return $this->urlBuilder->getBaseUrl($urlType) . $baseUrl . $fileName;
232+
return $this->urlBuilder->getBaseUrl($urlType) . $baseUrl . $fileName;
231233
}
232234

233235
/**

0 commit comments

Comments
 (0)