Skip to content

Commit ca294fc

Browse files
committed
closes NodeBB#3663
1 parent 714efd0 commit ca294fc

File tree

15 files changed

+129
-116
lines changed

15 files changed

+129
-116
lines changed

app.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
var nconf = require('nconf');
2424
nconf.argv().env('__');
2525

26-
var fs = require('fs'),
27-
url = require('url'),
26+
var url = require('url'),
2827
async = require('async'),
2928
semver = require('semver'),
3029
winston = require('winston'),
3130
colors = require('colors'),
3231
path = require('path'),
3332
pkg = require('./package.json'),
33+
file = require('./src/file'),
3434
utils = require('./public/src/utils.js');
3535

3636
global.env = process.env.NODE_ENV || 'production';
@@ -53,7 +53,7 @@ if (nconf.get('config')) {
5353
configFile = path.resolve(__dirname, nconf.get('config'));
5454
}
5555

56-
var configExists = fs.existsSync(configFile);
56+
var configExists = file.existsSync(configFile);
5757

5858
loadConfig();
5959

loader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var nconf = require('nconf'),
88

99
async = require('async'),
1010
logrotate = require('logrotate-stream'),
11-
11+
file = require('./src/file'),
1212
pkg = require('./package.json');
1313

1414
nconf.argv().env().file({
@@ -243,7 +243,7 @@ Loader.notifyWorkers = function(msg, worker_pid) {
243243
fs.open(path.join(__dirname, 'config.json'), 'r', function(err) {
244244
if (!err) {
245245
if (nconf.get('daemon') !== 'false' && nconf.get('daemon') !== false) {
246-
if (fs.existsSync(pidFilePath)) {
246+
if (file.existsSync(pidFilePath)) {
247247
try {
248248
var pid = fs.readFileSync(pidFilePath, { encoding: 'utf-8' });
249249
process.kill(pid, 0);

minifier.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var uglifyjs = require('uglify-js'),
44
less = require('less'),
55
async = require('async'),
66
fs = require('fs'),
7+
file = require('./src/file'),
78
crypto = require('crypto'),
89
utils = require('./public/src/utils'),
910

@@ -14,16 +15,16 @@ var uglifyjs = require('uglify-js'),
1415
/* Javascript */
1516
Minifier.js.minify = function (scripts, minify, callback) {
1617
scripts = scripts.filter(function(file) {
17-
return fs.existsSync(file) && file.endsWith('.js');
18+
return file && file.endsWith('.js');
1819
});
1920

20-
if (minify) {
21-
minifyScripts(scripts, function() {
22-
callback.apply(this, arguments);
23-
});
24-
} else {
25-
concatenateScripts(scripts, callback);
26-
}
21+
async.filter(scripts, file.exists, function(scripts) {
22+
if (minify) {
23+
minifyScripts(scripts, callback);
24+
} else {
25+
concatenateScripts(scripts, callback);
26+
}
27+
});
2728
};
2829

2930
process.on('message', function(payload) {

public/src/modules/translator.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@
264264
var fs = require('fs'),
265265
path = require('path'),
266266
winston = require('winston'),
267+
file = require('../../../src/file'),
267268
meta = require('../../../src/meta');
268269

269270
language = language || meta.config.defaultLang || 'en_GB';
270271

271-
if (!fs.existsSync(path.join(__dirname, '../../language', language))) {
272+
if (!file.existsSync(path.join(__dirname, '../../language', language))) {
272273
winston.warn('[translator] Language \'' + meta.config.defaultLang + '\' not found. Defaulting to \'en_GB\'');
273274
language = 'en_GB';
274275
}

src/controllers/admin/themes.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
'use strict';
22

33
var path = require('path');
4-
var fs = require('fs');
4+
var file = require('../../file');
55

66
var themesController = {};
77

88
themesController.get = function(req, res, next) {
99
var themeDir = path.join(__dirname, '../../../node_modules/' + req.params.theme);
10-
fs.exists(themeDir, function(exists) {
11-
if (exists) {
12-
var themeConfig = require(path.join(themeDir, 'theme.json')),
13-
screenshotPath = path.join(themeDir, themeConfig.screenshot);
14-
if (themeConfig.screenshot && fs.existsSync(screenshotPath)) {
15-
res.sendFile(screenshotPath);
16-
} else {
17-
res.sendFile(path.join(__dirname, '../../../public/images/themes/default.png'));
18-
}
19-
} else {
10+
file.exists(themeDir, function(exists) {
11+
if (!exists) {
2012
return next();
2113
}
14+
15+
var themeConfig = require(path.join(themeDir, 'theme.json')),
16+
screenshotPath = path.join(themeDir, themeConfig.screenshot);
17+
if (themeConfig.screenshot && file.existsSync(screenshotPath)) {
18+
res.sendFile(screenshotPath);
19+
} else {
20+
res.sendFile(path.join(__dirname, '../../../public/images/themes/default.png'));
21+
}
2222
});
2323
};
2424

src/file.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var fs = require('fs'),
88
Magic = mmmagic.Magic,
99
mime = require('mime'),
1010

11-
meta = require('./meta'),
1211
utils = require('../public/src/utils');
1312

1413
var file = {};
@@ -63,6 +62,7 @@ file.isFileTypeAllowed = function(path, allowedExtensions, callback) {
6362
};
6463

6564
file.allowedExtensions = function() {
65+
var meta = require('./meta');
6666
var allowedExtensions = (meta.config.allowedFileExtensions || '').trim();
6767
if (!allowedExtensions) {
6868
return [];
@@ -80,4 +80,21 @@ file.allowedExtensions = function() {
8080
return allowedExtensions;
8181
};
8282

83+
file.exists = function(path, callback) {
84+
fs.stat(path, function(err, stat) {
85+
callback(!err && stat);
86+
});
87+
};
88+
89+
file.existsSync = function(path) {
90+
var exists = false;
91+
try {
92+
exists = fs.statSync(path);
93+
} catch(err) {
94+
exists = false;
95+
}
96+
97+
return !!exists;
98+
};
99+
83100
module.exports = file;

src/logger.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var fs = require('fs'),
1010
winston = require('winston'),
1111
util = require('util'),
1212
socketio = require('socket.io'),
13+
file = require('./file'),
1314
meta = require('./meta'),
1415
morgan = require('morgan');
1516

@@ -76,7 +77,7 @@ var opts = {
7677
/* Open the streams to log to: either a path or stdout */
7778
var stream;
7879
if(value) {
79-
if(fs.existsSync(value)) {
80+
if(file.existsSync(value)) {
8081
var stats = fs.statSync(value);
8182
if(stats) {
8283
if(stats.isDirectory()) {

src/meta/css.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var winston = require('winston'),
1111
plugins = require('../plugins'),
1212
emitter = require('../emitter'),
1313
db = require('../database'),
14+
file = require('../file'),
1415
utils = require('../../public/src/utils');
1516

1617
module.exports = function(Meta) {
@@ -149,24 +150,25 @@ module.exports = function(Meta) {
149150
Meta.css.getFromFile = function(callback) {
150151
var cachePath = path.join(__dirname, '../../public/stylesheet.css'),
151152
acpCachePath = path.join(__dirname, '../../public/admin.css');
152-
fs.exists(cachePath, function(exists) {
153-
if (exists) {
154-
if (nconf.get('isPrimary') === 'true') {
155-
winston.verbose('[meta/css] Reading stylesheets from file');
156-
async.map([cachePath, acpCachePath], fs.readFile, function(err, files) {
157-
Meta.css.cache = files[0];
158-
Meta.css.acpCache = files[1];
159-
160-
emitter.emit('meta:css.compiled');
161-
callback();
162-
});
163-
} else {
164-
callback();
165-
}
166-
} else {
153+
file.exists(cachePath, function(exists) {
154+
if (!exists) {
167155
winston.warn('[meta/css] No stylesheets found on disk, re-minifying');
168-
Meta.css.minify.apply(Meta.css, arguments);
156+
Meta.css.minify(callback);
157+
return;
169158
}
159+
160+
if (nconf.get('isPrimary') !== 'true') {
161+
return callback();
162+
}
163+
164+
winston.verbose('[meta/css] Reading stylesheets from file');
165+
async.map([cachePath, acpCachePath], fs.readFile, function(err, files) {
166+
Meta.css.cache = files[0];
167+
Meta.css.acpCache = files[1];
168+
169+
emitter.emit('meta:css.compiled');
170+
callback();
171+
});
170172
});
171173
};
172174

@@ -197,10 +199,10 @@ module.exports = function(Meta) {
197199
}
198200

199201
function filterMissingFiles(files) {
200-
return files.filter(function(file) {
201-
var exists = fs.existsSync(path.join(__dirname, '../../node_modules', file));
202+
return files.filter(function(filePath) {
203+
var exists = file.existsSync(path.join(__dirname, '../../node_modules', filePath));
202204
if (!exists) {
203-
winston.warn('[meta/css] File not found! ' + file);
205+
winston.warn('[meta/css] File not found! ' + filePath);
204206
}
205207
return exists;
206208
});

src/meta/js.js

+23-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var winston = require('winston'),
88
os = require('os'),
99
nconf = require('nconf'),
1010
fs = require('fs'),
11-
11+
file = require('../file'),
1212
plugins = require('../plugins'),
1313
emitter = require('../emitter'),
1414
utils = require('../../public/src/utils');
@@ -208,30 +208,31 @@ module.exports = function(Meta) {
208208
var scriptPath = path.join(__dirname, '../../public/nodebb.min.js'),
209209
mapPath = path.join(__dirname, '../../public/nodebb.min.js.map'),
210210
paths = [scriptPath];
211-
fs.exists(scriptPath, function(exists) {
212-
if (exists) {
213-
if (nconf.get('isPrimary') === 'true') {
214-
fs.exists(mapPath, function(exists) {
215-
if (exists) {
216-
paths.push(mapPath);
217-
}
211+
file.exists(scriptPath, function(exists) {
212+
if (!exists) {
213+
winston.warn('[meta/js] No script file found on disk, re-minifying');
214+
Meta.js.minify(minify, callback);
215+
return;
216+
}
218217

219-
winston.verbose('[meta/js] Reading client-side scripts from file');
220-
async.map(paths, fs.readFile, function(err, files) {
221-
Meta.js.cache = files[0];
222-
Meta.js.map = files[1] || '';
218+
if (nconf.get('isPrimary') !== 'true') {
219+
return callback();
220+
}
223221

224-
emitter.emit('meta:js.compiled');
225-
callback();
226-
});
227-
});
228-
} else {
229-
callback();
222+
file.exists(mapPath, function(exists) {
223+
if (exists) {
224+
paths.push(mapPath);
230225
}
231-
} else {
232-
winston.warn('[meta/js] No script file found on disk, re-minifying');
233-
Meta.js.minify.apply(Meta.js, arguments);
234-
}
226+
227+
winston.verbose('[meta/js] Reading client-side scripts from file');
228+
async.map(paths, fs.readFile, function(err, files) {
229+
Meta.js.cache = files[0];
230+
Meta.js.map = files[1] || '';
231+
232+
emitter.emit('meta:js.compiled');
233+
callback();
234+
});
235+
});
235236
});
236237
};
237238

src/meta/themes.js

+21-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var nconf = require('nconf'),
66
fs = require('fs'),
77
path = require('path'),
88
async = require('async'),
9+
10+
file = require('../file'),
911
db = require('../database');
1012

1113
module.exports = function(Meta) {
@@ -34,27 +36,24 @@ module.exports = function(Meta) {
3436
async.map(themes, function (theme, next) {
3537
var config = path.join(themePath, theme, 'theme.json');
3638

37-
if (fs.existsSync(config)) {
38-
fs.readFile(config, function (err, file) {
39-
if (err) {
40-
return next();
41-
} else {
42-
var configObj = JSON.parse(file.toString());
43-
44-
// Minor adjustments for API output
45-
configObj.type = 'local';
46-
if (configObj.screenshot) {
47-
configObj.screenshot_url = nconf.get('relative_path') + '/css/previews/' + configObj.id;
48-
} else {
49-
configObj.screenshot_url = nconf.get('relative_path') + '/images/themes/default.png';
50-
}
51-
52-
next(err, configObj);
53-
}
54-
});
55-
} else {
56-
next();
57-
}
39+
fs.readFile(config, function (err, file) {
40+
if (err) {
41+
return next();
42+
}
43+
44+
var configObj = JSON.parse(file.toString());
45+
46+
// Minor adjustments for API output
47+
configObj.type = 'local';
48+
if (configObj.screenshot) {
49+
configObj.screenshot_url = nconf.get('relative_path') + '/css/previews/' + configObj.id;
50+
} else {
51+
configObj.screenshot_url = nconf.get('relative_path') + '/images/themes/default.png';
52+
}
53+
54+
next(null, configObj);
55+
});
56+
5857
}, function (err, themes) {
5958
themes = themes.filter(function (theme) {
6059
return (theme !== undefined);
@@ -145,7 +144,7 @@ module.exports = function(Meta) {
145144

146145
if (themeObj.templates) {
147146
themePath = path.join(nconf.get('themes_path'), themeObj.id, themeObj.templates);
148-
} else if (fs.existsSync(fallback)) {
147+
} else if (file.existsSync(fallback)) {
149148
themePath = fallback;
150149
}
151150

src/middleware/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var meta = require('../meta'),
44
db = require('../database'),
5+
file = require('../file'),
56
auth = require('../routes/authentication'),
67

78
path = require('path'),
@@ -21,7 +22,7 @@ var middleware = {};
2122

2223
function setupFavicon(app) {
2324
var faviconPath = path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico');
24-
if (fs.existsSync(faviconPath)) {
25+
if (file.existsSync(faviconPath)) {
2526
app.use(nconf.get('relative_path'), favicon(faviconPath));
2627
}
2728
}

0 commit comments

Comments
 (0)