feat: deprecate file.isFileTypeAllowed

use image.isFileTypeAllowed, this function was always meant for images
v1.18.x
Barış Soner Uşaklı 5 years ago
parent 2801ffa622
commit ffe3670ff5

@ -66,7 +66,7 @@ async function uploadAsImage(req, uploadedFile) {
uid: req.uid, uid: req.uid,
}); });
} }
await file.isFileTypeAllowed(uploadedFile.path); await image.isFileTypeAllowed(uploadedFile.path);
let fileObj = await uploadsController.uploadFile(req.uid, uploadedFile); let fileObj = await uploadsController.uploadFile(req.uid, uploadedFile);
@ -123,7 +123,7 @@ uploadsController.uploadThumb = async function (req, res, next) {
if (!uploadedFile.type.match(/image./)) { if (!uploadedFile.type.match(/image./)) {
throw new Error('[[error:invalid-file]]'); throw new Error('[[error:invalid-file]]');
} }
await file.isFileTypeAllowed(uploadedFile.path); await image.isFileTypeAllowed(uploadedFile.path);
await image.resizeImage({ await image.resizeImage({
path: uploadedFile.path, path: uploadedFile.path,
width: meta.config.topicThumbSize, width: meta.config.topicThumbSize,

@ -52,14 +52,8 @@ file.base64ToLocal = async function (imageData, uploadPath) {
}; };
file.isFileTypeAllowed = async function (path) { file.isFileTypeAllowed = async function (path) {
var plugins = require('./plugins'); winston.warn('[deprecated] file.isFileTypeAllowed is deprecated, use image.isFileTypeAllowed');
if (plugins.hasListeners('filter:file.isFileTypeAllowed')) { await require('./image').isFileTypeAllowed(path);
return await plugins.fireHook('filter:file.isFileTypeAllowed', path);
}
const sharp = require('sharp');
await sharp(path, {
failOnError: true,
}).metadata();
}; };
// https://stackoverflow.com/a/31205878/583363 // https://stackoverflow.com/a/31205878/583363

@ -24,6 +24,21 @@ function requireSharp() {
return sharp; 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) { image.resizeImage = async function (data) {
if (plugins.hasListeners('filter:image.resize')) { if (plugins.hasListeners('filter:image.resize')) {
await plugins.fireHook('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; 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')) { if (plugins.hasListeners('filter:uploadImage')) {
return plugins.fireHook('filter:uploadImage', { return await plugins.fireHook('filter:uploadImage', {
image: image, image: imageData,
uid: image.uid, uid: imageData.uid,
}, callback); });
} }
await image.isFileTypeAllowed(imageData.path);
async.waterfall([ const upload = await file.saveFileToLocal(filename, folder, imageData.path);
function (next) { return {
file.isFileTypeAllowed(image.path, next); url: upload.url,
}, path: upload.path,
function (next) { name: imageData.name,
file.saveFileToLocal(filename, folder, image.path, next); };
},
function (upload, next) {
next(null, {
url: upload.url,
path: upload.path,
name: image.name,
});
},
], callback);
}; };
require('./promisify')(image); require('./promisify')(image);

@ -10,6 +10,7 @@ module.exports = function (Plugins) {
'filter:user.account': 'filter:account/profile.build', 'filter:user.account': 'filter:account/profile.build',
'filter:user.account.edit': 'filter:account/edit.build', 'filter:user.account.edit': 'filter:account/edit.build',
'filter:notifications.get': 'filter:notifications.build', 'filter:notifications.get': 'filter:notifications.build',
'filter:file.isFileTypeAllowed': 'filter:image.isFileTypeAllowed',
}; };
Plugins.internals = { Plugins.internals = {

@ -44,7 +44,7 @@ module.exports = function (Topics) {
await pipeToFileAsync(data.thumb, pathToUpload); await pipeToFileAsync(data.thumb, pathToUpload);
await file.isFileTypeAllowed(pathToUpload); await image.isFileTypeAllowed(pathToUpload);
await image.resizeImage({ await image.resizeImage({
path: pathToUpload, path: pathToUpload,

@ -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) { it('should fail if file is not an image', function (done) {
image.size(path.join(__dirname, '../test/files/notanimage.png'), function (err) { image.size(path.join(__dirname, '../test/files/notanimage.png'), function (err) {
assert.equal(err.message, 'Input file contains unsupported image format'); assert.equal(err.message, 'Input file contains unsupported image format');

Loading…
Cancel
Save