From 3d9c400cab9c14bf2d7ed0b70dc8909a39ccd367 Mon Sep 17 00:00:00 2001 From: Bobby Date: Wed, 6 Sep 2023 18:58:15 +0700 Subject: [PATCH] fix: animated thumbs in album pages --- controllers/albumsController.js | 7 ++++++- controllers/uploadController.js | 4 +--- controllers/utils/Constants.js | 3 ++- controllers/utilsController.js | 9 ++++++--- routes/album.js | 4 +++- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/controllers/albumsController.js b/controllers/albumsController.js index cf0a725..202ae72 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -498,10 +498,14 @@ self.get = async (req, res) => { const extname = utils.extname(file.name) if (utils.mayGenerateThumb(extname)) { - file.thumb = `${config.domain}/thumbs/${file.name.slice(0, -extname.length)}.png` + let thumbext = '.png' + if (utils.isAnimatedThumb(extname)) thumbext = '.gif' + file.thumb = `${config.domain}/thumbs/${file.name.slice(0, -extname.length)}${thumbext}` + /* // TODO: Upstream's API response is no longer identical to this. if (req.locals.upstreamCompat) { file.thumbSquare = file.thumb } + */ } } @@ -519,6 +523,7 @@ self.getUpstreamCompat = async (req, res) => { // If requested via /api/album/:identifier, // map to .get() with chibisafe/upstream compatibility // This API is known to be used in Pitu/Magane + // TODO: Upstream's API response is no longer identical to this, please fix. req.locals.upstreamCompat = true res._json = res.json diff --git a/controllers/uploadController.js b/controllers/uploadController.js index dd1ecb1..e88bda1 100644 --- a/controllers/uploadController.js +++ b/controllers/uploadController.js @@ -1810,9 +1810,7 @@ self.list = async (req, res) => { file.extname = utils.extname(file.name) if (utils.mayGenerateThumb(file.extname)) { let thumbext = '.png' - if (file.extname === '.gif' && config.uploads.generateThumbs.animated) { - thumbext = '.gif' - } + if (utils.isAnimatedThumb(file.extname)) thumbext = '.gif' file.thumb = `thumbs/${file.name.slice(0, -file.extname.length)}${thumbext}` } } diff --git a/controllers/utils/Constants.js b/controllers/utils/Constants.js index 2f5e83e..a4e4925 100644 --- a/controllers/utils/Constants.js +++ b/controllers/utils/Constants.js @@ -1,7 +1,8 @@ const self = { IMAGE_EXTS: ['.gif', '.jpeg', '.jpg', '.png', '.svg', '.tif', '.tiff', '.webp'], VIDEO_EXTS: ['.3g2', '.3gp', '.asf', '.avchd', '.avi', '.divx', '.evo', '.flv', '.h264', '.h265', '.hevc', '.m2p', '.m2ts', '.m4v', '.mk3d', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.mxf', '.ogg', '.ogv', '.ps', '.qt', '.rmvb', '.ts', '.vob', '.webm', '.wmv'], - AUDIO_EXTS: ['.flac', '.mp3', '.wav', '.wma'] + AUDIO_EXTS: ['.flac', '.mp3', '.wav', '.wma'], + ANIMATED_EXTS: ['.gif'] // must also exists in either IMAGE_EXTS or VIDEO_EXTS for thumbnails generation } module.exports = self diff --git a/controllers/utilsController.js b/controllers/utilsController.js index fd541b4..188cd33 100644 --- a/controllers/utilsController.js +++ b/controllers/utilsController.js @@ -251,6 +251,11 @@ self.mayGenerateThumb = extname => { (config.uploads.generateThumbs.video && Constants.VIDEO_EXTS.includes(extname)) } +self.isAnimatedThumb = extname => { + extname = extname.toLowerCase() + return (config.uploads.generateThumbs.animated && Constants.ANIMATED_EXTS.includes(extname)) +} + // Expand if necessary (should be case-insensitive) const extPreserves = [ /\.tar\.\w+/i // tarballs @@ -401,9 +406,7 @@ self.generateThumbs = async (name, extname, force) => { extname = extname.toLowerCase() const thumbname = name.slice(0, -extname.length) let thumbext = '.png' - if (extname === '.gif' && config.uploads.generateThumbs.animated) { - thumbext = '.gif' - } + if (self.isAnimatedThumb(extname)) thumbext = '.gif' const thumbfile = path.join(paths.thumbs, thumbname + thumbext) diff --git a/routes/album.js b/routes/album.js index 3733c53..4807e39 100644 --- a/routes/album.js +++ b/routes/album.js @@ -59,7 +59,9 @@ routes.get('/a/:identifier', async (req, res) => { file.extname = path.extname(file.name) if (utils.mayGenerateThumb(file.extname)) { - file.thumb = `thumbs/${file.name.slice(0, -file.extname.length)}.png` + let thumbext = '.png' + if (utils.isAnimatedThumb(file.extname)) thumbext = '.gif' + file.thumb = `thumbs/${file.name.slice(0, -file.extname.length)}${thumbext}` // If thumbnail for album is still not set, set it to current file's full URL. // A potential improvement would be to let the user set a specific image as an album cover. if (!album.thumb) album.thumb = file.name