From c3b14f82fd1f35ce4dbaa89ba934d28b50a3caa8 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Thu, 20 Jun 2019 13:19:22 +0200 Subject: [PATCH] Moved settings#upload method out of settings controller --- .../services/routing/SettingsHandler.js | 70 +++++++++++++++++++ core/frontend/services/routing/index.js | 4 ++ core/frontend/services/themes/index.js | 3 + core/server/api/v2/settings.js | 60 +--------------- 4 files changed, 79 insertions(+), 58 deletions(-) create mode 100644 core/frontend/services/routing/SettingsHandler.js diff --git a/core/frontend/services/routing/SettingsHandler.js b/core/frontend/services/routing/SettingsHandler.js new file mode 100644 index 0000000000..53987303cd --- /dev/null +++ b/core/frontend/services/routing/SettingsHandler.js @@ -0,0 +1,70 @@ +const moment = require('moment-timezone'); +const fs = require('fs-extra'); +const path = require('path'); +const urlService = require('../url'); + +const common = require('../../../server/lib/common'); +const config = require('../../../server/config'); + +const activate = (filePath) => { + const settingsPath = config.getContentPath('settings'); + const backupRoutesPath = path.join(settingsPath, `routes-${moment().format('YYYY-MM-DD-HH-mm-ss')}.yaml`); + + return fs.copy(`${settingsPath}/routes.yaml`, backupRoutesPath) + .then(() => { + return fs.copy(filePath, `${settingsPath}/routes.yaml`); + }) + .then(() => { + urlService.resetGenerators({releaseResourcesOnly: true}); + }) + .then(() => { + const siteApp = require('../../../server/web/site/app'); + + const bringBackValidRoutes = () => { + urlService.resetGenerators({releaseResourcesOnly: true}); + + return fs.copy(backupRoutesPath, `${settingsPath}/routes.yaml`) + .then(() => { + return siteApp.reload(); + }); + }; + + try { + siteApp.reload(); + } catch (err) { + return bringBackValidRoutes() + .finally(() => { + throw err; + }); + } + + let tries = 0; + + function isBlogRunning() { + return Promise.delay(1000) + .then(() => { + if (!urlService.hasFinished()) { + if (tries > 5) { + throw new common.errors.InternalServerError({ + message: 'Could not load routes.yaml file.' + }); + } + + tries = tries + 1; + return isBlogRunning(); + } + }); + } + + return isBlogRunning() + .catch((err) => { + return bringBackValidRoutes() + .finally(() => { + throw err; + }); + }); + }); +}; + +module.exports.activate = activate; +// module.exports.serve = serve; diff --git a/core/frontend/services/routing/index.js b/core/frontend/services/routing/index.js index 2a480bc74a..c7bf68edeb 100644 --- a/core/frontend/services/routing/index.js +++ b/core/frontend/services/routing/index.js @@ -17,5 +17,9 @@ module.exports = { get TaxonomyRouter() { return require('./TaxonomyRouter'); + }, + + get settings() { + return require('./SettingsHandler'); } }; diff --git a/core/frontend/services/themes/index.js b/core/frontend/services/themes/index.js index 60d95c4b7a..f3bec1f031 100644 --- a/core/frontend/services/themes/index.js +++ b/core/frontend/services/themes/index.js @@ -96,6 +96,9 @@ module.exports = { } else { return engineDefaults['ghost-api']; } + }, + delete: function (){ + }, activate: function activate(loadedTheme, checkedTheme, error) { // no need to check the score, activation should be used in combination with validate.check diff --git a/core/server/api/v2/settings.js b/core/server/api/v2/settings.js index 4823e9ab8c..98779b8a6c 100644 --- a/core/server/api/v2/settings.js +++ b/core/server/api/v2/settings.js @@ -1,11 +1,10 @@ const Promise = require('bluebird'); const _ = require('lodash'); -const moment = require('moment-timezone'); const fs = require('fs-extra'); const path = require('path'); const config = require('../../config'); const models = require('../../models'); -const urlService = require('../../../frontend/services/url'); +const frontendRouting = require('../../../frontend/services/routing'); const common = require('../../lib/common'); const settingsCache = require('../../services/settings/cache'); @@ -151,62 +150,7 @@ module.exports = { method: 'edit' }, query(frame) { - const backupRoutesPath = path.join(config.getContentPath('settings'), `routes-${moment().format('YYYY-MM-DD-HH-mm-ss')}.yaml`); - - return fs.copy(`${config.getContentPath('settings')}/routes.yaml`, backupRoutesPath) - .then(() => { - return fs.copy(frame.file.path, `${config.getContentPath('settings')}/routes.yaml`); - }) - .then(() => { - urlService.resetGenerators({releaseResourcesOnly: true}); - }) - .then(() => { - const siteApp = require('../../web/site/app'); - - const bringBackValidRoutes = () => { - urlService.resetGenerators({releaseResourcesOnly: true}); - - return fs.copy(backupRoutesPath, `${config.getContentPath('settings')}/routes.yaml`) - .then(() => { - return siteApp.reload(); - }); - }; - - try { - siteApp.reload(); - } catch (err) { - return bringBackValidRoutes() - .finally(() => { - throw err; - }); - } - - let tries = 0; - - function isBlogRunning() { - return Promise.delay(1000) - .then(() => { - if (!urlService.hasFinished()) { - if (tries > 5) { - throw new common.errors.InternalServerError({ - message: 'Could not load routes.yaml file.' - }); - } - - tries = tries + 1; - return isBlogRunning(); - } - }); - } - - return isBlogRunning() - .catch((err) => { - return bringBackValidRoutes() - .finally(() => { - throw err; - }); - }); - }); + return frontendRouting.settings.activate(frame.file.path); } },