mirror of
https://github.com/unraid/webgui.git
synced 2026-02-25 11:49:12 -06:00
chore: move js cache to file
This commit is contained in:
@@ -14,30 +14,8 @@ if (isset($_COOKIE[session_name()])) {
|
||||
session_write_close();
|
||||
}
|
||||
|
||||
// Function to recursively find JS files in a directory
|
||||
function findJsFiles($directory) {
|
||||
if (!is_dir($directory)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$jsFiles = [];
|
||||
$iterator = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS)
|
||||
);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if ($file->isFile() && $file->getExtension() === 'js') {
|
||||
$path = $file->getPathname();
|
||||
$baseDir = '/usr/local/emhttp';
|
||||
if (strpos($path, $baseDir) === 0) {
|
||||
$path = substr($path, strlen($baseDir));
|
||||
}
|
||||
$jsFiles[] = $path;
|
||||
}
|
||||
}
|
||||
|
||||
return $jsFiles;
|
||||
}
|
||||
// Include JS caching functions
|
||||
require_once '/usr/local/emhttp/webGui/include/JSCache.php';
|
||||
|
||||
// Base whitelist of files
|
||||
$arrWhitelist = [
|
||||
@@ -69,15 +47,9 @@ $arrWhitelist = [
|
||||
'/webGui/images/UN-logotype-gradient.svg'
|
||||
];
|
||||
|
||||
// Add JS files from the unraid-components directory
|
||||
// Add JS files from the unraid-components directory using cache
|
||||
$webComponentsDirectory = '/usr/local/emhttp/plugins/dynamix.my.servers/unraid-components/';
|
||||
$jsFiles = findJsFiles($webComponentsDirectory);
|
||||
|
||||
// Add logo asset if needed
|
||||
// Note: In the original JS, there was a getters.paths().webgui.logo.assetPath that we'd add
|
||||
// Since we don't have access to that here, you may need to add the specific logo path if known
|
||||
|
||||
// Merge the JS files with the whitelist
|
||||
$jsFiles = getCachedJSFiles($webComponentsDirectory);
|
||||
$arrWhitelist = array_merge($arrWhitelist, $jsFiles);
|
||||
|
||||
if (in_array(preg_replace(['/\?v=\d+$/','/\?\d+$/'],'',$_SERVER['REQUEST_URI']),$arrWhitelist)) {
|
||||
|
||||
53
emhttp/plugins/dynamix/include/JSCache.php
Normal file
53
emhttp/plugins/dynamix/include/JSCache.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
// Function to recursively find JS files in a directory
|
||||
function findJsFiles($directory) {
|
||||
if (!is_dir($directory)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$jsFiles = [];
|
||||
$iterator = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS)
|
||||
);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if ($file->isFile() && $file->getExtension() === 'js') {
|
||||
$path = $file->getPathname();
|
||||
$baseDir = '/usr/local/emhttp';
|
||||
if (strpos($path, $baseDir) === 0) {
|
||||
$path = substr($path, strlen($baseDir));
|
||||
}
|
||||
$jsFiles[] = $path;
|
||||
}
|
||||
}
|
||||
|
||||
return $jsFiles;
|
||||
}
|
||||
|
||||
// Function to get JS files with caching
|
||||
function getCachedJSFiles($directory) {
|
||||
$cacheFile = '/tmp/js_files_cache.php';
|
||||
$cacheLifetime = 300; // Cache lifetime in seconds (5 minutes)
|
||||
|
||||
// Check if cache exists and is still valid
|
||||
$useCache = false;
|
||||
if (file_exists($cacheFile)) {
|
||||
$cacheTime = filemtime($cacheFile);
|
||||
if (time() - $cacheTime < $cacheLifetime) {
|
||||
$useCache = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($useCache) {
|
||||
// Use cached JS files list
|
||||
return include $cacheFile;
|
||||
} else {
|
||||
// Generate new JS files list
|
||||
$jsFiles = findJsFiles($directory);
|
||||
|
||||
// Store in cache file
|
||||
file_put_contents($cacheFile, '<?php return ' . var_export($jsFiles, true) . ';');
|
||||
|
||||
return $jsFiles;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user