Node.js based forum software built for the modern web
thumb tests
barisusakli04b07e292ced
diff --git a/src/controllers/uploads.js b/src/controllers/uploads.jsindex 923e3cf7d8..22e533a33d 100644--- a/src/controllers/uploads.js+++ b/src/controllers/uploads.js@@ -6,7 +6,6 @@ var async = require('async');var nconf = require('nconf');var validator = require('validator');var winston = require('winston');-var mime = require('mime');var meta = require('../meta');var file = require('../file');@@ -31,7 +30,7 @@ uploadsController.upload = function (req, res, filesIterator) {deleteTempFiles(files);if (err) {- return res.status(500).send(err.message);+ return res.status(500).json({path: req.path, error: err.message});}res.status(200).send(images);@@ -138,26 +137,24 @@ uploadsController.uploadThumb = function (req, res, next) {}uploadsController.upload(req, res, function (uploadedFile, next) {- file.isFileTypeAllowed(uploadedFile.path, function (err) {- if (err) {- return next(err);- }-- if (!uploadedFile.type.match(/image./)) {- return next(new Error('[[error:invalid-file]]'));- }-- var size = parseInt(meta.config.topicThumbSize, 10) || 120;- image.resizeImage({- path: uploadedFile.path,- extension: path.extname(uploadedFile.name),- width: size,- height: size- }, function (err) {- if (err) {- return next(err);+ async.waterfall([+ function (next) {+ if (!uploadedFile.type.match(/image./)) {+ return next(new Error('[[error:invalid-file]]'));}+ file.isFileTypeAllowed(uploadedFile.path, next);+ },+ function (next) {+ var size = parseInt(meta.config.topicThumbSize, 10) || 120;+ image.resizeImage({+ path: uploadedFile.path,+ extension: path.extname(uploadedFile.name),+ width: size,+ height: size+ }, next);+ },+ function (next) {if (plugins.hasListeners('filter:uploadImage')) {return plugins.fireHook('filter:uploadImage', {image: uploadedFile,@@ -166,8 +163,8 @@ uploadsController.uploadThumb = function (req, res, next) {}uploadFile(req.uid, uploadedFile, next);- });- });+ }+ ], next);}, next);};@@ -186,12 +183,14 @@ uploadsController.uploadGroupCover = function (uid, uploadedFile, callback) {}, callback);}- file.isFileTypeAllowed(uploadedFile.path, function (err) {- if (err) {- return callback(err);+ async.waterfall([+ function (next) {+ file.isFileTypeAllowed(uploadedFile.path, next);+ },+ function (next) {+ saveFileToLocal(uploadedFile, next);}- saveFileToLocal(uploadedFile, callback);- });+ ], callback);};function uploadFile(uid, uploadedFile, callback) {@@ -230,17 +229,18 @@ function saveFileToLocal(uploadedFile, callback) {filename = Date.now() + '-' + validator.escape(filename.replace(path.extname(uploadedFile.name) || '', '')).substr(0, 255) + extension;- file.saveFileToLocal(filename, 'files', uploadedFile.path, function (err, upload) {- if (err) {- return callback(err);+ async.waterfall([+ function (next) {+ file.saveFileToLocal(filename, 'files', uploadedFile.path, next);+ },+ function (upload, next) {+ next(null, {+ url: nconf.get('relative_path') + upload.url,+ path: upload.path,+ name: uploadedFile.name+ });}-- callback(null, {- url: nconf.get('relative_path') + upload.url,- path: upload.path,- name: uploadedFile.name- });- });+ ], callback);}function deleteTempFiles(files) {diff --git a/test/uploads.js b/test/uploads.jsindex f8a3738c8f..7ac1b595a2 100644--- a/test/uploads.js+++ b/test/uploads.js@@ -88,6 +88,20 @@ describe('Upload Controllers', function () {});});+ it('should resize and upload an image to a post', function (done) {+ var oldValue = meta.config.maximumImageWidth;+ meta.config.maximumImageWidth = 10;+ helpers.uploadFile(nconf.get('url') + '/api/post/upload', path.join(__dirname, '../test/files/test.png'), {cid: cid}, jar, csrf_token, function (err, res, body) {+ assert.ifError(err);+ assert.equal(res.statusCode, 200);+ assert(Array.isArray(body));+ assert(body[0].path);+ assert(body[0].url);+ meta.config.maximumImageWidth = oldValue;+ done();+ });+ });+it('should upload a file to a post', function (done) {meta.config.allowFileUploads = 1;@@ -101,6 +115,37 @@ describe('Upload Controllers', function () {});});+ it('should fail if topic thumbs are disabled', function (done) {+ helpers.uploadFile(nconf.get('url') + '/api/topic/thumb/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) {+ assert.ifError(err);+ assert.equal(res.statusCode, 500);+ assert.equal(body.error, '[[error:topic-thumbnails-are-disabled]]');+ done();+ });+ });++ it('should fail if file is not image', function (done) {+ meta.config.allowTopicsThumbnail = 1;+ helpers.uploadFile(nconf.get('url') + '/api/topic/thumb/upload', path.join(__dirname, '../test/files/503.html'), {}, jar, csrf_token, function (err, res, body) {+ assert.ifError(err);+ assert.equal(res.statusCode, 500);+ assert.equal(body.error, '[[error:invalid-file]]');+ done();+ });+ });++ it('should upload topic thumb', function (done) {+ meta.config.allowTopicsThumbnail = 1;+ helpers.uploadFile(nconf.get('url') + '/api/topic/thumb/upload', path.join(__dirname, '../test/files/test.png'), {}, jar, csrf_token, function (err, res, body) {+ assert.ifError(err);+ assert.equal(res.statusCode, 200);+ assert(Array.isArray(body));+ assert(body[0].path);+ assert(body[0].url);+ done();+ });+ });+});