-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssets.php
More file actions
264 lines (190 loc) · 7.39 KB
/
Assets.php
File metadata and controls
264 lines (190 loc) · 7.39 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace MerapiPanel\Module\FileManager;
use MerapiPanel\Box;
use MerapiPanel\Box\FileFragment;
use MerapiPanel\Box\FileLoader;
use MerapiPanel\Box\Module\__Fragment;
use MerapiPanel\Box\Module\Entity\Module;
use MerapiPanel\Utility\Http\Request;
use MerapiPanel\Utility\Http\Response;
use Symfony\Component\Filesystem\Path;
class ModuleFileAssetsLoader extends FileLoader
{
public function getFile(string $name): Box\FileFragment|null
{
preg_match("/\@(\w+)/i", $name, $matches); // get module name
if (isset($matches[1])) { // assets with module namespace
$moduleName = ucfirst($matches[1]); // add module namespace
$module = Box::module($moduleName); // get module
$modulePath = $module->path; // module path
// $modulePath = $_ENV['__MP_APP__'] . "/Module/" . $moduleName; // module path
$path = ltrim(str_replace("@" . $matches[1], "", $name), "\\/"); // remove module namespace from path
if (strpos($path, "assets") !== 0 && file_exists($modulePath . "/assets/" . $path)) {
$path = "assets/" . $path;
} else if (strpos($path, "Assets") !== 0 && file_exists($modulePath . "/Assets/" . $path)) {
$path = "Assets/" . $path;
}
return new FileFragment(Path::join($modulePath, $path));
}
return null;
}
}
class BuildinFilesLoader extends FileLoader
{
public function getFile(string $name): Box\FileFragment|null
{
$name = ltrim($name, "\\/");
if (file_exists(Path::join($this->directory, $name)))
// file in directory directly
{
$path = Path::join($this->directory, $name);
} else if (strpos($name, "assets") !== 0 && file_exists(Path::join($this->directory, "assets", $name)))
// case sensitive in linux
{
$path = Path::join($this->directory, "assets", $name);
} else if (strpos($name, "Assets") !== 0 && file_exists(Path::join($this->directory, "Assets", $name)))
// case sensitive in linux
{
$path = Path::join($this->directory, "Assets", $name);
}
if (isset($path) && file_exists($path)) {
return new FileFragment($path);
}
return null;
}
}
enum AssetsType: string
{
case BUILDIN = "buildin";
case MODULE = "module";
}
class Assets extends __Fragment
{
public string $routeLink = "/public/assets/{data}";
protected Module $module;
protected $loaders = [
AssetsType::BUILDIN->value => [],
AssetsType::MODULE->value => []
];
function onCreate(Module $module)
{
$this->module = $module;
$this->loaders = [
AssetsType::BUILDIN->value => [
new BuildinFilesLoader(Path::join($_ENV['__MP_APP__'], "Buildin"))
],
AssetsType::MODULE->value => [
new ModuleFileAssetsLoader(Path::join($_ENV['__MP_APP__'], "Module"))
]
];
}
public function addLoader(AssetsType|string $type, FileLoader $loader)
{
if (!in_array($type, array_keys($this->loaders))) {
throw new \Exception("Invalid type: " . $type);
}
$this->loaders[$type instanceof AssetsType ? $type->value : $type][] = $loader;
}
/**
* Generates a URL for the given absolute file path, with caching for performance.
*
* @param string $absoluteFilePath The absolute file path for which to generate the URL
* @return string The generated URL
*/
public function url($absoluteFilePath, $encrypt = false)
{
// Remove query string from the absolute file path
$absoluteFilePath = preg_replace("/\?.*/", "", $absoluteFilePath);
$final = str_replace("{data}", $absoluteFilePath, $this->routeLink);
// Return the generated URL
return $final;
}
public function getAssset(Request $req)
{
$realPath = $this->getRealPath($req->data);
if (!$realPath || !is_file($realPath)) {
return [
"code" => 404,
"message" => "Assets not found"
];
}
return $this->sendResponse($req, $realPath);
}
private function loadAssetComponent($req, $source, $file)
{
$refRealPath = $this->getRealPath($source);
if (!file_exists($refRealPath)) {
return [
"code" => 404,
"message" => "Referer assets not found",
];
}
$dirname = dirname($refRealPath);
$fullPath = rtrim($dirname, "\/") . "/" . ltrim($file, "\/");
if (!file_exists($fullPath)) {
$fullPath = rtrim($dirname, "\/") . "/" . pathinfo(ltrim($file, "\/"), PATHINFO_BASENAME);
}
if (!file_exists($fullPath)) {
return [
"code" => 404,
"message" => "Assets not found ",
];
}
return $this->sendResponse($req, $fullPath);
}
private function sendResponse($req, $file): Response
{
$response = new Response();
if ($_ENV['__MP_CACHE__'] === true) {
// Determine the last modified time of the file for caching
$lastModifiedTime = filemtime($file);
$etag = md5($lastModifiedTime . $file);
$response->setStatusCode(200);
$response->setHeader("Status-Code", 200);
$response->setHeader("Cache-Control", "public, max-age=86400");
$response->setHeader("Last-Modified", gmdate("D, d M Y H:i:s", $lastModifiedTime) . " GMT");
$response->setHeader("Etag", $etag);
// Check if the page has been modified since the browser's cache
if ($req->http("if-modified-since") && $req->http("if-modified-since") == gmdate("D, d M Y H:i:s", $lastModifiedTime)) {
// Return 304 Not Modified without any content if the ETag matches
$response->setStatusCode(304);
return $response;
}
}
$output = file_get_contents($file);
$mimeTypes = json_decode(file_get_contents(__DIR__ . "/mimeType.json"), true);
// Set the appropriate Content-Type header
$contentType = $mimeTypes[strtolower(pathinfo($file, PATHINFO_EXTENSION))] ?? 'application/octet-stream'; // Default to binary if MIME type is application/octet-stream
$response->setHeader("Content-Type", $contentType);
$response->setContent($output);
return $response;
}
private function getRealPath($path): bool|FileFragment|null
{
if (!$path) {
return false;
}
$file_result = null;
preg_match("/\@(\w+)/i", $path, $matches); // get module name
if (isset($matches[1])) { // assets with module namespace
/**
* @var ModuleFileAssetsLoader $loader
*/
foreach ($this->loaders[AssetsType::MODULE->value] as $loader) {
if ($file_result = $loader->getFile($path)) {
break;
}
}
} else {
/**
* @var BuildinFilesLoader $loader
*/
foreach ($this->loaders[AssetsType::BUILDIN->value] as $loader) {
if ($file_result = $loader->getFile($path)) {
break;
}
}
}
return $file_result;
}
}