Add maintenance: purge orphaned source image KV entries

When a source image file is deleted without going through the API
(e.g. the old bin delete bug), the KV entry remains and shows a
broken image. The new maintenance action scans all source image
entries, removes any whose file is missing on disk, and reports
how many were cleaned up.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-03 03:08:38 +00:00
parent b200a7ec8d
commit 1aa7350c4d
4 changed files with 35 additions and 2 deletions

View File

@@ -3,7 +3,7 @@ process.on('uncaughtException', (err) => { console.error('[uncaughtException
import express from 'express';
import multer from 'multer';
import { unlinkSync, mkdirSync } from 'node:fs';
import { unlinkSync, mkdirSync, existsSync } from 'node:fs';
import { extname, join } from 'node:path';
import { execFileSync } from 'node:child_process';
import sharp from 'sharp';
@@ -634,6 +634,18 @@ app.delete('/api/pdfs/:id', (req, res) => {
// Maintenance
// ---------------------------------------------------------------------------
app.post('/api/maintenance/purge-missing-sources', (req, res) => {
const sources = list_source_images();
const removed = [];
for (const src of sources) {
if (!existsSync(join('./data/images', src.id))) {
delete_source_image(src.id);
removed.push(src.id);
}
}
ok(res, { removed, total: sources.length });
});
app.post('/api/maintenance/pdf-thumbs', (req, res) => {
const pdfs = list_pdfs();
let generated = 0;