0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Moved validation utils into validation/upload

- These two functions have no dependencies and are only used in valiation/upload
- Co-locating the code makes it easier to move
- Exported them with a new module.exports._test pattern - we'll see about whether this is a good idea
- This is one of many similar changes needed to make it easier to refactor to the existing setup
This commit is contained in:
Hannah Wolfe 2020-04-22 07:21:41 +01:00
parent d85c634669
commit 1a4506dcf0
3 changed files with 31 additions and 27 deletions

View file

@ -2,7 +2,20 @@ const path = require('path');
const errors = require('@tryghost/errors');
const {i18n} = require('../../../../lib/common');
const config = require('../../../../config');
const localUtils = require('../../utils');
const checkFileExists = (fileData) => {
return !!(fileData.mimetype && fileData.path);
};
const checkFileIsValid = (fileData, types, extensions) => {
const type = fileData.mimetype;
if (types.includes(type) && extensions.includes(fileData.ext)) {
return true;
}
return false;
};
module.exports = function upload(options) {
const type = options.type;
@ -17,7 +30,7 @@ module.exports = function upload(options) {
req.file.type = req.file.mimetype;
// Check if a file was provided
if (!localUtils.checkFileExists(req.file)) {
if (!checkFileExists(req.file)) {
return next(new errors.ValidationError({
message: i18n.t(`errors.api.${type}.missingFile`)
}));
@ -26,7 +39,7 @@ module.exports = function upload(options) {
req.file.ext = path.extname(req.file.name).toLowerCase();
// Check if the file is valid
if (!localUtils.checkFileIsValid(req.file, contentTypes, extensions)) {
if (!checkFileIsValid(req.file, contentTypes, extensions)) {
return next(new errors.UnsupportedMediaTypeError({
message: i18n.t(`errors.api.${type}.invalidFile`, {extensions: extensions})
}));
@ -35,3 +48,8 @@ module.exports = function upload(options) {
next();
};
};
module.exports._test = {
checkFileExists,
checkFileIsValid
};

View file

@ -29,17 +29,3 @@ module.exports.removeOpenRedirectFromUrl = function removeOpenRedirectFromUrl(ur
(parsedUrl.hash || '')
);
};
module.exports.checkFileExists = function checkFileExists(fileData) {
return !!(fileData.mimetype && fileData.path);
};
module.exports.checkFileIsValid = function checkFileIsValid(fileData, types, extensions) {
const type = fileData.mimetype;
if (types.includes(type) && extensions.includes(fileData.ext)) {
return true;
}
return false;
};

View file

@ -1,30 +1,30 @@
var should = require('should'),
webUtils = require('../../../core/server/web/shared/utils');
const should = require('should');
const uploadValidation = require('../../../../../core/server/web/shared/middlewares/validation/upload')._test;
describe('web utils', function () {
describe('checkFileExists', function () {
it('should return true if file exists in input', function () {
webUtils.checkFileExists({mimetype: 'file', path: 'path'}).should.be.true();
uploadValidation.checkFileExists({mimetype: 'file', path: 'path'}).should.be.true();
});
it('should return false if file does not exist in input', function () {
webUtils.checkFileExists({}).should.be.false();
uploadValidation.checkFileExists({}).should.be.false();
});
it('should return false if file is incorrectly structured', function () {
webUtils.checkFileExists({type: 'file'}).should.be.false();
uploadValidation.checkFileExists({type: 'file'}).should.be.false();
});
});
describe('checkFileIsValid', function () {
it('returns true if file has valid extension and type', function () {
webUtils.checkFileIsValid({
uploadValidation.checkFileIsValid({
name: 'test.txt',
mimetype: 'text',
ext: '.txt'
}, ['text'], ['.txt']).should.be.true();
webUtils.checkFileIsValid({
uploadValidation.checkFileIsValid({
name: 'test.jpg',
mimetype: 'jpeg',
ext: '.jpg'
@ -32,12 +32,12 @@ describe('web utils', function () {
});
it('returns false if file has invalid extension', function () {
webUtils.checkFileIsValid({name: 'test.txt', mimetype: 'text'}, ['text'], ['.tar']).should.be.false();
webUtils.checkFileIsValid({name: 'test', mimetype: 'text'}, ['text'], ['.txt']).should.be.false();
uploadValidation.checkFileIsValid({name: 'test.txt', mimetype: 'text'}, ['text'], ['.tar']).should.be.false();
uploadValidation.checkFileIsValid({name: 'test', mimetype: 'text'}, ['text'], ['.txt']).should.be.false();
});
it('returns false if file has invalid type', function () {
webUtils.checkFileIsValid({name: 'test.txt', mimetype: 'text'}, ['archive'], ['.txt']).should.be.false();
uploadValidation.checkFileIsValid({name: 'test.txt', mimetype: 'text'}, ['archive'], ['.txt']).should.be.false();
});
});
});