From 56be83c768fb675d128234516fc4e4440561dba5 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 12 Sep 2024 16:45:57 +0200 Subject: [PATCH] StaticController: Allow to access a lib's js/css assets --- .../Web/Controller/StaticController.php | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/library/Icinga/Web/Controller/StaticController.php b/library/Icinga/Web/Controller/StaticController.php index f5ce1631e0..3bbfe2d7b5 100644 --- a/library/Icinga/Web/Controller/StaticController.php +++ b/library/Icinga/Web/Controller/StaticController.php @@ -38,7 +38,29 @@ public function handle(Request $request) return; } - $assetRoot = $library->getStaticAssetPath(); + preg_match('~^(\w+)/~', $assetPath, $m); + switch ($m[1] ?? null) { + case 'js': + $assetPath = substr($assetPath, 3); + $assetRoot = $library->getJsAssetPath(); + $contentType = 'text/javascript'; + + break; + case 'css': + $assetPath = substr($assetPath, 4); + $assetRoot = $library->getCssAssetPath(); + $contentType = 'text/css'; + + break; + case 'static': + $assetPath = substr($assetPath, 7); + + // `static/` is the default + default: + $assetRoot = $library->getStaticAssetPath(); + $contentType = null; + } + if (empty($assetRoot)) { $app->getResponse() ->setHttpResponseCode(404); @@ -79,7 +101,7 @@ public function handle(Request $request) } else { $app->getResponse() ->setHeader('ETag', $eTag) - ->setHeader('Content-Type', mime_content_type($filePath), true) + ->setHeader('Content-Type', $contentType ?? mime_content_type($filePath), true) ->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', $fileStat['mtime']) . ' GMT') ->setBody(file_get_contents($filePath)); }