Update server and markdown renderer

This commit is contained in:
dev
2026-10-31 11:14:27 +03:00
parent 00b7e96260
commit a4644b473b
2 changed files with 23 additions and 4 deletions
+14 -1
View File
@@ -30,7 +30,7 @@
text = text.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (m, alt, src) => {
const url = safeUrl(src.replace(/&/g, '&'));
if (!url) return alt;
return `<img src="${esc(thumbUrl(url))}" data-img="${esc(url)}" alt="${esc(alt)}" loading="lazy" onerror="this.onerror=null;this.src='${esc(url)}'">`;
return `<img src="${esc(thumbUrl(url))}" data-img="${esc(url)}" alt="${esc(alt)}" loading="lazy">`;
});
text = text.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (m, label, href) => {
const url = safeUrl(href.replace(/&amp;/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 };
})();
+9 -3
View File
@@ -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);
});