Skip to content

Commit dcf8f8f

Browse files
ENGCOM-6497: Refactor: Add information about the path that is not allowed #26009
- Merge Pull Request #26009 from lbajsarowicz/magento2:bugfix/add-path-details - Merged commits: 1. 97b1286
2 parents 9400c1e + 97b1286 commit dcf8f8f

File tree

2 files changed

+146
-87
lines changed

2 files changed

+146
-87
lines changed

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

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,39 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
89
namespace Magento\MediaStorage\App;
910

11+
use Closure;
12+
use Exception;
13+
use LogicException;
1014
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
15+
use Magento\Framework\App;
16+
use Magento\Framework\App\Area;
17+
use Magento\Framework\App\Filesystem\DirectoryList;
18+
use Magento\Framework\App\ResponseInterface;
1119
use Magento\Framework\App\State;
20+
use Magento\Framework\AppInterface;
1221
use Magento\Framework\Filesystem;
22+
use Magento\Framework\Filesystem\Directory\WriteInterface;
23+
use Magento\MediaStorage\Model\File\Storage\Config;
1324
use Magento\MediaStorage\Model\File\Storage\ConfigFactory;
1425
use Magento\MediaStorage\Model\File\Storage\Response;
15-
use Magento\Framework\App;
16-
use Magento\Framework\App\Filesystem\DirectoryList;
17-
use Magento\Framework\AppInterface;
26+
use Magento\MediaStorage\Model\File\Storage\Synchronization;
1827
use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
19-
use Magento\Framework\App\Area;
20-
use Magento\MediaStorage\Model\File\Storage\Config;
2128
use Magento\MediaStorage\Service\ImageResize;
2229

2330
/**
31+
* Media Storage
32+
*
2433
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2534
*/
2635
class Media implements AppInterface
2736
{
2837
/**
2938
* Authorization function
3039
*
31-
* @var \Closure
40+
* @var Closure
3241
*/
3342
private $isAllowed;
3443

@@ -59,7 +68,7 @@ class Media implements AppInterface
5968
private $response;
6069

6170
/**
62-
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
71+
* @var WriteInterface
6372
*/
6473
private $directory;
6574

@@ -92,7 +101,7 @@ class Media implements AppInterface
92101
* @param ConfigFactory $configFactory
93102
* @param SynchronizationFactory $syncFactory
94103
* @param Response $response
95-
* @param \Closure $isAllowed
104+
* @param Closure $isAllowed
96105
* @param string $mediaDirectory
97106
* @param string $configCacheFile
98107
* @param string $relativeFileName
@@ -106,7 +115,7 @@ public function __construct(
106115
ConfigFactory $configFactory,
107116
SynchronizationFactory $syncFactory,
108117
Response $response,
109-
\Closure $isAllowed,
118+
Closure $isAllowed,
110119
$mediaDirectory,
111120
$configCacheFile,
112121
$relativeFileName,
@@ -120,6 +129,7 @@ public function __construct(
120129
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::PUB);
121130
$mediaDirectory = trim($mediaDirectory);
122131
if (!empty($mediaDirectory)) {
132+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
123133
$this->mediaDirectoryPath = str_replace('\\', '/', realpath($mediaDirectory));
124134
}
125135
$this->configCacheFile = $configCacheFile;
@@ -135,9 +145,9 @@ public function __construct(
135145
* Run application
136146
*
137147
* @return Response
138-
* @throws \LogicException
148+
* @throws LogicException
139149
*/
140-
public function launch()
150+
public function launch(): ResponseInterface
141151
{
142152
$this->appState->setAreaCode(Area::AREA_GLOBAL);
143153

@@ -150,12 +160,12 @@ public function launch()
150160
$allowedResources = $config->getAllowedResources();
151161
$isAllowed = $this->isAllowed;
152162
if (!$isAllowed($this->relativeFileName, $allowedResources)) {
153-
throw new \LogicException('The specified path is not allowed.');
163+
throw new LogicException('The path is not allowed: ' . $this->relativeFileName);
154164
}
155165
}
156166

157167
try {
158-
/** @var \Magento\MediaStorage\Model\File\Storage\Synchronization $sync */
168+
/** @var Synchronization $sync */
159169
$sync = $this->syncFactory->create(['directory' => $this->directory]);
160170
$sync->synchronize($this->relativeFileName);
161171
$this->imageResize->resizeFromImageName($this->getOriginalImage($this->relativeFileName));
@@ -164,14 +174,19 @@ public function launch()
164174
} else {
165175
$this->setPlaceholderImage();
166176
}
167-
} catch (\Exception $e) {
177+
} catch (Exception $e) {
168178
$this->setPlaceholderImage();
169179
}
170180

171181
return $this->response;
172182
}
173183

174-
private function setPlaceholderImage()
184+
/**
185+
* Set Placeholder as a response
186+
*
187+
* @return void
188+
*/
189+
private function setPlaceholderImage(): void
175190
{
176191
$placeholder = $this->placeholderFactory->create(['type' => 'image']);
177192
$this->response->setFilePath($placeholder->getPath());
@@ -189,9 +204,9 @@ private function getOriginalImage(string $resizedImagePath): string
189204
}
190205

191206
/**
192-
* {@inheritdoc}
207+
* @inheritdoc
193208
*/
194-
public function catchException(App\Bootstrap $bootstrap, \Exception $exception)
209+
public function catchException(App\Bootstrap $bootstrap, Exception $exception)
195210
{
196211
$this->response->setHttpResponseCode(404);
197212
if ($bootstrap->isDeveloperMode()) {

0 commit comments

Comments
 (0)