Вынести удаление файлов в lib/files.js
- lib/files.js: THUMBS_DIR, thumbFileFor, thumbUnlinkFor, safeUnlink и unlinkMedia (файл + миниатюра) - server.js: хелперы импортируются вместо локальных копий, unlinkQuizMedia заменён на unlinkMedia - комментарий /* ignore */ убран: catch внутри thumbUnlinkFor
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const storage = require('../storage');
|
||||||
|
|
||||||
|
const UPLOADS_DIR = path.join(__dirname, '..', 'uploads');
|
||||||
|
const THUMBS_DIR = path.join(UPLOADS_DIR, '.thumbs');
|
||||||
|
|
||||||
|
function thumbFileFor(fp) {
|
||||||
|
const base = path.basename(fp).replace(/\.[^.]+$/, '') + '.webp';
|
||||||
|
return path.join(THUMBS_DIR, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
function thumbUnlinkFor(key) {
|
||||||
|
const tp = thumbFileFor(key || '');
|
||||||
|
try { if (fs.existsSync(tp)) fs.unlinkSync(tp); } catch {}
|
||||||
|
}
|
||||||
|
|
||||||
|
function safeUnlink(relPath) {
|
||||||
|
const key = storage.keyFromPath(relPath);
|
||||||
|
if (!key) return;
|
||||||
|
storage.unlinkLocalOnly(key);
|
||||||
|
if (!storage.isRemote()) return;
|
||||||
|
storage.del(key).catch(err => console.error('Upload delete failed:', key, err.message));
|
||||||
|
}
|
||||||
|
|
||||||
|
function unlinkMedia(list) {
|
||||||
|
for (const p of list || []) {
|
||||||
|
if (!p) continue;
|
||||||
|
safeUnlink(p);
|
||||||
|
thumbUnlinkFor(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
UPLOADS_DIR,
|
||||||
|
THUMBS_DIR,
|
||||||
|
thumbFileFor,
|
||||||
|
thumbUnlinkFor,
|
||||||
|
safeUnlink,
|
||||||
|
unlinkMedia,
|
||||||
|
};
|
||||||
@@ -944,7 +944,7 @@ app.use((req, res, next) => {
|
|||||||
});
|
});
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
const THUMBS_DIR = path.join(UPLOADS_DIR, '.thumbs');
|
const { THUMBS_DIR, thumbFileFor, thumbUnlinkFor, safeUnlink, unlinkMedia } = require('./lib/files');
|
||||||
const ORIGINALS_DIR = path.join(UPLOADS_DIR, '.originals');
|
const ORIGINALS_DIR = path.join(UPLOADS_DIR, '.originals');
|
||||||
const THUMB_WIDTH = 480;
|
const THUMB_WIDTH = 480;
|
||||||
let sharp = null;
|
let sharp = null;
|
||||||
@@ -955,16 +955,6 @@ if (sharp) {
|
|||||||
try { fs.mkdirSync(ORIGINALS_DIR, { recursive: true }); } catch {}
|
try { fs.mkdirSync(ORIGINALS_DIR, { recursive: true }); } catch {}
|
||||||
try { fs.mkdirSync(storage.cacheDir, { recursive: true }); } catch {}
|
try { fs.mkdirSync(storage.cacheDir, { recursive: true }); } catch {}
|
||||||
|
|
||||||
function thumbFileFor(fp) {
|
|
||||||
const base = path.basename(fp).replace(/\.[^.]+$/, '') + '.webp';
|
|
||||||
return path.join(THUMBS_DIR, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
function thumbUnlinkFor(key) {
|
|
||||||
const tp = thumbFileFor(key || '');
|
|
||||||
try { if (fs.existsSync(tp)) fs.unlinkSync(tp); } catch {}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function sendImageThumb(res, key) {
|
async function sendImageThumb(res, key) {
|
||||||
const tp = thumbFileFor(key);
|
const tp = thumbFileFor(key);
|
||||||
try {
|
try {
|
||||||
@@ -1835,14 +1825,6 @@ async function purgeScheduledDeletions() {
|
|||||||
return { entries: erefs.rowCount, groups: gcount, chat_threads: ccount, short_messages: scount, feedbacks: fbcount, farewell_messages: fwcount, students: stcount, broken_images: bicount, calendar_events: calCount, quizzes: qcount };
|
return { entries: erefs.rowCount, groups: gcount, chat_threads: ccount, short_messages: scount, feedbacks: fbcount, farewell_messages: fwcount, students: stcount, broken_images: bicount, calendar_events: calCount, quizzes: qcount };
|
||||||
}
|
}
|
||||||
|
|
||||||
function safeUnlink(relPath) {
|
|
||||||
const key = storage.keyFromPath(relPath);
|
|
||||||
if (!key) return;
|
|
||||||
storage.unlinkLocalOnly(key);
|
|
||||||
if (!storage.isRemote()) return;
|
|
||||||
storage.del(key).catch(err => console.error('Upload delete failed:', key, err.message));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function hardDeleteChatThread(threadId) {
|
async function hardDeleteChatThread(threadId) {
|
||||||
const { rows } = await pool.query(
|
const { rows } = await pool.query(
|
||||||
`SELECT a.path FROM chat_attachments a
|
`SELECT a.path FROM chat_attachments a
|
||||||
@@ -15345,14 +15327,6 @@ const quizMediaUpload = multer({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
function unlinkQuizMedia(list) {
|
|
||||||
for (const p of list || []) {
|
|
||||||
if (!p) continue;
|
|
||||||
Promise.resolve(safeUnlink(p)).catch(() => {});
|
|
||||||
try { thumbUnlinkFor(p); } catch (e) { /* ignore */ }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
app.get('/api/quizzes/files/:name', fileLimiter, async (req, res) => {
|
app.get('/api/quizzes/files/:name', fileLimiter, async (req, res) => {
|
||||||
let user = null;
|
let user = null;
|
||||||
try {
|
try {
|
||||||
@@ -15448,7 +15422,7 @@ app.put('/api/quizzes/assets/:id', requireAuth, async (req, res) => {
|
|||||||
app.delete('/api/quizzes/assets/:id', requireAuth, async (req, res) => {
|
app.delete('/api/quizzes/assets/:id', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const removed = await deleteQuizAsset(pool, reqInt(req.params.id), req.user);
|
const removed = await deleteQuizAsset(pool, reqInt(req.params.id), req.user);
|
||||||
unlinkQuizMedia([removed.path]);
|
unlinkMedia([removed.path]);
|
||||||
await logAudit(req, 'quiz.asset.delete', { path: removed.path });
|
await logAudit(req, 'quiz.asset.delete', { path: removed.path });
|
||||||
res.json({ ok: true });
|
res.json({ ok: true });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -15468,7 +15442,7 @@ app.get('/api/quizzes/:id', requireAuth, async (req, res) => {
|
|||||||
app.put('/api/quizzes/:id', requireAuth, async (req, res) => {
|
app.put('/api/quizzes/:id', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const result = await saveQuiz(pool, reqInt(req.params.id), req.user, req.body || {});
|
const result = await saveQuiz(pool, reqInt(req.params.id), req.user, req.body || {});
|
||||||
unlinkQuizMedia(result.removed);
|
unlinkMedia(result.removed);
|
||||||
await logAudit(req, 'quiz.update', {
|
await logAudit(req, 'quiz.update', {
|
||||||
id: result.quiz.id,
|
id: result.quiz.id,
|
||||||
kind: result.quiz.kind,
|
kind: result.quiz.kind,
|
||||||
@@ -15508,7 +15482,7 @@ app.put('/api/quizzes/:id/restore', requireAuth, async (req, res) => {
|
|||||||
app.delete('/api/quizzes/:id/permanent', requireAuth, async (req, res) => {
|
app.delete('/api/quizzes/:id/permanent', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const result = await deleteQuizPermanent(pool, reqInt(req.params.id), req.user);
|
const result = await deleteQuizPermanent(pool, reqInt(req.params.id), req.user);
|
||||||
unlinkQuizMedia(result.paths);
|
unlinkMedia(result.paths);
|
||||||
await logAudit(req, 'quiz.permanent-delete', { id: reqInt(req.params.id) });
|
await logAudit(req, 'quiz.permanent-delete', { id: reqInt(req.params.id) });
|
||||||
broadcastQuizzesChanged();
|
broadcastQuizzesChanged();
|
||||||
res.json({ ok: true });
|
res.json({ ok: true });
|
||||||
@@ -15528,7 +15502,7 @@ app.post('/api/quizzes/:id/cover', requireAuth, (req, res) => {
|
|||||||
try {
|
try {
|
||||||
await convertPhoto(req.file);
|
await convertPhoto(req.file);
|
||||||
const result = await setQuizCover(pool, reqInt(req.params.id), req.user, `/uploads/${req.file.filename}`);
|
const result = await setQuizCover(pool, reqInt(req.params.id), req.user, `/uploads/${req.file.filename}`);
|
||||||
unlinkQuizMedia([result.old]);
|
unlinkMedia([result.old]);
|
||||||
await logAudit(req, 'quiz.cover', { id: reqInt(req.params.id), path: result.cover_path });
|
await logAudit(req, 'quiz.cover', { id: reqInt(req.params.id), path: result.cover_path });
|
||||||
broadcastQuizzesChanged();
|
broadcastQuizzesChanged();
|
||||||
res.json({ item: { path: result.cover_path } });
|
res.json({ item: { path: result.cover_path } });
|
||||||
@@ -15542,7 +15516,7 @@ app.post('/api/quizzes/:id/cover', requireAuth, (req, res) => {
|
|||||||
app.delete('/api/quizzes/:id/cover', requireAuth, async (req, res) => {
|
app.delete('/api/quizzes/:id/cover', requireAuth, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const result = await setQuizCover(pool, reqInt(req.params.id), req.user, null);
|
const result = await setQuizCover(pool, reqInt(req.params.id), req.user, null);
|
||||||
unlinkQuizMedia([result.old]);
|
unlinkMedia([result.old]);
|
||||||
await logAudit(req, 'quiz.cover.delete', { id: reqInt(req.params.id) });
|
await logAudit(req, 'quiz.cover.delete', { id: reqInt(req.params.id) });
|
||||||
broadcastQuizzesChanged();
|
broadcastQuizzesChanged();
|
||||||
res.json({ ok: true });
|
res.json({ ok: true });
|
||||||
@@ -15691,7 +15665,7 @@ app.get('/api/public-quizzes/files/:name', fileLimiter, async (req, res) => {
|
|||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
cleanupQuizAssets(pool, 24)
|
cleanupQuizAssets(pool, 24)
|
||||||
.then(r => unlinkQuizMedia(r.paths))
|
.then(r => unlinkMedia(r.paths))
|
||||||
.catch(err => console.error('Quiz assets cleanup:', err.message));
|
.catch(err => console.error('Quiz assets cleanup:', err.message));
|
||||||
}, 6 * 60 * 60 * 1000).unref();
|
}, 6 * 60 * 60 * 1000).unref();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user