diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js index fec5607fe4..dd269b7a85 100644 --- a/src/controllers/uploads.js +++ b/src/controllers/uploads.js @@ -1,19 +1,18 @@ "use strict"; -var uploadsController = {}, +var fs = require('fs'); +var path = require('path'); +var async = require('async'); +var nconf = require('nconf'); +var validator = require('validator'); +var winston = require('winston'); - fs = require('fs'), - path = require('path'), - async = require('async'), - nconf = require('nconf'), - validator = require('validator'), - - meta = require('../meta'), - file = require('../file'), - plugins = require('../plugins'), - utils = require('../../public/src/utils'), - image = require('../image'); +var meta = require('../meta'); +var file = require('../file'); +var plugins = require('../plugins'); +var image = require('../image'); +var uploadsController = {}; uploadsController.upload = function(req, res, filesIterator, next) { var files = req.files.files; @@ -126,8 +125,8 @@ function uploadFile(uid, uploadedFile, callback) { } if (meta.config.hasOwnProperty('allowedFileExtensions')) { - var allowed = meta.config.allowedFileExtensions.split(',').filter(Boolean); - var extension = path.extname(uploadedFile.name).slice(1); + var allowed = file.allowedExtensions(); + var extension = path.extname(uploadedFile.name); if (allowed.length > 0 && allowed.indexOf(extension) === -1) { return callback(new Error('[[error:invalid-file-type, ' + allowed.join(', ') + ']]')); } diff --git a/src/file.js b/src/file.js index 01a40ee6bd..151a66f992 100644 --- a/src/file.js +++ b/src/file.js @@ -4,7 +4,6 @@ var fs = require('fs'), nconf = require('nconf'), path = require('path'), winston = require('winston'), - mime = require('mime'), jimp = require('jimp'), utils = require('../public/src/utils'); @@ -46,6 +45,28 @@ file.isFileTypeAllowed = function(path, callback) { }); }; +file.allowedExtensions = function() { + var meta = require('./meta'); + var allowedExtensions = (meta.config.allowedFileExtensions || '').trim(); + if (!allowedExtensions) { + return []; + } + allowedExtensions = allowedExtensions.split(','); + allowedExtensions = allowedExtensions.filter(Boolean).map(function(extension) { + extension = extension.trim(); + if (!extension.startsWith('.')) { + extension = '.' + extension; + } + return extension; + }); + + if (allowedExtensions.indexOf('.jpg') !== -1 && allowedExtensions.indexOf('.jpeg') === -1) { + allowedExtensions.push('.jpeg'); + } + + return allowedExtensions; +}; + file.exists = function(path, callback) { fs.stat(path, function(err, stat) { callback(!err && stat);