|
7 | 7 | */ |
8 | 8 | namespace Magento\MediaStorage\App; |
9 | 9 |
|
10 | | -use Magento\MediaStorage\Model\File\Storage\Request; |
| 10 | +use Magento\Framework\Filesystem; |
| 11 | +use Magento\MediaStorage\Model\File\Storage\ConfigFactory; |
11 | 12 | use Magento\MediaStorage\Model\File\Storage\Response; |
12 | 13 | use Magento\Framework\App; |
13 | 14 | use Magento\Framework\App\Filesystem\DirectoryList; |
14 | 15 | use Magento\Framework\AppInterface; |
15 | | -use Magento\Framework\ObjectManagerInterface; |
| 16 | +use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory; |
16 | 17 |
|
| 18 | +/** |
| 19 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 20 | + */ |
17 | 21 | class Media implements AppInterface |
18 | 22 | { |
19 | | - /** |
20 | | - * @var \Magento\Framework\ObjectManagerInterface |
21 | | - */ |
22 | | - protected $_objectManager; |
23 | | - |
24 | | - /** |
25 | | - * @var \Magento\MediaStorage\Model\File\Storage\Request |
26 | | - */ |
27 | | - protected $_request; |
28 | | - |
29 | 23 | /** |
30 | 24 | * Authorization function |
31 | 25 | * |
32 | 26 | * @var \Closure |
33 | 27 | */ |
34 | | - protected $_isAllowed; |
| 28 | + private $isAllowed; |
35 | 29 |
|
36 | 30 | /** |
37 | 31 | * Media directory path |
38 | 32 | * |
39 | 33 | * @var string |
40 | 34 | */ |
41 | | - protected $_mediaDirectory; |
| 35 | + private $mediaDirectoryPath; |
42 | 36 |
|
43 | 37 | /** |
44 | 38 | * Configuration cache file path |
45 | 39 | * |
46 | 40 | * @var string |
47 | 41 | */ |
48 | | - protected $_configCacheFile; |
| 42 | + private $configCacheFile; |
49 | 43 |
|
50 | 44 | /** |
51 | 45 | * Requested file name relative to working directory |
52 | 46 | * |
53 | 47 | * @var string |
54 | 48 | */ |
55 | | - protected $_relativeFileName; |
| 49 | + private $relativeFileName; |
56 | 50 |
|
57 | 51 | /** |
58 | | - * Working directory |
59 | | - * |
60 | | - * @var string |
| 52 | + * @var Response |
61 | 53 | */ |
62 | | - protected $_workingDirectory; |
| 54 | + private $response; |
63 | 55 |
|
64 | 56 | /** |
65 | | - * @var \Magento\MediaStorage\Model\File\Storage\Response |
| 57 | + * @var \Magento\Framework\Filesystem\Directory\WriteInterface |
66 | 58 | */ |
67 | | - protected $_response; |
| 59 | + private $directory; |
68 | 60 |
|
69 | 61 | /** |
70 | | - * @var \Magento\Framework\Filesystem $filesystem |
| 62 | + * @var ConfigFactory |
71 | 63 | */ |
72 | | - protected $filesystem; |
| 64 | + private $configFactory; |
73 | 65 |
|
74 | 66 | /** |
75 | | - * @var \Magento\Framework\Filesystem\Directory\Read $directory |
| 67 | + * @var SynchronizationFactory |
76 | 68 | */ |
77 | | - protected $directory; |
| 69 | + private $syncFactory; |
78 | 70 |
|
79 | 71 | /** |
80 | | - * @param ObjectManagerInterface $objectManager |
81 | | - * @param Request $request |
| 72 | + * @param ConfigFactory $configFactory |
| 73 | + * @param SynchronizationFactory $syncFactory |
82 | 74 | * @param Response $response |
83 | 75 | * @param \Closure $isAllowed |
84 | | - * @param string $workingDirectory |
85 | 76 | * @param string $mediaDirectory |
86 | 77 | * @param string $configCacheFile |
87 | 78 | * @param string $relativeFileName |
88 | | - * @param \Magento\Framework\Filesystem $filesystem |
| 79 | + * @param Filesystem $filesystem |
89 | 80 | */ |
90 | 81 | public function __construct( |
91 | | - ObjectManagerInterface $objectManager, |
92 | | - Request $request, |
| 82 | + ConfigFactory $configFactory, |
| 83 | + SynchronizationFactory $syncFactory, |
93 | 84 | Response $response, |
94 | 85 | \Closure $isAllowed, |
95 | | - $workingDirectory, |
96 | 86 | $mediaDirectory, |
97 | 87 | $configCacheFile, |
98 | 88 | $relativeFileName, |
99 | | - \Magento\Framework\Filesystem $filesystem |
| 89 | + Filesystem $filesystem |
100 | 90 | ) { |
101 | | - $this->_objectManager = $objectManager; |
102 | | - $this->_request = $request; |
103 | | - $this->_response = $response; |
104 | | - $this->_isAllowed = $isAllowed; |
105 | | - $this->_workingDirectory = $workingDirectory; |
106 | | - $this->_mediaDirectory = $mediaDirectory; |
107 | | - $this->_configCacheFile = $configCacheFile; |
108 | | - $this->_relativeFileName = $relativeFileName; |
109 | | - $this->filesystem = $filesystem; |
110 | | - $this->directory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); |
| 91 | + $this->response = $response; |
| 92 | + $this->isAllowed = $isAllowed; |
| 93 | + $this->directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); |
| 94 | + $mediaDirectory = trim($mediaDirectory); |
| 95 | + if (!empty($mediaDirectory)) { |
| 96 | + $this->mediaDirectoryPath = str_replace('\\', '/', realpath($mediaDirectory)); |
| 97 | + } |
| 98 | + $this->configCacheFile = $configCacheFile; |
| 99 | + $this->relativeFileName = $relativeFileName; |
| 100 | + $this->configFactory = $configFactory; |
| 101 | + $this->syncFactory = $syncFactory; |
111 | 102 | } |
112 | 103 |
|
113 | 104 | /** |
114 | 105 | * Run application |
115 | 106 | * |
116 | | - * @return \Magento\Framework\App\ResponseInterface |
| 107 | + * @return Response |
117 | 108 | * @throws \LogicException |
118 | 109 | */ |
119 | 110 | public function launch() |
120 | 111 | { |
121 | | - if (!$this->_mediaDirectory) { |
122 | | - $config = $this->_objectManager->create( |
123 | | - 'Magento\MediaStorage\Model\File\Storage\Config', |
124 | | - ['cacheFile' => $this->_configCacheFile] |
125 | | - ); |
| 112 | + if ($this->mediaDirectoryPath !== $this->directory->getAbsolutePath()) { |
| 113 | + // Path to media directory changed or absent - update the config |
| 114 | + /** @var \Magento\MediaStorage\Model\File\Storage\Config $config */ |
| 115 | + $config = $this->configFactory->create(['cacheFile' => $this->configCacheFile]); |
126 | 116 | $config->save(); |
127 | | - $this->_mediaDirectory = str_replace($this->_workingDirectory, '', $config->getMediaDirectory()); |
| 117 | + $this->mediaDirectoryPath = $config->getMediaDirectory(); |
128 | 118 | $allowedResources = $config->getAllowedResources(); |
129 | | - $this->_relativeFileName = str_replace( |
130 | | - $this->_mediaDirectory . '/', |
131 | | - '', |
132 | | - $this->_request->getPathInfo() |
133 | | - ); |
134 | | - $isAllowed = $this->_isAllowed; |
135 | | - if (!$isAllowed($this->_relativeFileName, $allowedResources)) { |
| 119 | + $isAllowed = $this->isAllowed; |
| 120 | + if (!$isAllowed($this->relativeFileName, $allowedResources)) { |
136 | 121 | throw new \LogicException('The specified path is not allowed.'); |
137 | 122 | } |
138 | 123 | } |
139 | 124 |
|
140 | | - if (0 !== stripos($this->_request->getPathInfo(), $this->_mediaDirectory . '/')) { |
141 | | - throw new \LogicException('The specified path is not within media directory.'); |
142 | | - } |
143 | | - |
144 | | - $sync = $this->_objectManager->get('Magento\MediaStorage\Model\File\Storage\Synchronization'); |
145 | | - $sync->synchronize($this->_relativeFileName, $this->_request->getFilePath()); |
| 125 | + /** @var \Magento\MediaStorage\Model\File\Storage\Synchronization $sync */ |
| 126 | + $sync = $this->syncFactory->create(['directory' => $this->directory]); |
| 127 | + $sync->synchronize($this->relativeFileName); |
146 | 128 |
|
147 | | - if ($this->directory->isReadable($this->directory->getRelativePath($this->_request->getFilePath()))) { |
148 | | - $this->_response->setFilePath($this->_request->getFilePath()); |
| 129 | + if ($this->directory->isReadable($this->relativeFileName)) { |
| 130 | + $this->response->setFilePath($this->directory->getAbsolutePath($this->relativeFileName)); |
149 | 131 | } else { |
150 | | - $this->_response->setHttpResponseCode(404); |
| 132 | + $this->response->setHttpResponseCode(404); |
151 | 133 | } |
152 | | - return $this->_response; |
| 134 | + return $this->response; |
153 | 135 | } |
154 | 136 |
|
155 | 137 | /** |
156 | 138 | * {@inheritdoc} |
157 | 139 | */ |
158 | 140 | public function catchException(App\Bootstrap $bootstrap, \Exception $exception) |
159 | 141 | { |
160 | | - $this->_response->setHttpResponseCode(404); |
161 | | - $this->_response->sendHeaders(); |
| 142 | + $this->response->setHttpResponseCode(404); |
| 143 | + if ($bootstrap->isDeveloperMode()) { |
| 144 | + $this->response->setHeader('Content-Type', 'text/plain'); |
| 145 | + $this->response->setBody($exception->getMessage() . "\n" . $exception->getTraceAsString()); |
| 146 | + } |
| 147 | + $this->response->sendResponse(); |
162 | 148 | return true; |
163 | 149 | } |
164 | 150 | } |
0 commit comments