Update gallery cache if image count changes

This commit is contained in:
2025-10-29 23:27:30 +00:00
parent 370f32ac06
commit 118cdc0dbf

View File

@@ -16,6 +16,13 @@ $cache_file = $CONFIG['cache_file'];
// Serve cached gallery JSON if fresh
if (file_exists($cache_file)) {
$data = json_decode(file_get_contents($cache_file), true);
$count = count_all_images($CONFIG);
if ($count !== $data['countall']) {
unlink($cache_file);
$data = buildGalleryCache($CONFIG);
if (!is_dir(dirname($cache_file))) @mkdir(dirname($cache_file), 0775, true);
file_put_contents($cache_file, json_encode($data, JSON_PRETTY_PRINT));
}
} else {
$data = buildGalleryCache($CONFIG);
if (!is_dir(dirname($cache_file))) @mkdir(dirname($cache_file), 0775, true);
@@ -37,6 +44,30 @@ echo json_encode([
// --- FUNCTIONS ---
function count_all_images($CONFIG) {
$dir = $CONFIG['sorted_img_dir'];
if (!is_dir($dir)) {
return 0;
}
$count = 0;
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS)
);
foreach ($iterator as $file) {
if ($file->isFile()) {
$ext = strtolower($file->getExtension());
if ($ext === 'arw' || $ext === 'raw' ||
$ext === 'jpg' || $ext === 'png' ) {
$count++;
}
}
}
return $count;
}
function count_raw_files($CONFIG) {
$dir = $CONFIG['sorted_img_dir'].'/arw';
if (!is_dir($dir)) {
@@ -101,6 +132,7 @@ function buildGalleryCache($CONFIG) {
'generated' => date('c'),
'countjpg' => count($images),
'countraw' => count_raw_files($CONFIG),
'countall' => count_all_images($CONFIG),
'images' => $images
];
}