You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.2 KiB
JavaScript

10 years ago
'use strict';
var path = require('path');
var fs = require('fs');
var async = require('async');
10 years ago
var file = require('../../file');
10 years ago
8 years ago
var themesController = module.exports;
10 years ago
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);
var themeConfigPath = path.join(themeDir, 'theme.json');
8 years ago
var screenshotPath;
async.waterfall([
function (next) {
fs.readFile(themeConfigPath, 'utf8', function (err, config) {
if (err) {
if (err.code === 'ENOENT') {
return next(Error('invalid-data'));
}
8 years ago
return next(err);
}
return next(null, config);
});
},
function (themeConfig, next) {
try {
themeConfig = JSON.parse(themeConfig);
} catch (e) {
return next(e);
}
next(null, themeConfig.screenshot ? path.join(themeDir, themeConfig.screenshot) : defaultScreenshotPath);
},
8 years ago
function (_screenshotPath, next) {
screenshotPath = _screenshotPath;
file.exists(screenshotPath, next);
},
function (exists) {
res.sendFile(exists ? screenshotPath : defaultScreenshotPath);
},
], next);
10 years ago
};