Skip to content

Commit a266dea

Browse files
authored
Merge pull request #4361 from magento-obsessive-owls/MC-15972-squashed
2 parents 1321b15 + eeda4f4 commit a266dea

File tree

5 files changed

+173
-41
lines changed

5 files changed

+173
-41
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/Processor.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ public function addImage(
167167
}
168168

169169
$fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($pathinfo['basename']);
170-
$dispretionPath = \Magento\MediaStorage\Model\File\Uploader::getDispersionPath($fileName);
171-
$fileName = $dispretionPath . '/' . $fileName;
170+
$dispersionPath = \Magento\MediaStorage\Model\File\Uploader::getDispersionPath($fileName);
171+
$fileName = $dispersionPath . '/' . $fileName;
172172

173-
$fileName = $this->getNotDuplicatedFilename($fileName, $dispretionPath);
173+
$fileName = $this->getNotDuplicatedFilename($fileName, $dispersionPath);
174174

175175
$destinationFile = $this->mediaConfig->getTmpMediaPath($fileName);
176176

@@ -465,27 +465,27 @@ protected function getUniqueFileName($file, $forTmp = false)
465465
* Get filename which is not duplicated with other files in media temporary and media directories
466466
*
467467
* @param string $fileName
468-
* @param string $dispretionPath
468+
* @param string $dispersionPath
469469
* @return string
470470
* @since 101.0.0
471471
*/
472-
protected function getNotDuplicatedFilename($fileName, $dispretionPath)
472+
protected function getNotDuplicatedFilename($fileName, $dispersionPath)
473473
{
474-
$fileMediaName = $dispretionPath . '/'
474+
$fileMediaName = $dispersionPath . '/'
475475
. \Magento\MediaStorage\Model\File\Uploader::getNewFileName($this->mediaConfig->getMediaPath($fileName));
476-
$fileTmpMediaName = $dispretionPath . '/'
476+
$fileTmpMediaName = $dispersionPath . '/'
477477
. \Magento\MediaStorage\Model\File\Uploader::getNewFileName($this->mediaConfig->getTmpMediaPath($fileName));
478478

479479
if ($fileMediaName != $fileTmpMediaName) {
480480
if ($fileMediaName != $fileName) {
481481
return $this->getNotDuplicatedFilename(
482482
$fileMediaName,
483-
$dispretionPath
483+
$dispersionPath
484484
);
485485
} elseif ($fileTmpMediaName != $fileName) {
486486
return $this->getNotDuplicatedFilename(
487487
$fileTmpMediaName,
488-
$dispretionPath
488+
$dispersionPath
489489
);
490490
}
491491
}

app/code/Magento/MediaStorage/Model/ResourceModel/File/Storage/File.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66
namespace Magento\MediaStorage\Model\ResourceModel\File\Storage;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\Filesystem\Io\File as FileIo;
11+
use Magento\Framework\App\ObjectManager;
912

1013
/**
1114
* Class File
1215
*/
1316
class File
1417
{
18+
/**
19+
* @var FileIo
20+
*/
21+
private $fileIo;
22+
1523
/**
1624
* @var \Magento\Framework\Filesystem
1725
*/
@@ -25,11 +33,16 @@ class File
2533
/**
2634
* @param \Magento\Framework\Filesystem $filesystem
2735
* @param \Psr\Log\LoggerInterface $log
36+
* @param FileIo $fileIo
2837
*/
29-
public function __construct(\Magento\Framework\Filesystem $filesystem, \Psr\Log\LoggerInterface $log)
30-
{
38+
public function __construct(
39+
\Magento\Framework\Filesystem $filesystem,
40+
\Psr\Log\LoggerInterface $log,
41+
FileIo $fileIo = null
42+
) {
3143
$this->_logger = $log;
3244
$this->_filesystem = $filesystem;
45+
$this->fileIo = $fileIo ?? ObjectManager::getInstance()->get(FileIo::class);
3346
}
3447

3548
/**
@@ -45,14 +58,15 @@ public function getStorageData($dir = '/')
4558
$directoryInstance = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
4659
if ($directoryInstance->isDirectory($dir)) {
4760
foreach ($directoryInstance->readRecursively($dir) as $path) {
48-
$itemName = basename($path);
61+
$pathInfo = $this->fileIo->getPathInfo($path);
62+
$itemName = $pathInfo['basename'];
4963
if ($itemName == '.svn' || $itemName == '.htaccess') {
5064
continue;
5165
}
5266
if ($directoryInstance->isDirectory($path)) {
5367
$directories[] = [
5468
'name' => $itemName,
55-
'path' => dirname($path) == '.' ? '/' : dirname($path),
69+
'path' => $pathInfo['dirname'] === '.' ? '/' : $pathInfo['dirname'],
5670
];
5771
} else {
5872
$files[] = $path;
@@ -64,7 +78,7 @@ public function getStorageData($dir = '/')
6478
}
6579

6680
/**
67-
* Clear files and directories in storage
81+
* Clear all files in storage $dir
6882
*
6983
* @param string $dir
7084
* @return $this
@@ -73,8 +87,17 @@ public function clear($dir = '')
7387
{
7488
$directoryInstance = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA);
7589
if ($directoryInstance->isDirectory($dir)) {
76-
foreach ($directoryInstance->read($dir) as $path) {
77-
$directoryInstance->delete($path);
90+
$paths = $directoryInstance->readRecursively($dir);
91+
foreach ($paths as $path) {
92+
if ($directoryInstance->isDirectory($path)) {
93+
continue;
94+
}
95+
96+
$pathInfo = $this->fileIo->getPathInfo($path);
97+
98+
if ($pathInfo['basename'] !== '.htaccess') {
99+
$directoryInstance->delete($path);
100+
}
78101
}
79102
}
80103

@@ -127,7 +150,7 @@ public function saveFile($filePath, $content, $overwrite = false)
127150
}
128151
} catch (\Magento\Framework\Exception\FileSystemException $e) {
129152
$this->_logger->info($e->getMessage());
130-
throw new \Magento\Framework\Exception\LocalizedException(__('Unable to save file: %1', $filePath));
153+
throw new LocalizedException(__('Unable to save file: %1', $filePath));
131154
}
132155

133156
return false;

app/code/Magento/MediaStorage/Test/Unit/Model/ResourceModel/File/Storage/FileTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
namespace Magento\MediaStorage\Test\Unit\Model\ResourceModel\File\Storage;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
910

1011
/**
1112
* Class FileTest
1213
*/
1314
class FileTest extends \PHPUnit\Framework\TestCase
1415
{
16+
/**
17+
* @var \Magento\Framework\Filesystem\Io\File
18+
*/
19+
private $fileIoMock;
20+
1521
/**
1622
* @var \Magento\MediaStorage\Model\ResourceModel\File\Storage\File
1723
*/
@@ -44,9 +50,17 @@ protected function setUp()
4450
['isDirectory', 'readRecursively']
4551
);
4652

47-
$this->storageFile = new \Magento\MediaStorage\Model\ResourceModel\File\Storage\File(
48-
$this->filesystemMock,
49-
$this->loggerMock
53+
$this->fileIoMock = $this->createPartialMock(\Magento\Framework\Filesystem\Io\File::class, ['getPathInfo']);
54+
55+
$objectManager = new ObjectManager($this);
56+
57+
$this->storageFile = $objectManager->getObject(
58+
\Magento\MediaStorage\Model\ResourceModel\File\Storage\File::class,
59+
[
60+
'filesystem' => $this->filesystemMock,
61+
'log' => $this->loggerMock,
62+
'fileIo' => $this->fileIoMock
63+
]
5064
);
5165
}
5266

@@ -98,6 +112,20 @@ public function testGetStorageData()
98112
'folder_one/folder_two/.htaccess',
99113
'folder_one/folder_two/file_two.txt',
100114
];
115+
116+
$pathInfos = array_map(
117+
function ($path) {
118+
return [$path, pathinfo($path)];
119+
},
120+
$paths
121+
);
122+
123+
$this->fileIoMock->expects(
124+
$this->any()
125+
)->method(
126+
'getPathInfo'
127+
)->will($this->returnValueMap($pathInfos));
128+
101129
sort($paths);
102130
$this->directoryReadMock->expects(
103131
$this->once()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\File\Test\Unit;
9+
10+
/**
11+
* Unit Test class for \Magento\Framework\File\Uploader
12+
*/
13+
class UploaderTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @param string $fileName
17+
* @param string|bool $expectedCorrectedFileName
18+
*
19+
* @dataProvider getCorrectFileNameProvider
20+
*/
21+
public function testGetCorrectFileName($fileName, $expectedCorrectedFileName)
22+
{
23+
$isExceptionExpected = $expectedCorrectedFileName === true;
24+
25+
if ($isExceptionExpected) {
26+
$this->expectException(\InvalidArgumentException::class);
27+
}
28+
29+
$this->assertEquals(
30+
$expectedCorrectedFileName,
31+
\Magento\Framework\File\Uploader::getCorrectFileName($fileName)
32+
);
33+
}
34+
35+
/**
36+
* @return array
37+
*/
38+
public function getCorrectFileNameProvider()
39+
{
40+
return [
41+
[
42+
'^&*&^&*^$$$$()',
43+
'file.'
44+
],
45+
[
46+
'^&*&^&*^$$$$().png',
47+
'file.png'
48+
],
49+
[
50+
'_',
51+
'file.'
52+
],
53+
[
54+
'_.jpg',
55+
'file.jpg'
56+
],
57+
[
58+
'a.' . str_repeat('b', 88),
59+
'a.' . str_repeat('b', 88)
60+
],
61+
[
62+
'a.' . str_repeat('b', 89),
63+
true
64+
]
65+
];
66+
}
67+
}

0 commit comments

Comments
 (0)