feat(backup): include digests and digest_images in backup/restore and sweep

This commit is contained in:
dev
2026-11-04 11:58:38 +03:00
parent cefa336ea4
commit 9dd8ba20c0
3 changed files with 52 additions and 7 deletions
+27 -3
View File
@@ -1811,7 +1811,7 @@ async function removeEntryFiles(entryId) {
async function sweepOrphanedUploads() {
const dir = UPLOADS_DIR;
try { fs.mkdirSync(dir, { recursive: true }); } catch {}
const [{ rows: photos }, { rows: files }, { rows: gphotos }, { rows: gporiginals }, { rows: ephotos }, { rows: pendingJobs }, { rows: mphotos }, { rows: sphotos }, { rows: logos }, { rows: studentAvatars }, { rows: userAvatars }, { rows: groupCovers }, { rows: originals }, { rows: jobBefore }, { rows: chatFiles }, { rows: bmarkPhotos }, { rows: faceRefs }, { rows: docPhotos }, { rows: docFiles }] = await Promise.all([
const [{ rows: photos }, { rows: files }, { rows: gphotos }, { rows: gporiginals }, { rows: ephotos }, { rows: pendingJobs }, { rows: mphotos }, { rows: sphotos }, { rows: logos }, { rows: studentAvatars }, { rows: userAvatars }, { rows: groupCovers }, { rows: originals }, { rows: jobBefore }, { rows: chatFiles }, { rows: bmarkPhotos }, { rows: faceRefs }, { rows: docPhotos }, { rows: docFiles }, { rows: digestImages }, { rows: digestPreviews }] = await Promise.all([
pool.query('SELECT photo_path AS p FROM entries WHERE photo_path IS NOT NULL'),
pool.query('SELECT path AS p FROM project_files'),
pool.query('SELECT photo_path AS p FROM group_photos'),
@@ -1831,9 +1831,11 @@ async function sweepOrphanedUploads() {
pool.query('SELECT photo_path AS p FROM student_face_refs'),
pool.query('SELECT path AS p FROM doc_images'),
pool.query('SELECT path AS p FROM doc_files'),
pool.query('SELECT path AS p FROM digest_images'),
pool.query('SELECT preview_path AS p FROM digests WHERE preview_path IS NOT NULL'),
]);
const refs = new Set();
[...photos, ...files, ...gphotos, ...gporiginals, ...ephotos, ...pendingJobs, ...mphotos, ...sphotos, ...logos, ...studentAvatars, ...userAvatars, ...groupCovers, ...originals, ...jobBefore, ...chatFiles, ...bmarkPhotos, ...faceRefs, ...docPhotos, ...docFiles].forEach(r => {
[...photos, ...files, ...gphotos, ...gporiginals, ...ephotos, ...pendingJobs, ...mphotos, ...sphotos, ...logos, ...studentAvatars, ...userAvatars, ...groupCovers, ...originals, ...jobBefore, ...chatFiles, ...bmarkPhotos, ...faceRefs, ...docPhotos, ...docFiles, ...digestImages, ...digestPreviews].forEach(r => {
const key = storage.keyFromPath(r.p);
if (key) refs.add(key);
});
@@ -1872,6 +1874,8 @@ const BROKEN_IMAGE_SOURCES = [
{ key: 'student_face_refs.photo_path', table: 'student_face_refs', column: 'photo_path', idColumn: 'id', kind: 'row', label: 'Эталон лица' },
{ key: 'doc_images.path', table: 'doc_images', column: 'path', idColumn: 'id', kind: 'row', label: 'Картинка документации' },
{ key: 'doc_files.path', table: 'doc_files', column: 'path', idColumn: 'id', kind: 'row', label: 'Вложение документации' },
{ key: 'digest_images.path', table: 'digest_images', column: 'path', idColumn: 'id', kind: 'row', label: 'Картинка дайджеста' },
{ key: 'digests.preview_path', table: 'digests', column: 'preview_path', idColumn: 'id', kind: 'column', label: 'Обложка дайджеста' },
{ key: 'project_files.path', table: 'project_files', column: 'path', idColumn: 'id', kind: 'row', label: 'Файл проекта' },
{ key: 'chat_attachments.path', table: 'chat_attachments', column: 'path', idColumn: 'id', kind: 'row', label: 'Вложение чата' },
{ key: 'photo_jobs.before_path', table: 'photo_jobs', column: 'before_path', idColumn: 'id', kind: 'column', label: 'Фото до обработки' },
@@ -5278,6 +5282,9 @@ app.post('/api/digests', requireAuth, async (req, res) => {
try {
const item = await createDigest(pool, req.body, req.user.id);
await logAudit(req, 'digest.create', { id: item.id, title: item.title });
if (item.published_at) {
await onDigestPublished(item).catch(() => {});
}
res.json({ item });
} catch (e) {
if (e.message === 'Неверная длина заголовка' || e.message === 'Неверная длина текста') {
@@ -7024,7 +7031,7 @@ function backupUploadGuard(req, res, next) {
async function buildBackupArchive() {
const staging = fs.mkdtempSync(path.join(os.tmpdir(), 'wido-bk-'));
try {
const [g, s, e, st, pf, br, us, ub, gp, ep, md, sp, sl, pj, lr, lrv, al, nt, nr, bi, ct, cm, ca, sm, fb, uss, un, bm, sfr, pfcs, swm, ds, da, di, df, si, atr, sar] = await Promise.all([
const [g, s, e, st, pf, br, us, ub, gp, ep, md, sp, sl, pj, lr, lrv, al, nt, nr, bi, ct, cm, ca, sm, fb, uss, un, bm, sfr, pfcs, swm, ds, da, di, df, si, atr, sar, dg, dgi] = await Promise.all([
pool.query('SELECT * FROM groups ORDER BY id'),
pool.query('SELECT * FROM students ORDER BY id'),
pool.query('SELECT * FROM entries ORDER BY id'),
@@ -7063,6 +7070,8 @@ async function buildBackupArchive() {
pool.query('SELECT * FROM sidebar_items ORDER BY id'),
pool.query('SELECT * FROM achievement_types ORDER BY id'),
pool.query('SELECT * FROM student_achievements ORDER BY id'),
pool.query('SELECT * FROM digests ORDER BY id'),
pool.query('SELECT * FROM digest_images ORDER BY id'),
]);
const settings = {};
st.rows.forEach(r => { settings[r.key] = r.value; });
@@ -7084,11 +7093,13 @@ async function buildBackupArchive() {
achievement_types: atr.rowCount, student_achievements: sar.rowCount,
doc_sections: ds.rowCount, doc_articles: da.rowCount, doc_images: di.rowCount,
doc_files: df.rowCount, sidebar_items: si.rowCount,
digests: dg.rowCount, digest_images: dgi.rowCount,
},
groups: g.rows, students: s.rows, entries: e.rows, settings, project_files: pf.rows, branches: br.rows, users: us.rows, user_branches: ub.rows, group_photos: gp.rows, entry_photos: ep.rows, modules: md.rows, student_photos: sp.rows, share_links: sl.rows, photo_jobs: pj.rows, lesson_reports: lr.rows, lesson_report_versions: lrv.rows, audit_log: al.rows, notifications: nt.rows, notification_reads: nr.rows, banned_ips: bi.rows, chat_threads: ct.rows, chat_messages: cm.rows, chat_attachments: ca.rows, short_messages: sm.rows, feedbacks: fb.rows, user_settings: uss.rows, user_notes: un.rows, bookmarks: bm.rows, student_face_refs: sfr.rows, photo_faces: pfcs.rows, student_work_marks: swm.rows,
achievement_types: atr.rows, student_achievements: sar.rows,
doc_sections: ds.rows, doc_articles: da.rows, doc_images: di.rows,
doc_files: df.rows, sidebar_items: si.rows,
digests: dg.rows, digest_images: dgi.rows,
};
fs.writeFileSync(path.join(staging, 'data.json'), JSON.stringify(payload));
const files = await storage.downloadAll(path.join(staging, 'uploads'));
@@ -7369,6 +7380,7 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
await client.query('DELETE FROM doc_articles');
await client.query('DELETE FROM doc_sections');
await client.query('DELETE FROM sidebar_items');
await client.query('DELETE FROM digest_images');
await client.query('DELETE FROM users');
await client.query('DELETE FROM branches');
for (const x of ndata.branches) {
@@ -7446,6 +7458,18 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
[x.id, x.article_id, x.name, x.path, x.size_bytes, x.mime, x.uploaded_by, x.created_at || null]
);
}
for (const x of (ndata.digests || [])) {
await client.query(
'INSERT INTO digests (id, title, body, author_id, is_pinned, preview_path, post_at, hide_at, created_at, updated_at) VALUES ($1, $2, $3, (SELECT id FROM users WHERE id = $4), $5, $6, $7, $8, $9, $10)',
[x.id, x.title, x.body, x.author_id, x.is_pinned !== false, x.preview_path, x.post_at || null, x.hide_at || null, x.created_at || null, x.updated_at || null]
);
}
for (const x of (ndata.digest_images || [])) {
await client.query(
'INSERT INTO digest_images (id, digest_id, path, name, size, created_at) VALUES ($1, (SELECT id FROM digests WHERE id = $2), $3, $4, $5, $6)',
[x.id, x.digest_id, x.path, x.name || '', x.size || 0, x.created_at || null]
);
}
for (const x of ndata.user_branches) {
await client.query(
'INSERT INTO user_branches (user_id, branch_id) VALUES ($1,$2)',