diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js index 3fd253410d..36ca7959e8 100644 --- a/src/controllers/uploads.js +++ b/src/controllers/uploads.js @@ -66,7 +66,7 @@ async function uploadAsImage(req, uploadedFile) { uid: req.uid, }); } - await file.isFileTypeAllowed(uploadedFile.path); + await image.isFileTypeAllowed(uploadedFile.path); let fileObj = await uploadsController.uploadFile(req.uid, uploadedFile); @@ -123,7 +123,7 @@ uploadsController.uploadThumb = async function (req, res, next) { if (!uploadedFile.type.match(/image./)) { throw new Error('[[error:invalid-file]]'); } - await file.isFileTypeAllowed(uploadedFile.path); + await image.isFileTypeAllowed(uploadedFile.path); await image.resizeImage({ path: uploadedFile.path, width: meta.config.topicThumbSize, diff --git a/src/file.js b/src/file.js index 7a9a366076..c8ccedb030 100644 --- a/src/file.js +++ b/src/file.js @@ -52,14 +52,8 @@ file.base64ToLocal = async function (imageData, uploadPath) { }; file.isFileTypeAllowed = async function (path) { - var plugins = require('./plugins'); - if (plugins.hasListeners('filter:file.isFileTypeAllowed')) { - return await plugins.fireHook('filter:file.isFileTypeAllowed', path); - } - const sharp = require('sharp'); - await sharp(path, { - failOnError: true, - }).metadata(); + winston.warn('[deprecated] file.isFileTypeAllowed is deprecated, use image.isFileTypeAllowed'); + await require('./image').isFileTypeAllowed(path); }; // https://stackoverflow.com/a/31205878/583363 diff --git a/src/image.js b/src/image.js index 803ce0960b..ef373b6b72 100644 --- a/src/image.js +++ b/src/image.js @@ -24,6 +24,21 @@ function requireSharp() { return sharp; } +image.isFileTypeAllowed = async function (path) { + const plugins = require('./plugins'); + // deprecated: remove in 1.14.0 + if (plugins.hasListeners('filter:file.isFileTypeAllowed')) { + return await plugins.fireHook('filter:file.isFileTypeAllowed', path); + } + if (plugins.hasListeners('filter:image.isFileTypeAllowed')) { + return await plugins.fireHook('filter:image.isFileTypeAllowed', path); + } + const sharp = require('sharp'); + await sharp(path, { + failOnError: true, + }).metadata(); +}; + image.resizeImage = async function (data) { if (plugins.hasListeners('filter:image.resize')) { await plugins.fireHook('filter:image.resize', { @@ -155,29 +170,20 @@ image.sizeFromBase64 = function (imageData) { return Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64').length; }; -image.uploadImage = function (filename, folder, image, callback) { +image.uploadImage = async function (filename, folder, imageData) { if (plugins.hasListeners('filter:uploadImage')) { - return plugins.fireHook('filter:uploadImage', { - image: image, - uid: image.uid, - }, callback); + return await plugins.fireHook('filter:uploadImage', { + image: imageData, + uid: imageData.uid, + }); } - - async.waterfall([ - function (next) { - file.isFileTypeAllowed(image.path, next); - }, - function (next) { - file.saveFileToLocal(filename, folder, image.path, next); - }, - function (upload, next) { - next(null, { - url: upload.url, - path: upload.path, - name: image.name, - }); - }, - ], callback); + await image.isFileTypeAllowed(imageData.path); + const upload = await file.saveFileToLocal(filename, folder, imageData.path); + return { + url: upload.url, + path: upload.path, + name: imageData.name, + }; }; require('./promisify')(image); diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js index 671588b7d2..32bf48c534 100644 --- a/src/plugins/hooks.js +++ b/src/plugins/hooks.js @@ -10,6 +10,7 @@ module.exports = function (Plugins) { 'filter:user.account': 'filter:account/profile.build', 'filter:user.account.edit': 'filter:account/edit.build', 'filter:notifications.get': 'filter:notifications.build', + 'filter:file.isFileTypeAllowed': 'filter:image.isFileTypeAllowed', }; Plugins.internals = { diff --git a/src/topics/thumb.js b/src/topics/thumb.js index 2d9be49856..33b750e7c0 100644 --- a/src/topics/thumb.js +++ b/src/topics/thumb.js @@ -44,7 +44,7 @@ module.exports = function (Topics) { await pipeToFileAsync(data.thumb, pathToUpload); - await file.isFileTypeAllowed(pathToUpload); + await image.isFileTypeAllowed(pathToUpload); await image.resizeImage({ path: pathToUpload, diff --git a/test/uploads.js b/test/uploads.js index 6d05046be1..44ea53cdcd 100644 --- a/test/uploads.js +++ b/test/uploads.js @@ -163,6 +163,13 @@ describe('Upload Controllers', function () { }); }); + it('should fail if file is not an image', function (done) { + image.isFileTypeAllowed(path.join(__dirname, '../test/files/notanimage.png'), function (err) { + assert.equal(err.message, 'Input file contains unsupported image format'); + done(); + }); + }); + it('should fail if file is not an image', function (done) { image.size(path.join(__dirname, '../test/files/notanimage.png'), function (err) { assert.equal(err.message, 'Input file contains unsupported image format');