-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
UrlConvertor.php
231 lines (199 loc) · 6.88 KB
/
UrlConvertor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
declare(strict_types=1);
namespace Yireo\NextGenImages\Util;
use Magento\Framework\App\Filesystem\DirectoryList as FilesystemDirectoryList;
use Magento\Framework\Escaper;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Asset\File\NotFoundException;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
class UrlConvertor
{
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @var Escaper
*/
private $escaper;
/**
* @param StoreManagerInterface $storeManager
* @param DirectoryList $directoryList
* @param Escaper $escaper
*/
public function __construct(
StoreManagerInterface $storeManager,
DirectoryList $directoryList,
Escaper $escaper
) {
$this->storeManager = $storeManager;
$this->directoryList = $directoryList;
$this->escaper = $escaper;
}
/**
* @param string $url
* @return bool
*/
public function isLocal(string $url): bool
{
$url = $this->normalizeUrl($url);
if (!preg_match('#^http(s?)://#', $url)) {
return true;
}
foreach ($this->storeManager->getStores() as $store) {
/** @var Store $store */
$storeBaseUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_WEB);
if (strpos($url, $storeBaseUrl) !== false) {
return true;
}
$storeMediaUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
if (strpos($url, $storeMediaUrl) !== false) {
return true;
}
$storeStaticUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_STATIC);
if (strpos($url, $storeStaticUrl) !== false) {
return true;
}
}
return false;
}
/**
* @param string $filename
* @return string
* @throws NoSuchEntityException
*/
public function getUrlFromFilename(string $filename): string
{
try {
if (strpos($filename, $this->getMediaFolder()) !== false) {
return str_replace($this->getMediaFolder() . '/', $this->getMediaUrl(), $filename);
}
} catch (FileSystemException|NoSuchEntityException $e) {
throw new NotFoundException((string)__('Media folder does not exist'));
}
try {
if (strpos($filename, $this->getStaticFolder()) !== false) {
return str_replace($this->getStaticFolder() . '/', $this->getStaticUrl(), $filename);
}
} catch (FileSystemException|NoSuchEntityException $e) {
throw new NotFoundException((string)__('Static folder does not exist'));
}
if (strpos($filename, $this->getBaseFolder()) !== false) {
return str_replace($this->getBaseFolder() . '/', $this->getBaseUrl(), $filename);
}
if (!preg_match('/^\//', $filename)) {
return $this->getBaseUrl() . $filename;
}
throw new NotFoundException((string)__('Filename "' . $filename . '" is not matched with an URL'));
}
/**
* @param string $url
* @return string
* @throws FileSystemException
*/
public function getFilenameFromUrl(string $url): string
{
$url = (string)$this->escaper->escapeHtml($url);
$url = preg_replace('/\/static\/version(\d+\/)/', '/static/', $url);
$url = str_replace('/pub/', '/', $url);
$url = $this->normalizeUrl($url);
if ($this->isLocal($url) === false) {
throw new NotFoundException((string)__('URL "' . $url . '" does not appear to be a local file'));
}
foreach ($this->storeManager->getStores() as $store) {
/** @var Store $store */
$storeBaseUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_WEB);
if (strpos($url, $storeBaseUrl) !== false) {
return str_replace($storeBaseUrl, $this->getBaseFolder() . '/', $url);
}
$storeMediaUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
if (strpos($url, $storeMediaUrl) !== false) {
return str_replace($storeMediaUrl, $this->getMediaFolder() . '/', $url);
}
$staticUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_STATIC);
if (strpos($url, $staticUrl) !== false) {
return str_replace($staticUrl, $this->getStaticFolder() . '/', $url);
}
}
if (preg_match('/^\//', $url)) {
return $this->getBaseFolder() . $url;
}
// @todo: https://gitlab.hyva.io/hyva-themes/hyva-compat/magento2-yireo-next-gen-images/-/blob/main/src/Plugin/UrlConvertorPlugin.php#L17
if (preg_match('/image\.(img|full|thumb|url)/m', $url)) {
return $url;
}
throw new NotFoundException((string)__('URL "' . $url . '" is not matched with a local file'));
}
/**
* @return string
* @throws NoSuchEntityException
*/
private function getBaseUrl(): string
{
$store = $this->storeManager->getStore();
$baseUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_WEB);
return $this->normalizeUrl($baseUrl);
}
/**
* @return string
* @throws NoSuchEntityException
*/
private function getMediaUrl(): string
{
/** @var Store $store */
$store = $this->storeManager->getStore();
$mediaUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
return $this->normalizeUrl($mediaUrl);
}
/**
* @return string
* @throws NoSuchEntityException
*/
private function getStaticUrl(): string
{
/** @var Store $store */
$store = $this->storeManager->getStore();
$staticUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_STATIC);
return $this->normalizeUrl($staticUrl);
}
/**
* @return string
*/
private function getBaseFolder(): string
{
return $this->directoryList->getRoot() . '/pub';
}
/**
* @return string
* @throws FileSystemException
*/
private function getMediaFolder(): string
{
return rtrim($this->directoryList->getPath(FilesystemDirectoryList::MEDIA), '/');
}
/**
* @return string
* @throws FileSystemException
*/
private function getStaticFolder(): string
{
return rtrim($this->directoryList->getPath(FilesystemDirectoryList::STATIC_VIEW), '/');
}
/**
* @param string $url
* @return string
*/
private function normalizeUrl(string $url): string
{
$url = str_replace('/index.php/', '/', $url);
return preg_replace('#^//#', 'http://', $url);
}
}