From e45d854c09cd34d5bc4086ee68f3d5026e127d88 Mon Sep 17 00:00:00 2001 From: Bobby Wibowo Date: Fri, 23 Aug 2019 16:49:53 +0700 Subject: [PATCH] Updated * Updated path resolving for upload folder option. This SHOULD now allow using an absolute path, even those outside of the lolisafe installation. * Added a config option at uploads -> generateThumbs, named placeholder. It's a string option that lets you set path of the placeholder image for files whose thumbnail could not be generated. It defaults to the old hard-coded path when set to falsy value. * Fixed thumbs script not using upload folder from config file. * Added filters for thumb generation error handling. This is used to ignore some common error messages, such as file formats not being supported. --- config.sample.js | 4 +++- controllers/albumsController.js | 2 +- controllers/utilsController.js | 18 ++++++++++++------ scripts/thumbs.js | 3 ++- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/config.sample.js b/config.sample.js index 2701a38..ffbf438 100644 --- a/config.sample.js +++ b/config.sample.js @@ -282,10 +282,12 @@ module.exports = { /* Thumbnails are only used in the dashboard and album's public pages. You need to install a separate binary called ffmpeg (https://ffmpeg.org/) for video thumbnails. + NOTE: Placeholder defaults to 'public/images/unavailable.png'. */ generateThumbs: { image: true, - video: false + video: false, + placeholder: null }, /* diff --git a/controllers/albumsController.js b/controllers/albumsController.js index ac3fdb1..79275cd 100644 --- a/controllers/albumsController.js +++ b/controllers/albumsController.js @@ -11,7 +11,7 @@ const albumsController = {} const maxTries = config.uploads.maxTries || 1 const homeDomain = config.homeDomain || config.domain -const uploadsDir = path.join(__dirname, '..', config.uploads.folder) +const uploadsDir = path.resolve(config.uploads.folder) const zipsDir = path.join(uploadsDir, 'zips') const zipMaxTotalSize = config.cloudflare.zipMaxTotalSize const zipMaxTotalSizeBytes = parseInt(config.cloudflare.zipMaxTotalSize) * 1000000 diff --git a/controllers/utilsController.js b/controllers/utilsController.js index 44df6f4..2c61787 100644 --- a/controllers/utilsController.js +++ b/controllers/utilsController.js @@ -36,9 +36,9 @@ const _stats = { } } -const uploadsDir = path.join(__dirname, '..', config.uploads.folder) +const uploadsDir = path.resolve(config.uploads.folder) const thumbsDir = path.join(uploadsDir, 'thumbs') -const thumbUnavailable = path.join(__dirname, '../public/images/unavailable.png') +const thumbPlaceholder = path.resolve(config.uploads.generateThumbs.placeholder || 'public/images/unavailable.png') const cloudflareAuth = config.cloudflare.apiKey && config.cloudflare.email && config.cloudflare.zoneId utilsController.imageExtensions = ['.webp', '.jpg', '.jpeg', '.gif', '.png', '.tiff', '.tif', '.svg'] @@ -250,11 +250,17 @@ utilsController.generateThumbs = (name, force) => { }) .then(resolve) .catch(error => { - console.error(`${name}: ${error.toString()}`) - fs.symlink(thumbUnavailable, thumbname, error => { - if (error) console.error(error) + const errorString = error.toString() + const tests = [ + /Error: Input file contains unsupported image format/, + /Error: ffprobe exited with code 1/ + ] + if (!tests.some(t => t.test(errorString))) + console.error(`${name}: ${errorString}`) + fs.symlink(thumbPlaceholder, thumbname, err => { + if (err) console.error(err) // We return true if we could make a symlink to the placeholder image - resolve(!error) + resolve(!err) }) }) }) diff --git a/scripts/thumbs.js b/scripts/thumbs.js index 08f325d..8946a8f 100644 --- a/scripts/thumbs.js +++ b/scripts/thumbs.js @@ -1,4 +1,5 @@ const { stripIndents } = require('./_utils') +const config = require('./../config') const fs = require('fs') const path = require('path') const utils = require('./../controllers/utilsController') @@ -58,7 +59,7 @@ thumbs.do = async () => { cfcache: 0 = do not clear cloudflare cache (default), 1 = clear cloudflare cache `)) - const uploadsDir = path.join(__dirname, '..', 'uploads') + const uploadsDir = path.resolve(config.uploads.folder) const thumbsDir = path.join(uploadsDir, 'thumbs') const _uploads = await thumbs.getFiles(uploadsDir)