10 use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
13 use Magento\MediaStorage\Model\File\Storage\ConfigFactory;
18 use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
40 private $mediaDirectoryPath;
47 private $configCacheFile;
54 private $relativeFileName;
69 private $configFactory;
79 private $placeholderFactory;
106 ConfigFactory $configFactory,
107 SynchronizationFactory $syncFactory,
114 PlaceholderFactory $placeholderFactory,
123 $this->mediaDirectoryPath = str_replace(
'\\',
'/', realpath(
$mediaDirectory));
125 $this->configCacheFile = $configCacheFile;
126 $this->relativeFileName = $relativeFileName;
128 $this->syncFactory = $syncFactory;
129 $this->placeholderFactory = $placeholderFactory;
130 $this->appState = $state;
131 $this->imageResize = $imageResize;
144 if ($this->mediaDirectoryPath !== $this->directory->getAbsolutePath()) {
147 $config = $this->configFactory->create([
'cacheFile' => $this->configCacheFile]);
149 $this->mediaDirectoryPath =
$config->getMediaDirectory();
151 $isAllowed = $this->isAllowed;
153 throw new \LogicException(
'The specified path is not allowed.');
159 $sync = $this->syncFactory->create([
'directory' => $this->directory]);
160 $sync->synchronize($this->relativeFileName);
161 $this->imageResize->resizeFromImageName($this->getOriginalImage($this->relativeFileName));
162 if ($this->directory->isReadable($this->relativeFileName)) {
163 $this->response->setFilePath($this->directory->getAbsolutePath($this->relativeFileName));
165 $this->setPlaceholderImage();
167 }
catch (\Exception $e) {
168 $this->setPlaceholderImage();
171 return $this->response;
174 private function setPlaceholderImage()
176 $placeholder = $this->placeholderFactory->create([
'type' =>
'image']);
177 $this->response->setFilePath($placeholder->getPath());
186 private function getOriginalImage(
string $resizedImagePath): string
188 return preg_replace(
'|^.*((?:/[^/]+){3})$|',
'$1', $resizedImagePath);
196 $this->response->setHttpResponseCode(404);
198 $this->response->setHeader(
'Content-Type',
'text/plain');
199 $this->response->setBody($exception->getMessage() .
"\n" . $exception->getTraceAsString());
201 $this->response->sendResponse();
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap