From a4644b473b09100b17f2d0f516274b1fead4bcef Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 31 Oct 2026 11:14:27 +0300 Subject: [PATCH] Update server and markdown renderer --- public/js/md.js | 15 ++++++++++++++- server.js | 12 +++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/public/js/md.js b/public/js/md.js index cd1e075..a89a3af 100644 --- a/public/js/md.js +++ b/public/js/md.js @@ -30,7 +30,7 @@ text = text.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (m, alt, src) => { const url = safeUrl(src.replace(/&/g, '&')); if (!url) return alt; - return `${esc(alt)}`; + return `${esc(alt)}`; }); text = text.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (m, label, href) => { const url = safeUrl(href.replace(/&/g, '&')); @@ -188,5 +188,18 @@ return out.join('\n'); } + let fallbackBound = false; + function bindImageFallbacks() { + if (fallbackBound || typeof document === 'undefined') return; + fallbackBound = true; + document.addEventListener('error', (e) => { + const img = e.target; + if (!(img instanceof HTMLImageElement) || !img.hasAttribute('data-img')) return; + const full = img.getAttribute('data-img'); + if (img.getAttribute('src') !== full) img.setAttribute('src', full); + }, true); + } + bindImageFallbacks(); + window.WhatIDoMD = { render, inline, esc, thumbUrl, safeUrl }; })(); diff --git a/server.js b/server.js index 8298016..63f0dcc 100644 --- a/server.js +++ b/server.js @@ -868,13 +868,19 @@ function thumbUnlinkFor(key) { } async function sendImageThumb(res, key) { + const tp = thumbFileFor(key); + try { + if (fs.existsSync(tp)) { + res.setHeader('Cache-Control', 'public, max-age=31536000, immutable'); + return res.sendFile(tp); + } + } catch {} const local = await storage.localize(key); if (!local) return res.status(404).end(); if (!sharp) { res.setHeader('Cache-Control', 'public, max-age=3600'); return res.sendFile(local); } - const tp = thumbFileFor(key); try { if (!fs.existsSync(tp)) { const tmp = tp + '.' + crypto.randomBytes(4).toString('hex') + '.tmp'; @@ -1761,7 +1767,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: groupCovers }, { rows: originals }, { rows: jobBefore }, { rows: chatFiles }, { rows: bmarkPhotos }, { rows: faceRefs }, { rows: docPhotos }] = 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 }] = 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'), @@ -1782,7 +1788,7 @@ async function sweepOrphanedUploads() { pool.query('SELECT path AS p FROM doc_images'), ]); const refs = new Set(); - [...photos, ...files, ...gphotos, ...gporiginals, ...ephotos, ...pendingJobs, ...mphotos, ...sphotos, ...logos, ...studentAvatars, ...groupCovers, ...originals, ...jobBefore, ...chatFiles, ...bmarkPhotos, ...faceRefs, ...docPhotos].forEach(r => { + [...photos, ...files, ...gphotos, ...gporiginals, ...ephotos, ...pendingJobs, ...mphotos, ...sphotos, ...logos, ...studentAvatars, ...userAvatars, ...groupCovers, ...originals, ...jobBefore, ...chatFiles, ...bmarkPhotos, ...faceRefs, ...docPhotos].forEach(r => { const key = storage.keyFromPath(r.p); if (key) refs.add(key); });