Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Media.php
Go to the documentation of this file.
1 <?php
9 
10 use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
13 use Magento\MediaStorage\Model\File\Storage\ConfigFactory;
18 use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory;
22 
26 class Media implements AppInterface
27 {
33  private $isAllowed;
34 
40  private $mediaDirectoryPath;
41 
47  private $configCacheFile;
48 
54  private $relativeFileName;
55 
59  private $response;
60 
64  private $directory;
65 
69  private $configFactory;
70 
74  private $syncFactory;
75 
79  private $placeholderFactory;
80 
84  private $appState;
85 
89  private $imageResize;
90 
105  public function __construct(
106  ConfigFactory $configFactory,
107  SynchronizationFactory $syncFactory,
108  Response $response,
109  \Closure $isAllowed,
111  $configCacheFile,
112  $relativeFileName,
114  PlaceholderFactory $placeholderFactory,
115  State $state,
116  ImageResize $imageResize
117  ) {
118  $this->response = $response;
119  $this->isAllowed = $isAllowed;
120  $this->directory = $filesystem->getDirectoryWrite(DirectoryList::PUB);
122  if (!empty($mediaDirectory)) {
123  $this->mediaDirectoryPath = str_replace('\\', '/', realpath($mediaDirectory));
124  }
125  $this->configCacheFile = $configCacheFile;
126  $this->relativeFileName = $relativeFileName;
127  $this->configFactory = $configFactory;
128  $this->syncFactory = $syncFactory;
129  $this->placeholderFactory = $placeholderFactory;
130  $this->appState = $state;
131  $this->imageResize = $imageResize;
132  }
133 
140  public function launch()
141  {
142  $this->appState->setAreaCode(Area::AREA_GLOBAL);
143 
144  if ($this->mediaDirectoryPath !== $this->directory->getAbsolutePath()) {
145  // Path to media directory changed or absent - update the config
147  $config = $this->configFactory->create(['cacheFile' => $this->configCacheFile]);
148  $config->save();
149  $this->mediaDirectoryPath = $config->getMediaDirectory();
150  $allowedResources = $config->getAllowedResources();
151  $isAllowed = $this->isAllowed;
152  if (!$isAllowed($this->relativeFileName, $allowedResources)) {
153  throw new \LogicException('The specified path is not allowed.');
154  }
155  }
156 
157  try {
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));
164  } else {
165  $this->setPlaceholderImage();
166  }
167  } catch (\Exception $e) {
168  $this->setPlaceholderImage();
169  }
170 
171  return $this->response;
172  }
173 
174  private function setPlaceholderImage()
175  {
176  $placeholder = $this->placeholderFactory->create(['type' => 'image']);
177  $this->response->setFilePath($placeholder->getPath());
178  }
179 
186  private function getOriginalImage(string $resizedImagePath): string
187  {
188  return preg_replace('|^.*((?:/[^/]+){3})$|', '$1', $resizedImagePath);
189  }
190 
194  public function catchException(App\Bootstrap $bootstrap, \Exception $exception)
195  {
196  $this->response->setHttpResponseCode(404);
197  if ($bootstrap->isDeveloperMode()) {
198  $this->response->setHeader('Content-Type', 'text/plain');
199  $this->response->setBody($exception->getMessage() . "\n" . $exception->getTraceAsString());
200  }
201  $this->response->sendResponse();
202  return true;
203  }
204 }
$response
Definition: 404.php:11
$config
Definition: fraud_order.php:17
__construct(ConfigFactory $configFactory, SynchronizationFactory $syncFactory, Response $response, \Closure $isAllowed, $mediaDirectory, $configCacheFile, $relativeFileName, Filesystem $filesystem, PlaceholderFactory $placeholderFactory, State $state, ImageResize $imageResize)
Definition: Media.php:105
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73
$isAllowed
Definition: get.php:20
$allowedResources
Definition: get.php:17
$configFactory
Definition: config_data.php:43
$mediaDirectory
$filesystem
catchException(App\Bootstrap $bootstrap, \Exception $exception)
Definition: Media.php:194