Skip to content

Commit a7001ce

Browse files
MC-32593: var/resource_config.json is regenerated each time an image is requested by get.php
1 parent ac3eb00 commit a7001ce

File tree

2 files changed

+81
-43
lines changed

2 files changed

+81
-43
lines changed

app/code/Magento/MediaStorage/App/Media.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@
2020
use Magento\Framework\AppInterface;
2121
use Magento\Framework\Filesystem;
2222
use Magento\Framework\Filesystem\Directory\WriteInterface;
23+
use Magento\Framework\Filesystem\Driver\File;
2324
use Magento\MediaStorage\Model\File\Storage\Config;
2425
use Magento\MediaStorage\Model\File\Storage\ConfigFactory;
2526
use Magento\MediaStorage\Model\File\Storage\Response;
2627
use Magento\MediaStorage\Model\File\Storage\Synchronization;
2728
use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
2829
use Magento\MediaStorage\Service\ImageResize;
2930

31+
3032
/**
31-
* Media Storage
33+
* The class resize original images
3234
*
3335
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3436
*/
@@ -70,7 +72,12 @@ class Media implements AppInterface
7072
/**
7173
* @var WriteInterface
7274
*/
73-
private $directory;
75+
private $directoryPub;
76+
77+
/**
78+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
79+
*/
80+
private $directoryMedia;
7481

7582
/**
7683
* @var ConfigFactory
@@ -109,6 +116,7 @@ class Media implements AppInterface
109116
* @param PlaceholderFactory $placeholderFactory
110117
* @param State $state
111118
* @param ImageResize $imageResize
119+
* @param File $file
112120
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
113121
*/
114122
public function __construct(
@@ -122,15 +130,17 @@ public function __construct(
122130
Filesystem $filesystem,
123131
PlaceholderFactory $placeholderFactory,
124132
State $state,
125-
ImageResize $imageResize
133+
ImageResize $imageResize,
134+
File $file
126135
) {
127136
$this->response = $response;
128137
$this->isAllowed = $isAllowed;
129-
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::PUB);
138+
$this->directoryPub = $filesystem->getDirectoryWrite(DirectoryList::PUB);
139+
$this->directoryMedia = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
130140
$mediaDirectory = trim($mediaDirectory);
131141
if (!empty($mediaDirectory)) {
132142
// phpcs:ignore Magento2.Functions.DiscouragedFunction
133-
$this->mediaDirectoryPath = str_replace('\\', '/', realpath($mediaDirectory));
143+
$this->mediaDirectoryPath = str_replace('\\', '/', $file->getRealPath($mediaDirectory));
134144
}
135145
$this->configCacheFile = $configCacheFile;
136146
$this->relativeFileName = $relativeFileName;
@@ -151,7 +161,7 @@ public function launch(): ResponseInterface
151161
{
152162
$this->appState->setAreaCode(Area::AREA_GLOBAL);
153163

154-
if ($this->mediaDirectoryPath !== $this->directory->getAbsolutePath()) {
164+
if ($this->checkMediaDirectoryChanged()) {
155165
// Path to media directory changed or absent - update the config
156166
/** @var Config $config */
157167
$config = $this->configFactory->create(['cacheFile' => $this->configCacheFile]);
@@ -166,11 +176,11 @@ public function launch(): ResponseInterface
166176

167177
try {
168178
/** @var Synchronization $sync */
169-
$sync = $this->syncFactory->create(['directory' => $this->directory]);
179+
$sync = $this->syncFactory->create(['directory' => $this->directoryPub]);
170180
$sync->synchronize($this->relativeFileName);
171181
$this->imageResize->resizeFromImageName($this->getOriginalImage($this->relativeFileName));
172-
if ($this->directory->isReadable($this->relativeFileName)) {
173-
$this->response->setFilePath($this->directory->getAbsolutePath($this->relativeFileName));
182+
if ($this->directoryPub->isReadable($this->relativeFileName)) {
183+
$this->response->setFilePath($this->directoryPub->getAbsolutePath($this->relativeFileName));
174184
} else {
175185
$this->setPlaceholderImage();
176186
}
@@ -182,7 +192,17 @@ public function launch(): ResponseInterface
182192
}
183193

184194
/**
185-
* Set Placeholder as a response
195+
* Check if media directory changed
196+
*
197+
* @return bool
198+
*/
199+
private function checkMediaDirectoryChanged(): bool
200+
{
201+
return rtrim($this->mediaDirectoryPath, '/') !== rtrim($this->directoryMedia->getAbsolutePath(), '/');
202+
}
203+
204+
/**
205+
* Set placeholder image into response
186206
*
187207
* @return void
188208
*/

app/code/Magento/MediaStorage/Test/Unit/App/MediaTest.php

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Filesystem;
1616
use Magento\Framework\Filesystem\Directory\Read;
1717
use Magento\Framework\Filesystem\Directory\WriteInterface;
18+
use Magento\Framework\Filesystem\DriverPool;
1819
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1920
use Magento\MediaStorage\App\Media;
2021
use Magento\MediaStorage\Model\File\Storage\Config;
@@ -74,7 +75,12 @@ class MediaTest extends TestCase
7475
/**
7576
* @var Read|MockObject
7677
*/
77-
private $directoryMock;
78+
private $directoryMediaMock;
79+
80+
/**
81+
* @var \Magento\Framework\Filesystem\Directory\Read|\PHPUnit_Framework_MockObject_MockObject
82+
*/
83+
private $directoryPubMock;
7884

7985
protected function setUp()
8086
{
@@ -96,12 +102,30 @@ protected function setUp()
96102
->will($this->returnValue($this->sync));
97103

98104
$this->filesystemMock = $this->createMock(Filesystem::class);
99-
$this->directoryMock = $this->getMockForAbstractClass(WriteInterface::class);
100-
105+
$this->directoryPubMock = $this->getMockForAbstractClass(
106+
WriteInterface::class,
107+
[],
108+
'',
109+
false,
110+
true,
111+
true,
112+
['isReadable', 'getAbsolutePath']
113+
);
114+
$this->directoryMediaMock = $this->getMockForAbstractClass(
115+
WriteInterface::class,
116+
[],
117+
'',
118+
false,
119+
true,
120+
true,
121+
['getAbsolutePath']
122+
);
101123
$this->filesystemMock->expects($this->any())
102124
->method('getDirectoryWrite')
103-
->with(DirectoryList::PUB)
104-
->will($this->returnValue($this->directoryMock));
125+
->willReturnMap([
126+
[DirectoryList::PUB, DriverPool::FILE, $this->directoryPubMock],
127+
[DirectoryList::MEDIA, DriverPool::FILE, $this->directoryMediaMock],
128+
]);
105129

106130
$this->responseMock = $this->createMock(Response::class);
107131
}
@@ -116,17 +140,17 @@ public function testProcessRequestCreatesConfigFileMediaDirectoryIsNotProvided()
116140
$this->mediaModel = $this->getMediaModel();
117141

118142
$filePath = '/absolute/path/to/test/file.png';
119-
$this->directoryMock->expects($this->any())
143+
$this->directoryMediaMock->expects($this->once())
144+
->method('getAbsolutePath')
145+
->with(null)
146+
->will($this->returnValue(self::MEDIA_DIRECTORY));
147+
$this->directoryPubMock->expects($this->once())
120148
->method('getAbsolutePath')
121-
->will($this->returnValueMap(
122-
[
123-
[null, self::MEDIA_DIRECTORY],
124-
[self::RELATIVE_FILE_PATH, $filePath],
125-
]
126-
));
149+
->with(self::RELATIVE_FILE_PATH)
150+
->will($this->returnValue($filePath));
127151
$this->configMock->expects($this->once())->method('save');
128152
$this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
129-
$this->directoryMock->expects($this->once())
153+
$this->directoryPubMock->expects($this->once())
130154
->method('isReadable')
131155
->with(self::RELATIVE_FILE_PATH)
132156
->will($this->returnValue(true));
@@ -140,18 +164,18 @@ public function testProcessRequestReturnsFileIfItsProperlySynchronized()
140164

141165
$filePath = '/absolute/path/to/test/file.png';
142166
$this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
143-
$this->directoryMock->expects($this->once())
167+
$this->directoryMediaMock->expects($this->once())
168+
->method('getAbsolutePath')
169+
->with(null)
170+
->will($this->returnValue(self::MEDIA_DIRECTORY));
171+
$this->directoryPubMock->expects($this->once())
144172
->method('isReadable')
145173
->with(self::RELATIVE_FILE_PATH)
146174
->will($this->returnValue(true));
147-
$this->directoryMock->expects($this->any())
175+
$this->directoryPubMock->expects($this->once())
148176
->method('getAbsolutePath')
149-
->will($this->returnValueMap(
150-
[
151-
[null, self::MEDIA_DIRECTORY],
152-
[self::RELATIVE_FILE_PATH, $filePath],
153-
]
154-
));
177+
->with(self::RELATIVE_FILE_PATH)
178+
->will($this->returnValue($filePath));
155179
$this->responseMock->expects($this->once())->method('setFilePath')->with($filePath);
156180
$this->assertSame($this->responseMock, $this->mediaModel->launch());
157181
}
@@ -161,11 +185,11 @@ public function testProcessRequestReturnsNotFoundIfFileIsNotSynchronized()
161185
$this->mediaModel = $this->getMediaModel();
162186

163187
$this->sync->expects($this->once())->method('synchronize')->with(self::RELATIVE_FILE_PATH);
164-
$this->directoryMock->expects($this->once())
188+
$this->directoryMediaMock->expects($this->once())
165189
->method('getAbsolutePath')
166-
->with()
190+
->with(null)
167191
->will($this->returnValue(self::MEDIA_DIRECTORY));
168-
$this->directoryMock->expects($this->once())
192+
$this->directoryPubMock->expects($this->once())
169193
->method('isReadable')
170194
->with(self::RELATIVE_FILE_PATH)
171195
->will($this->returnValue(false));
@@ -205,16 +229,10 @@ public function testCatchException($isDeveloper, $setBodyCalls)
205229
public function testExceptionWhenIsAllowedReturnsFalse()
206230
{
207231
$this->mediaModel = $this->getMediaModel(false);
208-
209-
$filePath = '/absolute/path/to/test/file.png';
210-
$this->directoryMock->expects($this->any())
232+
$this->directoryMediaMock->expects($this->once())
211233
->method('getAbsolutePath')
212-
->will($this->returnValueMap(
213-
[
214-
[null, self::MEDIA_DIRECTORY],
215-
[self::RELATIVE_FILE_PATH, $filePath],
216-
]
217-
));
234+
->with(null)
235+
->will($this->returnValue(self::MEDIA_DIRECTORY));
218236
$this->configMock->expects($this->once())->method('save');
219237

220238
$this->expectException(LogicException::class);

0 commit comments

Comments
 (0)