v1.18.x
barisusakli
parent 714efd0d0e
commit ca294fc6ec

@ -23,14 +23,14 @@
var nconf = require('nconf'); var nconf = require('nconf');
nconf.argv().env('__'); nconf.argv().env('__');
var fs = require('fs'), var url = require('url'),
url = require('url'),
async = require('async'), async = require('async'),
semver = require('semver'), semver = require('semver'),
winston = require('winston'), winston = require('winston'),
colors = require('colors'), colors = require('colors'),
path = require('path'), path = require('path'),
pkg = require('./package.json'), pkg = require('./package.json'),
file = require('./src/file'),
utils = require('./public/src/utils.js'); utils = require('./public/src/utils.js');
global.env = process.env.NODE_ENV || 'production'; global.env = process.env.NODE_ENV || 'production';
@ -53,7 +53,7 @@ if (nconf.get('config')) {
configFile = path.resolve(__dirname, nconf.get('config')); configFile = path.resolve(__dirname, nconf.get('config'));
} }
var configExists = fs.existsSync(configFile); var configExists = file.existsSync(configFile);
loadConfig(); loadConfig();

@ -8,7 +8,7 @@ var nconf = require('nconf'),
async = require('async'), async = require('async'),
logrotate = require('logrotate-stream'), logrotate = require('logrotate-stream'),
file = require('./src/file'),
pkg = require('./package.json'); pkg = require('./package.json');
nconf.argv().env().file({ nconf.argv().env().file({
@ -243,7 +243,7 @@ Loader.notifyWorkers = function(msg, worker_pid) {
fs.open(path.join(__dirname, 'config.json'), 'r', function(err) { fs.open(path.join(__dirname, 'config.json'), 'r', function(err) {
if (!err) { if (!err) {
if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) { if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) {
if (fs.existsSync(pidFilePath)) { if (file.existsSync(pidFilePath)) {
try { try {
var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' }); var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' });
process.kill(pid, 0); process.kill(pid, 0);

@ -4,6 +4,7 @@ var uglifyjs = require('uglify-js'),
less = require('less'), less = require('less'),
async = require('async'), async = require('async'),
fs = require('fs'), fs = require('fs'),
file = require('./src/file'),
crypto = require('crypto'), crypto = require('crypto'),
utils = require('./public/src/utils'), utils = require('./public/src/utils'),
@ -14,16 +15,16 @@ var uglifyjs = require('uglify-js'),
/* Javascript */ /* Javascript */
Minifier.js.minify = function (scripts, minify, callback) { Minifier.js.minify = function (scripts, minify, callback) {
scripts = scripts.filter(function(file) { scripts = scripts.filter(function(file) {
return fs.existsSync(file) && file.endsWith('.js'); return file && file.endsWith('.js');
}); });
if (minify) { async.filter(scripts, file.exists, function(scripts) {
minifyScripts(scripts, function() { if (minify) {
callback.apply(this, arguments); minifyScripts(scripts, callback);
}); } else {
} else { concatenateScripts(scripts, callback);
concatenateScripts(scripts, callback); }
} });
}; };
process.on('message', function(payload) { process.on('message', function(payload) {

@ -264,11 +264,12 @@
var fs = require('fs'), var fs = require('fs'),
path = require('path'), path = require('path'),
winston = require('winston'), winston = require('winston'),
file = require('../../../src/file'),
meta = require('../../../src/meta'); meta = require('../../../src/meta');
language = language || meta.config.defaultLang || 'en_GB'; language = language || meta.config.defaultLang || 'en_GB';
if (!fs.existsSync(path.join(__dirname, '../../language', language))) { if (!file.existsSync(path.join(__dirname, '../../language', language))) {
winston.warn('[translator] Language \'' + meta.config.defaultLang + '\' not found. Defaulting to \'en_GB\''); winston.warn('[translator] Language \'' + meta.config.defaultLang + '\' not found. Defaulting to \'en_GB\'');
language = 'en_GB'; language = 'en_GB';
} }

@ -1,24 +1,24 @@
'use strict'; 'use strict';
var path = require('path'); var path = require('path');
var fs = require('fs'); var file = require('../../file');
var themesController = {}; var themesController = {};
themesController.get = function(req, res, next) { themesController.get = function(req, res, next) {
var themeDir = path.join(__dirname, '../../../node_modules/' + req.params.theme); var themeDir = path.join(__dirname, '../../../node_modules/' + req.params.theme);
fs.exists(themeDir, function(exists) { file.exists(themeDir, function(exists) {
if (exists) { if (!exists) {
var themeConfig = require(path.join(themeDir, 'theme.json')),
screenshotPath = path.join(themeDir, themeConfig.screenshot);
if (themeConfig.screenshot && fs.existsSync(screenshotPath)) {
res.sendFile(screenshotPath);
} else {
res.sendFile(path.join(__dirname, '../../../public/images/themes/default.png'));
}
} else {
return next(); return next();
} }
var themeConfig = require(path.join(themeDir, 'theme.json')),
screenshotPath = path.join(themeDir, themeConfig.screenshot);
if (themeConfig.screenshot && file.existsSync(screenshotPath)) {
res.sendFile(screenshotPath);
} else {
res.sendFile(path.join(__dirname, '../../../public/images/themes/default.png'));
}
}); });
}; };

@ -8,7 +8,6 @@ var fs = require('fs'),
Magic = mmmagic.Magic, Magic = mmmagic.Magic,
mime = require('mime'), mime = require('mime'),
meta = require('./meta'),
utils = require('../public/src/utils'); utils = require('../public/src/utils');
var file = {}; var file = {};
@ -63,6 +62,7 @@ file.isFileTypeAllowed = function(path, allowedExtensions, callback) {
}; };
file.allowedExtensions = function() { file.allowedExtensions = function() {
var meta = require('./meta');
var allowedExtensions = (meta.config.allowedFileExtensions || '').trim(); var allowedExtensions = (meta.config.allowedFileExtensions || '').trim();
if (!allowedExtensions) { if (!allowedExtensions) {
return []; return [];
@ -80,4 +80,21 @@ file.allowedExtensions = function() {
return allowedExtensions; return allowedExtensions;
}; };
file.exists = function(path, callback) {
fs.stat(path, function(err, stat) {
callback(!err && stat);
});
};
file.existsSync = function(path) {
var exists = false;
try {
exists = fs.statSync(path);
} catch(err) {
exists = false;
}
return !!exists;
};
module.exports = file; module.exports = file;

@ -10,6 +10,7 @@ var fs = require('fs'),
winston = require('winston'), winston = require('winston'),
util = require('util'), util = require('util'),
socketio = require('socket.io'), socketio = require('socket.io'),
file = require('./file'),
meta = require('./meta'), meta = require('./meta'),
morgan = require('morgan'); morgan = require('morgan');
@ -76,7 +77,7 @@ var opts = {
/* Open the streams to log to: either a path or stdout */ /* Open the streams to log to: either a path or stdout */
var stream; var stream;
if(value) { if(value) {
if(fs.existsSync(value)) { if(file.existsSync(value)) {
var stats = fs.statSync(value); var stats = fs.statSync(value);
if(stats) { if(stats) {
if(stats.isDirectory()) { if(stats.isDirectory()) {

@ -11,6 +11,7 @@ var winston = require('winston'),
plugins = require('../plugins'), plugins = require('../plugins'),
emitter = require('../emitter'), emitter = require('../emitter'),
db = require('../database'), db = require('../database'),
file = require('../file'),
utils = require('../../public/src/utils'); utils = require('../../public/src/utils');
module.exports = function(Meta) { module.exports = function(Meta) {
@ -149,24 +150,25 @@ module.exports = function(Meta) {
Meta.css.getFromFile = function(callback) { Meta.css.getFromFile = function(callback) {
var cachePath = path.join(__dirname, '../../public/stylesheet.css'), var cachePath = path.join(__dirname, '../../public/stylesheet.css'),
acpCachePath = path.join(__dirname, '../../public/admin.css'); acpCachePath = path.join(__dirname, '../../public/admin.css');
fs.exists(cachePath, function(exists) { file.exists(cachePath, function(exists) {
if (exists) { if (!exists) {
if (nconf.get('isPrimary') === 'true') {
winston.verbose('[meta/css] Reading stylesheets from file');
async.map([cachePath, acpCachePath], fs.readFile, function(err, files) {
Meta.css.cache = files[0];
Meta.css.acpCache = files[1];
emitter.emit('meta:css.compiled');
callback();
});
} else {
callback();
}
} else {
winston.warn('[meta/css] No stylesheets found on disk, re-minifying'); winston.warn('[meta/css] No stylesheets found on disk, re-minifying');
Meta.css.minify.apply(Meta.css, arguments); Meta.css.minify(callback);
return;
} }
if (nconf.get('isPrimary') !== 'true') {
return callback();
}
winston.verbose('[meta/css] Reading stylesheets from file');
async.map([cachePath, acpCachePath], fs.readFile, function(err, files) {
Meta.css.cache = files[0];
Meta.css.acpCache = files[1];
emitter.emit('meta:css.compiled');
callback();
});
}); });
}; };
@ -197,10 +199,10 @@ module.exports = function(Meta) {
} }
function filterMissingFiles(files) { function filterMissingFiles(files) {
return files.filter(function(file) { return files.filter(function(filePath) {
var exists = fs.existsSync(path.join(__dirname, '../../node_modules', file)); var exists = file.existsSync(path.join(__dirname, '../../node_modules', filePath));
if (!exists) { if (!exists) {
winston.warn('[meta/css] File not found! ' + file); winston.warn('[meta/css] File not found! ' + filePath);
} }
return exists; return exists;
}); });

@ -8,7 +8,7 @@ var winston = require('winston'),
os = require('os'), os = require('os'),
nconf = require('nconf'), nconf = require('nconf'),
fs = require('fs'), fs = require('fs'),
file = require('../file'),
plugins = require('../plugins'), plugins = require('../plugins'),
emitter = require('../emitter'), emitter = require('../emitter'),
utils = require('../../public/src/utils'); utils = require('../../public/src/utils');
@ -208,30 +208,31 @@ module.exports = function(Meta) {
var scriptPath = path.join(__dirname, '../../public/nodebb.min.js'), var scriptPath = path.join(__dirname, '../../public/nodebb.min.js'),
mapPath = path.join(__dirname, '../../public/nodebb.min.js.map'), mapPath = path.join(__dirname, '../../public/nodebb.min.js.map'),
paths = [scriptPath]; paths = [scriptPath];
fs.exists(scriptPath, function(exists) { file.exists(scriptPath, function(exists) {
if (exists) { if (!exists) {
if (nconf.get('isPrimary') === 'true') { winston.warn('[meta/js] No script file found on disk, re-minifying');
fs.exists(mapPath, function(exists) { Meta.js.minify(minify, callback);
if (exists) { return;
paths.push(mapPath); }
}
winston.verbose('[meta/js] Reading client-side scripts from file'); if (nconf.get('isPrimary') !== 'true') {
async.map(paths, fs.readFile, function(err, files) { return callback();
Meta.js.cache = files[0]; }
Meta.js.map = files[1] || '';
emitter.emit('meta:js.compiled'); file.exists(mapPath, function(exists) {
callback(); if (exists) {
}); paths.push(mapPath);
});
} else {
callback();
} }
} else {
winston.warn('[meta/js] No script file found on disk, re-minifying'); winston.verbose('[meta/js] Reading client-side scripts from file');
Meta.js.minify.apply(Meta.js, arguments); async.map(paths, fs.readFile, function(err, files) {
} Meta.js.cache = files[0];
Meta.js.map = files[1] || '';
emitter.emit('meta:js.compiled');
callback();
});
});
}); });
}; };

@ -6,6 +6,8 @@ var nconf = require('nconf'),
fs = require('fs'), fs = require('fs'),
path = require('path'), path = require('path'),
async = require('async'), async = require('async'),
file = require('../file'),
db = require('../database'); db = require('../database');
module.exports = function(Meta) { module.exports = function(Meta) {
@ -34,27 +36,24 @@ module.exports = function(Meta) {
async.map(themes, function (theme, next) { async.map(themes, function (theme, next) {
var config = path.join(themePath, theme, 'theme.json'); var config = path.join(themePath, theme, 'theme.json');
if (fs.existsSync(config)) { fs.readFile(config, function (err, file) {
fs.readFile(config, function (err, file) { if (err) {
if (err) { return next();
return next(); }
} else {
var configObj = JSON.parse(file.toString()); var configObj = JSON.parse(file.toString());
// Minor adjustments for API output // Minor adjustments for API output
configObj.type = 'local'; configObj.type = 'local';
if (configObj.screenshot) { if (configObj.screenshot) {
configObj.screenshot_url = nconf.get('relative_path') + '/css/previews/' + configObj.id; configObj.screenshot_url = nconf.get('relative_path') + '/css/previews/' + configObj.id;
} else { } else {
configObj.screenshot_url = nconf.get('relative_path') + '/images/themes/default.png'; configObj.screenshot_url = nconf.get('relative_path') + '/images/themes/default.png';
} }
next(err, configObj); next(null, configObj);
} });
});
} else {
next();
}
}, function (err, themes) { }, function (err, themes) {
themes = themes.filter(function (theme) { themes = themes.filter(function (theme) {
return (theme !== undefined); return (theme !== undefined);
@ -145,7 +144,7 @@ module.exports = function(Meta) {
if (themeObj.templates) { if (themeObj.templates) {
themePath = path.join(nconf.get('themes_path'), themeObj.id, themeObj.templates); themePath = path.join(nconf.get('themes_path'), themeObj.id, themeObj.templates);
} else if (fs.existsSync(fallback)) { } else if (file.existsSync(fallback)) {
themePath = fallback; themePath = fallback;
} }

@ -2,6 +2,7 @@
var meta = require('../meta'), var meta = require('../meta'),
db = require('../database'), db = require('../database'),
file = require('../file'),
auth = require('../routes/authentication'), auth = require('../routes/authentication'),
path = require('path'), path = require('path'),
@ -21,7 +22,7 @@ var middleware = {};
function setupFavicon(app) { function setupFavicon(app) {
var faviconPath = path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico'); var faviconPath = path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico');
if (fs.existsSync(faviconPath)) { if (file.existsSync(faviconPath)) {
app.use(nconf.get('relative_path'), favicon(faviconPath)); app.use(nconf.get('relative_path'), favicon(faviconPath));
} }
} }

@ -14,6 +14,7 @@ var fs = require('fs'),
translator = require('../public/src/modules/translator'), translator = require('../public/src/modules/translator'),
utils = require('../public/src/utils'), utils = require('../public/src/utils'),
hotswap = require('./hotswap'), hotswap = require('./hotswap'),
file = require('./file'),
controllers = require('./controllers'), controllers = require('./controllers'),
app, middleware; app, middleware;
@ -103,7 +104,7 @@ var fs = require('fs'),
return path.join(__dirname, '../node_modules/', plugin); return path.join(__dirname, '../node_modules/', plugin);
}); });
async.filter(plugins, fs.exists, function(plugins){ async.filter(plugins, file.exists, function(plugins) {
async.eachSeries(plugins, Plugins.loadPlugin, next); async.eachSeries(plugins, Plugins.loadPlugin, next);
}); });
}, },

@ -6,6 +6,7 @@ var fs = require('fs'),
async = require('async'), async = require('async'),
winston = require('winston'), winston = require('winston'),
nconf = require('nconf'), nconf = require('nconf'),
file = require('../file'),
utils = require('../../public/src/utils'); utils = require('../../public/src/utils');
@ -107,7 +108,7 @@ module.exports = function(Plugins) {
var realPath = pluginData.staticDirs[mappedPath]; var realPath = pluginData.staticDirs[mappedPath];
var staticDir = path.join(pluginPath, realPath); var staticDir = path.join(pluginPath, realPath);
fs.exists(staticDir, function(exists) { file.exists(staticDir, function(exists) {
if (exists) { if (exists) {
Plugins.staticDirs[pluginData.id + '/' + mappedPath] = staticDir; Plugins.staticDirs[pluginData.id + '/' + mappedPath] = staticDir;
} else { } else {

@ -22,31 +22,18 @@ module.exports = function(app, middleware, controllers) {
} else { } else {
return null; return null;
} }
}).filter(function(a) { return a; }); }).filter(Boolean);
if (matches) { if (!matches) {
async.map(matches, function(mappedPath, next) { return next();
var filePath = path.join(plugins.staticDirs[mappedPath], decodeURIComponent(relPath.slice(mappedPath.length))); }
fs.exists(filePath, function(exists) { matches = matches.map(function(mappedPath) {
if (exists) { return path.join(plugins.staticDirs[mappedPath], decodeURIComponent(relPath.slice(mappedPath.length)));
next(null, filePath); });
} else {
next();
}
});
}, function(err, matches) {
if (err) {
return next(err);
}
matches = matches.filter(Boolean);
if (matches.length) { if (matches.length) {
res.sendFile(matches[0]); res.sendFile(matches[0]);
} else {
next();
}
});
} else { } else {
next(); next();
} }

@ -186,9 +186,10 @@ module.exports.testSocket = function(socketPath, callback) {
return callback(new Error('invalid socket path : ' + socketPath)); return callback(new Error('invalid socket path : ' + socketPath));
} }
var net = require('net'); var net = require('net');
var file = require('./file');
async.series([ async.series([
function(next) { function(next) {
fs.exists(socketPath, function(exists) { file.exists(socketPath, function(exists) {
if (exists) { if (exists) {
next(); next();
} else { } else {

Loading…
Cancel
Save