timeago locale test

This commit is contained in:
Barış Soner Uşaklı
2017-05-23 23:32:38 -04:00
parent 8205d6c897
commit 29135dfed3
3 changed files with 39 additions and 20 deletions

View File

@@ -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);
};