From 32499fed36923f243da5c319cef0badd2432a823 Mon Sep 17 00:00:00 2001 From: Kibi Kelburton Date: Sat, 16 May 2026 20:36:11 +0200 Subject: [PATCH] #12 gif to webp convert --- src/comment_upload_handler.mjs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/comment_upload_handler.mjs b/src/comment_upload_handler.mjs index 8a9e797..cc350fe 100644 --- a/src/comment_upload_handler.mjs +++ b/src/comment_upload_handler.mjs @@ -235,7 +235,25 @@ export const handleCommentUpload = async (req, res) => { } } - const ext = cfg.mimes[actualMime] || 'bin'; + let ext = cfg.mimes[actualMime] || 'bin'; + + // Convert GIF → animated WebP to save disk space + if (actualMime === 'image/gif') { + const webpTmpPath = tmpPath.replace(/\.tmp$/, '.webp'); + try { + await queue.spawn('magick', [tmpPath, '-coalesce', '-quality', '80', webpTmpPath]); + // Replace the temp file with the converted one + await fs.unlink(tmpPath).catch(() => {}); + await fs.rename(webpTmpPath, tmpPath); + actualMime = 'image/webp'; + ext = 'webp'; + console.log(`[COMMENT_UPLOAD] Converted GIF → WebP: ${file.filename}`); + } catch (e) { + console.warn(`[COMMENT_UPLOAD] GIF→WebP conversion failed, keeping original:`, e.message); + await fs.unlink(webpTmpPath).catch(() => {}); + } + } + const filename = `${uuid}.${ext}`; // SHA-256 checksum