Less synchronous stuffs
parent
0ea89c2799
commit
44e55d2a98
@ -1,25 +1,53 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
var fs = require('fs');
|
||||||
|
var async = require('async');
|
||||||
|
|
||||||
var file = require('../../file');
|
var file = require('../../file');
|
||||||
|
|
||||||
var themesController = {};
|
var themesController = {};
|
||||||
|
|
||||||
|
var defaultScreenshotPath = path.join(__dirname, '../../../public/images/themes/default.png');
|
||||||
|
|
||||||
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);
|
||||||
file.exists(themeDir, function (err, exists) {
|
var themeConfigPath = path.join(themeDir, 'theme.json');
|
||||||
if (err || !exists) {
|
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
file.exists(themeConfigPath, function (err, exists) {
|
||||||
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
if (!exists) {
|
||||||
|
return next(Error('invalid-data'));
|
||||||
|
}
|
||||||
|
|
||||||
var themeConfig = require(path.join(themeDir, 'theme.json'));
|
next();
|
||||||
var screenshotPath = path.join(themeDir, themeConfig.screenshot);
|
});
|
||||||
if (themeConfig.screenshot && file.existsSync(screenshotPath)) {
|
},
|
||||||
res.sendFile(screenshotPath);
|
function (next) {
|
||||||
} else {
|
fs.readFile(themeConfigPath, next);
|
||||||
res.sendFile(path.join(__dirname, '../../../public/images/themes/default.png'));
|
},
|
||||||
|
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;
|
module.exports = themesController;
|
||||||
|
Loading…
Reference in New Issue