fix: animated thumbs in album pages

This commit is contained in:
Bobby 2023-09-06 18:58:15 +07:00
parent b6b645df93
commit 3d9c400cab
No known key found for this signature in database
GPG Key ID: B2F45B6A3C9A8FCA
5 changed files with 18 additions and 9 deletions

View File

@ -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

View File

@ -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}`
}
}

View File

@ -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

View File

@ -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)

View File

@ -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