Фоновый мониторинг битых ссылок на фото лиц с уведомлениями админа
- lib/photo-cleanup.js: scanAllBrokenFaceRefs / cleanupAllBrokenFaceRefs сканируют student_face_refs и photo_faces на отсутствующие файлы - server.js: тип уведомления photo.broken_link, настройки faces_broken_scan_enabled / faces_broken_scan_interval_hours, фоновый таймер (раз в 6 часов), админ-роуты GET /api/faces/broken/all, POST /api/faces/broken/scan, POST /api/faces/broken/cleanup-all - public/settings.html + public/js/settings.js: карточка мониторинга в настройках (вкл/выкл, интервал, проверить сейчас, удалить все, статус) - public/js/student.js: битые ссылки во вкладке «Лица» ученика с удалением по одной и пакетной очисткой для админа - db/init.sql + db/migration.sql: сиды новых настроек
This commit is contained in:
@@ -42,6 +42,49 @@ async function findBrokenFaceRefs(pool, storage, { studentId, limit } = {}) {
|
||||
return results;
|
||||
}
|
||||
|
||||
async function scanAllBrokenFaceRefs(pool, storage, { limit } = {}) {
|
||||
const results = [];
|
||||
for (const table of FACE_REF_TABLES) {
|
||||
let sql;
|
||||
const params = [];
|
||||
if (table === 'student_face_refs') {
|
||||
sql = `SELECT id, photo_path, student_id FROM student_face_refs WHERE photo_path IS NOT NULL AND photo_path <> ''`;
|
||||
} else {
|
||||
sql = `SELECT id, photo_path, student_id FROM photo_faces WHERE photo_path IS NOT NULL AND photo_path <> ''`;
|
||||
}
|
||||
sql += ` ORDER BY id`;
|
||||
if (limit) {
|
||||
sql += ` LIMIT $${params.length + 1}`;
|
||||
params.push(limit);
|
||||
}
|
||||
const { rows } = await pool.query(sql, params);
|
||||
for (const row of rows) {
|
||||
const key = storage.keyFromPath(row.photo_path);
|
||||
if (!key) continue;
|
||||
const exists = await storage.exists(key);
|
||||
if (!exists) {
|
||||
results.push({ table, id: row.id, photo_path: row.photo_path, student_id: row.student_id });
|
||||
}
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
async function cleanupAllBrokenFaceRefs(pool, storage, { limit } = {}) {
|
||||
const broken = await scanAllBrokenFaceRefs(pool, storage, { limit });
|
||||
let deleted = 0;
|
||||
const errors = [];
|
||||
for (const item of broken) {
|
||||
try {
|
||||
await deleteFaceRef(pool, storage, { table: item.table, id: item.id });
|
||||
deleted++;
|
||||
} catch (e) {
|
||||
errors.push({ table: item.table, id: item.id, error: e.message });
|
||||
}
|
||||
}
|
||||
return { broken, deleted, total: broken.length, errors };
|
||||
}
|
||||
|
||||
async function deleteFaceRef(pool, storage, { table, id }) {
|
||||
if (!isFaceTable(table)) {
|
||||
throw new Error('Недопустимая таблица');
|
||||
@@ -55,6 +98,8 @@ async function deleteFaceRef(pool, storage, { table, id }) {
|
||||
|
||||
module.exports = {
|
||||
findBrokenFaceRefs,
|
||||
scanAllBrokenFaceRefs,
|
||||
cleanupAllBrokenFaceRefs,
|
||||
deleteFaceRef,
|
||||
FACE_REF_TABLES,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user