Skip to content

Commit

Permalink
StaticController: Allow to access a lib's js/css assets
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Oct 7, 2024
1 parent c4b6e4b commit 56be83c
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions library/Icinga/Web/Controller/StaticController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 56be83c

Please sign in to comment.