feat(settings): Настраиваемый system favicon
Роут /favicon.ico берёт путь из настройки system_favicon; настройка входит в /api/public-settings, PUT /api/settings валидируется через isSafeUploadPath и удаляет файл и thumb при смене/очистке; sweepOrphanedUploads и BROKEN_IMAGE_SOURCES учитывают favicon; UI раздела настроек
This commit is contained in:
@@ -878,6 +878,22 @@ app.get('/uploads/*', async (req, res) => {
|
||||
if (!res.headersSent) res.status(404).end();
|
||||
}
|
||||
});
|
||||
app.get('/favicon.ico', async (req, res) => {
|
||||
const p = await getSetting('system_favicon', '');
|
||||
const key = p && isSafeUploadPath(p) ? storage.keyFromPath(p) : null;
|
||||
if (!key) return res.status(404).end();
|
||||
try {
|
||||
const buf = await storage.getBuffer(key);
|
||||
if (!buf) return res.status(404).end();
|
||||
res.type(FAVICON_MIME[(path.extname(p) || '').toLowerCase()] || 'image/x-icon');
|
||||
res.setHeader('Cache-Control', 'public, max-age=3600');
|
||||
res.send(buf);
|
||||
} catch (e) {
|
||||
console.error('Favicon serve failed:', key, e.message);
|
||||
if (!res.headersSent) res.status(404).end();
|
||||
}
|
||||
});
|
||||
|
||||
app.use('/vendor', express.static(path.join(__dirname, 'public', 'vendor'), { maxAge: '30d' }));
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
@@ -1705,7 +1721,7 @@ async function sweepOrphanedUploads() {
|
||||
pool.query(`SELECT after_path AS p FROM photo_jobs WHERE status = 'done' AND applied = false AND after_path IS NOT NULL`),
|
||||
pool.query('SELECT photo_path AS p FROM modules WHERE photo_path IS NOT NULL'),
|
||||
pool.query('SELECT photo_path AS p FROM student_photos'),
|
||||
pool.query(`SELECT value AS p FROM settings WHERE key = 'system_logo' AND value IS NOT NULL AND value <> ''`),
|
||||
pool.query(`SELECT value AS p FROM settings WHERE key IN ('system_logo', 'system_favicon') AND value IS NOT NULL AND value <> ''`),
|
||||
pool.query('SELECT photo_path AS p FROM students WHERE photo_path IS NOT NULL'),
|
||||
pool.query('SELECT avatar AS p FROM users WHERE avatar IS NOT NULL'),
|
||||
pool.query('SELECT cover_path AS p FROM groups WHERE cover_path IS NOT NULL'),
|
||||
@@ -1760,6 +1776,7 @@ const BROKEN_IMAGE_SOURCES = [
|
||||
{ key: 'photo_jobs.before_path', table: 'photo_jobs', column: 'before_path', idColumn: 'id', kind: 'column', label: 'Фото до обработки' },
|
||||
{ key: 'photo_jobs.after_path', table: 'photo_jobs', column: 'after_path', idColumn: 'id', kind: 'column', label: 'Фото после обработки' },
|
||||
{ key: 'settings.system_logo', table: 'settings', column: 'value', idColumn: null, kind: 'setting', label: 'Логотип системы', where: `key = 'system_logo'` },
|
||||
{ key: 'settings.system_favicon', table: 'settings', column: 'value', idColumn: null, kind: 'setting', label: 'Favicon системы', where: `key = 'system_favicon'` },
|
||||
];
|
||||
|
||||
function brokenImageSource(key) {
|
||||
@@ -5524,8 +5541,8 @@ app.get('/api/settings', requireAdmin, async (_, res) => {
|
||||
|
||||
app.get('/api/public-settings', apiLimiter, async (_, res) => {
|
||||
const out = await cacheWrap('public-settings', PUBLIC_TTL_MS, async () => {
|
||||
const keys = ['system_name', 'system_logo', 'footer_left', 'footer_right', 'share_show_student_message', 'share_show_entry_date', 'share_show_student_names', 'share_show_group_photos', 'cookie_notice_text', 'spam_interval_min', 'photo_capture_resolution', 'photo_capture_quality', 'photo_enhance_engine', 'camera_enabled', 'photo_ai_face_mode', 'photo_ai_face_model', 'photo_ai_device_pref', 'timezone', 'time_format'];
|
||||
const defaults = { system_name: 'WhatIDo', system_logo: '', spam_interval_min: '30', photo_capture_resolution: '640x480', photo_capture_quality: '0.92', photo_enhance_engine: 'auto', camera_enabled: 'true', photo_ai_face_mode: PHOTO_AI_DEFAULT_FACE_MODE, photo_ai_face_model: PHOTO_AI_FACE_MODEL, photo_ai_device_pref: 'auto', timezone: DEFAULT_TIMEZONE, time_format: '24h' };
|
||||
const keys = ['system_name', 'system_logo', 'system_favicon', 'footer_left', 'footer_right', 'share_show_student_message', 'share_show_entry_date', 'share_show_student_names', 'share_show_group_photos', 'cookie_notice_text', 'spam_interval_min', 'photo_capture_resolution', 'photo_capture_quality', 'photo_enhance_engine', 'camera_enabled', 'photo_ai_face_mode', 'photo_ai_face_model', 'photo_ai_device_pref', 'timezone', 'time_format'];
|
||||
const defaults = { system_name: 'WhatIDo', system_logo: '', system_favicon: '', spam_interval_min: '30', photo_capture_resolution: '640x480', photo_capture_quality: '0.92', photo_enhance_engine: 'auto', camera_enabled: 'true', photo_ai_face_mode: PHOTO_AI_DEFAULT_FACE_MODE, photo_ai_face_model: PHOTO_AI_FACE_MODEL, photo_ai_device_pref: 'auto', timezone: DEFAULT_TIMEZONE, time_format: '24h' };
|
||||
const result = {};
|
||||
for (const k of keys) result[k] = await getSetting(k, defaults[k] || '');
|
||||
const rm = /^(\d{2,5})x(\d{2,5})$/.exec(result.photo_capture_resolution);
|
||||
@@ -5592,6 +5609,9 @@ app.put('/api/settings', requireAdmin, async (req, res) => {
|
||||
if (key === 'system_logo' && String(value) !== '' && !isSafeUploadPath(String(value))) {
|
||||
return res.status(400).json({ error: 'system_logo — некорректный путь' });
|
||||
}
|
||||
if (key === 'system_favicon' && String(value) !== '' && !isSafeUploadPath(String(value))) {
|
||||
return res.status(400).json({ error: 'system_favicon — некорректный путь' });
|
||||
}
|
||||
if (key === 'notify_retention_days') {
|
||||
const n = parseInt(String(value), 10);
|
||||
if (!Number.isFinite(n) || n < 1 || n > 365) {
|
||||
@@ -5855,6 +5875,55 @@ app.delete('/api/settings/logo', requireAdmin, async (req, res) => {
|
||||
res.json({ ok: true, system_logo: '' });
|
||||
});
|
||||
|
||||
const FAVICON_MIME = { '.ico': 'image/x-icon', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp', '.bmp': 'image/bmp', '.avif': 'image/avif' };
|
||||
const faviconUpload = upload.single('favicon');
|
||||
app.post('/api/settings/favicon', requireAdmin, (req, res, next) => {
|
||||
faviconUpload(req, res, (err) => {
|
||||
if (err) {
|
||||
if (err.code === 'LIMIT_FILE_SIZE') return res.status(400).json({ error: FILE_TOO_LARGE_ERROR });
|
||||
if (err.message === 'Only images') return res.status(400).json({ error: 'Favicon: допустимы только изображения (ico, png, jpg, gif, webp, bmp, avif, heic, heif)' });
|
||||
if (err.message === 'Not allowed extension') return res.status(400).json({ error: 'Недопустимый тип файла' });
|
||||
return res.status(400).json({ error: 'Недопустимый файл' });
|
||||
}
|
||||
next();
|
||||
});
|
||||
}, async (req, res) => {
|
||||
if (!req.file) return res.status(400).json({ error: 'Файл обязателен' });
|
||||
const ext = (path.extname(req.file.originalname || '') || '').toLowerCase();
|
||||
if (!ALLOWED_IMAGE_EXT.has(ext)) {
|
||||
removeUpload(req.file);
|
||||
return res.status(400).json({ error: 'Favicon: допустимы только изображения (ico, png, jpg, gif, webp, bmp, avif, heic, heif, jfif)' });
|
||||
}
|
||||
try {
|
||||
if (['.heic', '.heif', '.jfif'].includes(ext)) await convertPhoto(req.file);
|
||||
const finalPath = `/uploads/${req.file.filename}`;
|
||||
const old = await getSetting('system_favicon', '');
|
||||
if (old && isSafeUploadPath(old) && old !== finalPath) safeUnlink(old);
|
||||
await pool.query(
|
||||
`INSERT INTO settings (key, value) VALUES ('system_favicon', $1)
|
||||
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value`,
|
||||
[finalPath]
|
||||
);
|
||||
await logAudit(req, 'settings.favicon.upload', { path: finalPath });
|
||||
invalidateSettings();
|
||||
res.json({ system_favicon: finalPath });
|
||||
} catch (e) {
|
||||
removeUpload(req.file);
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
app.delete('/api/settings/favicon', requireAdmin, async (req, res) => {
|
||||
const old = await getSetting('system_favicon', '');
|
||||
if (old) {
|
||||
await pool.query(`UPDATE settings SET value = '' WHERE key = 'system_favicon'`);
|
||||
if (isSafeUploadPath(old)) safeUnlink(old);
|
||||
}
|
||||
await logAudit(req, 'settings.favicon.remove', {});
|
||||
invalidateSettings();
|
||||
res.json({ ok: true, system_favicon: '' });
|
||||
});
|
||||
|
||||
app.get('/api/audit', requireAdmin, async (req, res) => {
|
||||
const limit = Math.min(parseInt(req.query.limit, 10) || 100, 1000);
|
||||
const offset = Math.max(parseInt(req.query.offset, 10) || 0, 0);
|
||||
|
||||
Reference in New Issue
Block a user