From 28a719c8ad5de6b2c766b2a21988fd5305324630 Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 5 Nov 2026 19:22:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B5=D0=BB=D0=B5=D0=BA=D1=82=D0=B8?= =?UTF-8?q?=D0=B2=D0=BD=D0=B0=D1=8F=20=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=B1=D0=B8=D1=82=D1=8B=D1=85=20=D1=81=D1=81=D1=8B?= =?UTF-8?q?=D0=BB=D0=BE=D0=BA=20=D0=BD=D0=B0=20=D1=84=D0=BE=D1=82=D0=BE=20?= =?UTF-8?q?=D0=BB=D0=B8=D1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавлена модалка #faceBrokenCleanupModal со списком битых ссылок (эталоны student_face_refs и найденные лица photo_faces), выбором отдельных записей и удалением только помеченных. Эндпоинт POST /api/faces/broken/cleanup-all принимает массив ids для точечного удаления, иначе сканирует и удаляет все битые. Каждое удаление аудируется face_ref.delete_broken с фото и учеником. --- public/admin.css | 13 +++++ public/js/settings.js | 118 +++++++++++++++++++++++++++++++++++++++--- public/settings.html | 23 +++++++- server.js | 27 ++++++++-- 4 files changed, 170 insertions(+), 11 deletions(-) diff --git a/public/admin.css b/public/admin.css index 4addc55..5a19c9e 100644 --- a/public/admin.css +++ b/public/admin.css @@ -472,6 +472,19 @@ body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;b .face-empty{color:var(--muted);font-size:.78rem} .face-ref-profile-btn[hidden]{display:none} +/* --- Модалка битых ссылок на фото лиц (#faceBrokenCleanupModal) --- */ +.face-broken-list{max-height:340px;overflow:auto;border:1px solid var(--border);border-radius:8px;background:var(--bg)} +.face-broken-item{display:flex;align-items:center;gap:8px;padding:6px 10px;border-bottom:1px solid var(--border)} +.face-broken-item:last-child{border-bottom:none} +.face-broken-item:hover{background:rgba(0,0,0,.03)} +.face-broken-row{display:flex;align-items:center;gap:8px;width:100%;cursor:pointer} +.face-broken-row input{width:18px;height:18px;flex-shrink:0;margin:0} +.face-broken-info{display:flex;flex-direction:column;min-width:0} +.face-broken-student{font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap} +.face-broken-path{font-size:.72rem;color:var(--muted);overflow:hidden;text-overflow:ellipsis;white-space:nowrap} +.face-broken-table{font-size:.68rem;color:var(--muted);margin-top:2px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap} +.face-broken-empty{padding:14px;text-align:center;color:var(--muted)} + /* --- Профиль ученика (#profileDataModal) --- */ .pf-modal{padding:0;max-width:960px;width:min(96vw,960px);max-height:92dvh;overflow:hidden} .pf-form{display:flex;flex-direction:column;min-height:0;max-height:92dvh;gap:0} diff --git a/public/js/settings.js b/public/js/settings.js index d0e0184..1aa0dd0 100644 --- a/public/js/settings.js +++ b/public/js/settings.js @@ -874,7 +874,8 @@ document.getElementById('aiProfilesList').addEventListener('click', e => { document.addEventListener('keydown', e => { if (e.key === 'Escape') { - if (document.getElementById('bkCountsModal').classList.contains('open')) closeBackupCountsModal(); + if (document.getElementById('faceBrokenCleanupModal')?.classList.contains('open')) closeFaceBrokenCleanupModal(); + else if (document.getElementById('bkCountsModal').classList.contains('open')) closeBackupCountsModal(); else if (document.getElementById('aiProfileModal').classList.contains('open')) closeAiProfileForm(); } }); @@ -1456,6 +1457,15 @@ const faceBrokenScanBtn = document.getElementById('faceBrokenScanBtn'); const faceBrokenCleanupBtn = document.getElementById('faceBrokenCleanupBtn'); const faceBrokenStatus = document.getElementById('faceBrokenStatus'); const faceBrokenHint = document.getElementById('faceBrokenHint'); +const faceBrokenCleanupModal = document.getElementById('faceBrokenCleanupModal'); +const faceBrokenList = document.getElementById('faceBrokenList'); +const faceBrokenSelectAllBtn = document.getElementById('faceBrokenSelectAllBtn'); +const faceBrokenSelectNoneBtn = document.getElementById('faceBrokenSelectNoneBtn'); +const faceBrokenSelectCount = document.getElementById('faceBrokenSelectCount'); +const faceBrokenDeleteBtn = document.getElementById('faceBrokenDeleteBtn'); +const faceBrokenDeleteCount = document.getElementById('faceBrokenDeleteCount'); +const faceBrokenCancelBtn = document.getElementById('faceBrokenCancelBtn'); +let faceBrokenItems = []; async function loadFaceBrokenStatus() { if (!faceBrokenStatus) return; @@ -1488,19 +1498,113 @@ if (faceBrokenScanBtn) faceBrokenScanBtn.addEventListener('click', async () => { }); if (faceBrokenCleanupBtn) faceBrokenCleanupBtn.addEventListener('click', async () => { - if (!confirm('Удалить все битые ссылки на фото лиц (эталоны и найденные лица) из базы? Файлы уже удалены, ссылки остаются мёртвыми.')) return; - faceBrokenCleanupBtn.disabled = true; - if (faceBrokenStatus) faceBrokenStatus.textContent = 'Очистка...'; + await openFaceBrokenCleanupModal(); +}); + +async function openFaceBrokenCleanupModal() { + if (!faceBrokenCleanupModal || !faceBrokenList) return; + faceBrokenList.innerHTML = '
Загрузка списка битых ссылок...
'; + faceBrokenCleanupModal.classList.add('open'); try { - const res = await fetch(`${API}/api/faces/broken/cleanup-all`, { method: 'POST', headers: hdrJson() }); + const res = await fetch(`${API}/api/faces/broken/all?limit=5000`, { headers: hdr() }); + if (!res.ok) throw new Error('load failed'); + const d = await res.json(); + faceBrokenItems = (d.broken || []).map(b => ({ table: b.table, id: b.id, photo_path: b.photo_path || '', student_id: b.student_id, student_name: b.student_name || null })); + renderFaceBrokenList(); + } catch { + faceBrokenList.innerHTML = '
Ошибка загрузки списка битых ссылок
'; + } +} + +function closeFaceBrokenCleanupModal() { + if (!faceBrokenCleanupModal) return; + faceBrokenCleanupModal.classList.remove('open'); + faceBrokenItems = []; + faceBrokenList.innerHTML = ''; +} + +function renderFaceBrokenList() { + if (!faceBrokenList) return; + if (!faceBrokenItems.length) { + faceBrokenList.innerHTML = '
Битых ссылок не найдено
'; + updateFaceBrokenCount(); + return; + } + faceBrokenList.innerHTML = faceBrokenItems.map(item => { + const tableLabel = item.table === 'student_face_refs' ? 'эталон лица' : 'найденное лицо'; + const name = esc(item.student_name || 'Удалённый ученик'); + const path = esc(item.photo_path || ''); + return ` +
+ +
+ `; + }).join(''); + if (typeof renderIcons === 'function') renderIcons(); + bindFaceBrokenCheckboxes(); + updateFaceBrokenCount(); +} + +function bindFaceBrokenCheckboxes() { + if (!faceBrokenList) return; + faceBrokenList.querySelectorAll('.face-broken-cb').forEach(cb => { + cb.addEventListener('change', updateFaceBrokenCount); + }); +} + +function updateFaceBrokenCount() { + if (!faceBrokenList || !faceBrokenSelectCount || !faceBrokenDeleteCount) return; + const cbs = faceBrokenList.querySelectorAll('.face-broken-cb'); + const checked = faceBrokenList.querySelectorAll('.face-broken-cb:checked').length; + if (faceBrokenSelectCount) faceBrokenSelectCount.textContent = `Выбрано: ${checked} из ${cbs.length}`; + if (faceBrokenDeleteCount) faceBrokenDeleteCount.textContent = checked; + if (faceBrokenDeleteBtn) faceBrokenDeleteBtn.disabled = checked === 0; +} + +if (faceBrokenSelectAllBtn) faceBrokenSelectAllBtn.addEventListener('click', () => { + if (!faceBrokenList) return; + faceBrokenList.querySelectorAll('.face-broken-cb').forEach(cb => { cb.checked = true; }); + updateFaceBrokenCount(); +}); + +if (faceBrokenSelectNoneBtn) faceBrokenSelectNoneBtn.addEventListener('click', () => { + if (!faceBrokenList) return; + faceBrokenList.querySelectorAll('.face-broken-cb').forEach(cb => { cb.checked = false; }); + updateFaceBrokenCount(); +}); + +if (faceBrokenCancelBtn) faceBrokenCancelBtn.addEventListener('click', closeFaceBrokenCleanupModal); + +if (faceBrokenCleanupModal) faceBrokenCleanupModal.addEventListener('click', e => { + if (e.target === e.currentTarget) closeFaceBrokenCleanupModal(); +}); + +if (faceBrokenDeleteBtn) faceBrokenDeleteBtn.addEventListener('click', async () => { + if (!faceBrokenList) return; + const selected = []; + faceBrokenList.querySelectorAll('.face-broken-cb:checked').forEach(cb => { + selected.push({ table: cb.dataset.table, id: parseInt(cb.dataset.id, 10) }); + }); + if (!selected.length) return; + faceBrokenDeleteBtn.disabled = true; + try { + const res = await fetch(`${API}/api/faces/broken/cleanup-all`, { method: 'POST', headers: hdrJson(), body: JSON.stringify({ ids: selected }) }); const d = await res.json().catch(() => ({})); if (!res.ok) throw new Error(d.error || 'cleanup failed'); showToast(`Удалено битых ссылок: ${d.deleted || 0} из ${d.total || 0}`); + closeFaceBrokenCleanupModal(); loadFaceBrokenStatus(); } catch { - showError('Не удалось удалить битые ссылки'); + showError('Не удалось удалить выбранные битые ссылки'); } finally { - faceBrokenCleanupBtn.disabled = false; + faceBrokenDeleteBtn.disabled = false; } }); diff --git a/public/settings.html b/public/settings.html index 7048132..85844eb 100644 --- a/public/settings.html +++ b/public/settings.html @@ -323,12 +323,33 @@
Проверка: загрузка... - +
Битые ссылки можно также удалять по одной на странице ученика во вкладке «Лица» + +
diff --git a/server.js b/server.js index 18765c0..aa7a642 100644 --- a/server.js +++ b/server.js @@ -11915,9 +11915,30 @@ app.post('/api/faces/broken/scan', requireAuth, requireAdmin, async (req, res) = app.post('/api/faces/broken/cleanup-all', requireAuth, requireAdmin, async (req, res) => { const limit = Math.min(10000, Math.max(1, safeId(req.body.limit) || 1000)); - const result = await cleanupAllBrokenFaceRefs(pool, storage, { limit }); - await logAudit(req, 'faces.broken_link.cleanup_all', { deleted: result.deleted, total: result.total, errors: result.errors.length }); - res.json(result); + let targets; + if (Array.isArray(req.body.ids) && req.body.ids.length) { + targets = req.body.ids + .filter(i => i && (i.table === 'student_face_refs' || i.table === 'photo_faces') && Number.isInteger(i.id)) + .map(i => ({ table: i.table, id: i.id })); + } else { + const broken = await scanAllBrokenFaceRefs(pool, storage, { limit }); + targets = broken.map(b => ({ table: b.table, id: b.id, photo_path: b.photo_path, student_id: b.student_id })); + } + let deleted = 0; + const errors = []; + for (const item of targets) { + try { + const deletedRow = await deleteFaceRef(pool, storage, { table: item.table, id: item.id }); + if (deletedRow) { + deleted++; + await logAudit(req, 'face_ref.delete_broken', { student_id: item.student_id, ref_id: item.id, photo_path: deletedRow.photo_path || item.photo_path }); + } + } catch (e) { + errors.push({ table: item.table, id: item.id, error: e.message }); + } + } + await logAudit(req, 'faces.broken_link.cleanup_all', { deleted, total: targets.length, errors: errors.length, mode: req.body.ids ? 'selected' : 'all' }); + res.json({ deleted, total: targets.length, errors }); }); app.post('/api/faces/scan-unscanned', requireAuth, requireAdmin, async (req, res) => {