mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
feat: deprecate file.isFileTypeAllowed
use image.isFileTypeAllowed, this function was always meant for images
This commit is contained in:
@@ -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,
|
||||
|
||||
10
src/file.js
10
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
|
||||
|
||||
48
src/image.js
48
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);
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user