diff --git a/src/file.js b/src/file.js index 35b57d9113..a936a4a43b 100644 --- a/src/file.js +++ b/src/file.js @@ -97,9 +97,8 @@ file.exists = function (path, callback) { if (err.code === 'ENOENT') { return callback(null, false); } - return callback(err); } - return callback(null, true); + callback(err, true); }); }; diff --git a/src/middleware/index.js b/src/middleware/index.js index 06b336d97f..99fc62b14b 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -1,7 +1,6 @@ 'use strict'; var async = require('async'); -var fs = require('fs'); var path = require('path'); var csrf = require('csurf'); var validator = require('validator'); @@ -13,6 +12,7 @@ var plugins = require('../plugins'); var meta = require('../meta'); var user = require('../user'); var groups = require('../groups'); +var file = require('../file'); var analytics = require('../analytics'); @@ -143,24 +143,25 @@ middleware.applyBlacklist = function (req, res, next) { }); }; -middleware.processTimeagoLocales = function (req, res) { +middleware.processTimeagoLocales = function (req, res, next) { var fallback = req.path.indexOf('-short') === -1 ? 'jquery.timeago.en.js' : 'jquery.timeago.en-short.js'; var localPath = path.join(__dirname, '../../public/vendor/jquery/timeago/locales', req.path); - var exists; - try { - exists = fs.accessSync(localPath, fs.F_OK | fs.R_OK); - } catch (e) { - exists = false; - } - - if (exists) { - res.status(200).sendFile(localPath, { - maxAge: req.app.enabled('cache') ? 5184000000 : 0, - }); - } else { - res.status(200).sendFile(path.join(__dirname, '../../public/vendor/jquery/timeago/locales', fallback), { - maxAge: req.app.enabled('cache') ? 5184000000 : 0, - }); - } + async.waterfall([ + function (next) { + file.exists(localPath, next); + }, + function (exists, next) { + if (exists) { + next(null, localPath); + } else { + next(null, path.join(__dirname, '../../public/vendor/jquery/timeago/locales', fallback)); + } + }, + function (path) { + res.status(200).sendFile(path, { + maxAge: req.app.enabled('cache') ? 5184000000 : 0, + }); + }, + ], next); }; diff --git a/test/controllers.js b/test/controllers.js index c353effbcb..24d71337c8 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -1287,6 +1287,25 @@ describe('Controllers', function () { }); }); + describe('timeago locales', function () { + it('should load timeago locale', function (done) { + request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.af.js', function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body.indexOf('Afrikaans') !== -1); + done(); + }); + }); + + it('should load timeago locale', function (done) { + request(nconf.get('url') + '/assets/vendor/jquery/timeago/locales/jquery.timeago.404.js', function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body.indexOf('English') !== -1); + done(); + }); + }); + }); after(function (done) { var analytics = require('../src/analytics');