diff --git a/src/controllers/admin.js b/src/controllers/admin.js index eed56b43a0..10c852a74d 100644 --- a/src/controllers/admin.js +++ b/src/controllers/admin.js @@ -1,6 +1,8 @@ "use strict"; var async = require('async'), + fs = require('fs'), + path = require('path'), user = require('../user'), categories = require('../categories'), @@ -32,6 +34,7 @@ var adminController = { settings: {}, logger: {}, sounds: {}, + themes: {}, users: require('./admin/users'), uploads: require('./admin/uploads') }; @@ -297,4 +300,21 @@ adminController.sounds.get = function(req, res, next) { }); }; +adminController.themes.get = function(req, res, next) { + var themeDir = path.join(__dirname, '../../node_modules/' + req.params.theme); + fs.exists(themeDir, function(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(); + } + }); +} + module.exports = adminController; diff --git a/src/middleware/index.js b/src/middleware/index.js index f9ee6e4c01..7f5eb46ddb 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -29,27 +29,6 @@ var utils = require('./../../public/src/utils'), var middleware = {}; -function routeThemeScreenshots(app, themes) { - var screenshotPath; - - async.each(themes, function(themeObj, next) { - if (themeObj.screenshot) { - screenshotPath = path.join(themesPath, themeObj.id, themeObj.screenshot); - (function(id, path) { - fs.exists(path, function(exists) { - if (exists) { - app.get(relativePath + '/css/previews/' + id, function(req, res) { - res.sendfile(path); - }); - } - }); - })(themeObj.id, screenshotPath); - } else { - next(false); - } - }); -} - function routeCurrentTheme(app, themeId, themesData) { var themeId = (themeId || 'nodebb-theme-vanilla'), themeObj = (function(id) { @@ -132,7 +111,6 @@ module.exports = function(app, data) { auth.initialize(app, middleware); routeCurrentTheme(app, data.currentThemeId, data.themesData); - routeThemeScreenshots(app, data.themesData); meta.templates.compile(); return middleware; diff --git a/src/routes/meta.js b/src/routes/meta.js index a7af01721e..9cfcc2faf8 100644 --- a/src/routes/meta.js +++ b/src/routes/meta.js @@ -56,6 +56,7 @@ module.exports = function(app, middleware, controllers) { app.get('/nodebb.min.js', middleware.addExpiresHeaders, sendMinifiedJS); app.get('/sitemap.xml', controllers.sitemap); app.get('/robots.txt', controllers.robots); + app.get('/css/previews/:theme', controllers.admin.themes.get); if (!minificationEnabled) { app.get('/nodebb.min.js.map', middleware.addExpiresHeaders, sendSourceMap);