From 11635470bc46d2e95dc807246c7b54b9fbca5a23 Mon Sep 17 00:00:00 2001 From: OldHawk Date: Fri, 29 Dec 2017 13:56:08 +0800 Subject: [PATCH] feat(config): set config default value on render index --- config/env/torrents.js | 2 +- .../controllers/core.server.controller.js | 27 ++++++++++++++++--- .../controllers/torrents.server.controller.js | 6 ++--- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/config/env/torrents.js b/config/env/torrents.js index a261ebc6..29450bf9 100644 --- a/config/env/torrents.js +++ b/config/env/torrents.js @@ -16,7 +16,7 @@ module.exports = { */ app: { name: 'CHD.im', - domain: 'http://chd.im:3000', + domain: 'http://chd:3000', showDemoWarningPopup: true, cronTimeZone: 'Asia/Shanghai', showDebugLog: true diff --git a/modules/core/server/controllers/core.server.controller.js b/modules/core/server/controllers/core.server.controller.js index f6d2368c..3380fb68 100644 --- a/modules/core/server/controllers/core.server.controller.js +++ b/modules/core/server/controllers/core.server.controller.js @@ -1,8 +1,6 @@ 'use strict'; -var validator = require('validator'), - path = require('path'), - moment = require('moment'), +var path = require('path'), config = require(path.resolve('./config/config')); /** @@ -15,6 +13,8 @@ exports.renderIndex = function (req, res) { req.user.addSignedIp(ip); } + setMeanTorrentConfigDefaultValue(req, res, config.meanTorrentConfig); + res.render('modules/core/server/views/index', { user: JSON.stringify(safeUserObject), sharedConfig: JSON.stringify(config.shared), @@ -28,6 +28,8 @@ exports.renderIndex = function (req, res) { exports.renderServerError = function (req, res) { var safeUserObject = req.user || null; + setMeanTorrentConfigDefaultValue(req, res, config.meanTorrentConfig); + res.status(500).render('modules/core/server/views/500', { user: JSON.stringify(safeUserObject), sharedConfig: JSON.stringify(config.shared), @@ -43,6 +45,8 @@ exports.renderServerError = function (req, res) { exports.renderNotFound = function (req, res) { var safeUserObject = req.user || null; + setMeanTorrentConfigDefaultValue(req, res, config.meanTorrentConfig); + res.status(404).format({ 'text/html': function () { res.render('modules/core/server/views/404', { @@ -92,3 +96,20 @@ function getSafeMeanTorrentConfig(cfg) { return cfg; } + +/** + * setMeanTorrentConfigDefaultValue + * @param cfg + */ +function setMeanTorrentConfigDefaultValue(req, res, cfg) { + var httpTransport = 'http://'; + if (config.secure && config.secure.ssl === true) { + httpTransport = 'https://'; + } + var baseUrl = req.app.get('domain') || httpTransport + req.headers.host; + + cfg.app.domain = baseUrl; + if (!cfg.announce.url.startsWith('http')) { + cfg.announce.url = baseUrl + cfg.announce.url; + } +} diff --git a/modules/torrents/server/controllers/torrents.server.controller.js b/modules/torrents/server/controllers/torrents.server.controller.js index 98a28e76..ccb2ca08 100644 --- a/modules/torrents/server/controllers/torrents.server.controller.js +++ b/modules/torrents/server/controllers/torrents.server.controller.js @@ -204,7 +204,7 @@ exports.upload = function (req, res) { } else { if (config.meanTorrentConfig.announce.privateTorrentCmsMode) { //force change announce url to config value - var announce = appConfig.domain + config.meanTorrentConfig.announce.url; + var announce = config.meanTorrentConfig.announce.url; torrent.metadata.announce = announce; var cws = fs.createWriteStream(newfile); @@ -477,7 +477,7 @@ exports.announceEdit = function (req, res) { message = 'Read torrent file faild'; reject(message); } else { - var announce = appConfig.domain + config.meanTorrentConfig.announce.url; + var announce = config.meanTorrentConfig.announce.url; torrent.metadata.announce = announce; torrent.metadata.comment = req.query.comment; torrent_data = torrent.metadata; @@ -551,7 +551,7 @@ exports.download = function (req, res) { reject(message); } else { if (config.meanTorrentConfig.announce.privateTorrentCmsMode) { - var announce = appConfig.domain + config.meanTorrentConfig.announce.url + '/' + user.passkey; + var announce = config.meanTorrentConfig.announce.url + '/' + user.passkey; torrent.metadata.announce = announce; } torrent_data = torrent.metadata;