Merge pull request #4608 from awsr/awsr-patch-1

Fix folder cleanup
This commit is contained in:
Vladimir Mandic
2026-01-31 08:06:33 +01:00
committed by GitHub
+17 -18
View File
@@ -983,13 +983,13 @@ async function thumbCacheCleanup(folder, imgCount, controller, force = false) {
callback: async () => {
log(`Thumbnail DB cleanup: Checking if "${folder}" needs cleaning`);
const t0 = performance.now();
const staticGalleryHashes = new Set(galleryHashes.values()); // External context should be safe since this function run is guarded by AbortController/AbortSignal in the SimpleFunctionQueue
const keptGalleryHashes = force ? new Set() : new Set(galleryHashes.values()); // External context should be safe since this function run is guarded by AbortController/AbortSignal in the SimpleFunctionQueue
const cachedHashesCount = await idbCount(folder)
.catch((e) => {
error(`Thumbnail DB cleanup: Error when getting entry count for "${folder}".`, e);
return Infinity; // Forces next check to fail if something went wrong
});
const cleanupCount = cachedHashesCount - staticGalleryHashes.size;
const cleanupCount = cachedHashesCount - keptGalleryHashes.size;
if (!force && (cleanupCount < 500 || !Number.isFinite(cleanupCount))) {
// Don't run when there aren't many excess entries
return;
@@ -1000,10 +1000,10 @@ async function thumbCacheCleanup(folder, imgCount, controller, force = false) {
return;
}
const cb_clearMsg = showCleaningMsg(cleanupCount);
await idbFolderCleanup(staticGalleryHashes, folder, controller.signal)
await idbFolderCleanup(keptGalleryHashes, folder, controller.signal)
.then((delcount) => {
const t1 = performance.now();
log(`Thumbnail DB cleanup: folder=${folder} kept=${staticGalleryHashes.size} deleted=${delcount} time=${Math.floor(t1 - t0)}ms`);
log(`Thumbnail DB cleanup: folder=${folder} kept=${keptGalleryHashes.size} deleted=${delcount} time=${Math.floor(t1 - t0)}ms`);
currentGalleryFolder = null;
el.clearCacheFolder.innerText = '<select a folder first>';
updateStatusWithSort('Thumbnail cache cleared');
@@ -1058,19 +1058,6 @@ function clearCacheIfDisabled(browser_cache) {
}
}
function folderCleanupRunner(evt) {
evt.preventDefault();
evt.stopPropagation();
if (!currentGalleryFolder) return;
el.clearCacheFolder.style.color = 'var(--color-green)';
setTimeout(() => {
el.clearCacheFolder.style.color = 'var(--color-blue)';
}, 1000);
const controller = resetGalleryState('Clearing folder thumbnails cache');
el.files.innerHTML = '';
thumbCacheCleanup(currentGalleryFolder, 0, controller, true);
}
function addCacheClearLabel() { // Don't use async
const setting = document.querySelector('#setting_browser_cache');
if (setting) {
@@ -1080,11 +1067,23 @@ function addCacheClearLabel() { // Don't use async
const span = document.createElement('span');
span.style.cssText = 'font-weight: bold; text-decoration: underline; cursor: pointer; color: var(--color-blue); user-select: none;';
span.innerText = '<select a folder first>';
span.addEventListener('dblclick', folderCleanupRunner);
div.append('Clear the thumbnail cache for: ', span, ' (double-click)');
setting.parentElement.insertAdjacentElement('afterend', div);
el.clearCacheFolder = span;
span.addEventListener('dblclick', (evt) => {
evt.preventDefault();
evt.stopPropagation();
if (!currentGalleryFolder) return;
el.clearCacheFolder.style.color = 'var(--color-green)';
setTimeout(() => {
el.clearCacheFolder.style.color = 'var(--color-blue)';
}, 1000);
const controller = resetGalleryState('Clearing folder thumbnails cache');
el.files.innerHTML = '';
thumbCacheCleanup(currentGalleryFolder, 0, controller, true);
});
return true;
}
return false;