Less synchronous stuffs

v1.18.x
Peter Jaszkowiak 8 years ago
parent 0ea89c2799
commit 44e55d2a98

@ -1,25 +1,53 @@
'use strict';
var path = require('path');
var fs = require('fs');
var async = require('async');
var file = require('../../file');
var themesController = {};
var defaultScreenshotPath = path.join(__dirname, '../../../public/images/themes/default.png');
themesController.get = function (req, res, next) {
var themeDir = path.join(__dirname, '../../../node_modules/' + req.params.theme);
file.exists(themeDir, function (err, exists) {
if (err || !exists) {
return next(err);
}
var themeConfig = require(path.join(themeDir, 'theme.json'));
var 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'));
}
});
var themeDir = path.join(__dirname, '../../../node_modules', req.params.theme);
var themeConfigPath = path.join(themeDir, 'theme.json');
async.waterfall([
function (next) {
file.exists(themeConfigPath, function (err, exists) {
if (err) {
return next(err);
}
if (!exists) {
return next(Error('invalid-data'));
}
next();
});
},
function (next) {
fs.readFile(themeConfigPath, next);
},
function (themeConfig, next) {
try {
themeConfig = JSON.parse(themeConfig);
next(null, themeConfig.screenshot ? path.join(themeDir, themeConfig.screenshot) : defaultScreenshotPath);
} catch (e) {
next(e);
}
},
function (screenshotPath, next) {
file.exists(screenshotPath, function (err, exists) {
if (err) {
return next(err);
}
res.sendFile(exists ? screenshotPath : defaultScreenshotPath);
});
},
], next);
};
module.exports = themesController;

Loading…
Cancel
Save