From 06a6dc835dcc6f7ac540f5efb11309f320df0539 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Thu, 20 Jun 2019 13:34:22 +0200 Subject: [PATCH] Moved API v0.1 settings upload/downalod routes.yaml methods to use setting handler service --- .../services/routing/SettingsHandler.js | 11 ++++ core/server/api/v0.1/settings.js | 60 +------------------ 2 files changed, 14 insertions(+), 57 deletions(-) diff --git a/core/frontend/services/routing/SettingsHandler.js b/core/frontend/services/routing/SettingsHandler.js index de03b599f1..116bc983fd 100644 --- a/core/frontend/services/routing/SettingsHandler.js +++ b/core/frontend/services/routing/SettingsHandler.js @@ -6,6 +6,17 @@ const urlService = require('../url'); const common = require('../../../server/lib/common'); const config = require('../../../server/config'); +/** + * The `routes.yaml` file offers a way to configure your Ghost blog. It's currently a setting feature + * we have added. That's why the `routes.yaml` file is treated as a "setting" right now. + * If we want to add single permissions for this file (e.g. upload/download routes.yaml), we can add later. + * + * How does it work? + * + * - we first reset all url generators (each url generator belongs to one express router) + * - we don't destroy the resources, we only release them (this avoids reloading all resources from the db again) + * - then we reload the whole site app, which will reset all routers and re-create the url generators + */ const activate = (filePath) => { const settingsPath = config.getContentPath('settings'); const backupRoutesPath = path.join(settingsPath, `routes-${moment().format('YYYY-MM-DD-HH-mm-ss')}.yaml`); diff --git a/core/server/api/v0.1/settings.js b/core/server/api/v0.1/settings.js index 06e4526204..a3fb87c325 100644 --- a/core/server/api/v0.1/settings.js +++ b/core/server/api/v0.1/settings.js @@ -2,14 +2,10 @@ // RESTful API for the Setting resource const Promise = require('bluebird'), _ = require('lodash'), - moment = require('moment-timezone'), - fs = require('fs-extra'), - path = require('path'), - config = require('../../config'), models = require('../../models'), canThis = require('../../services/permissions').canThis, localUtils = require('./utils'), - urlService = require('../../../frontend/services/url'), + frontendRouting = require('../../../frontend/services/routing'), common = require('../../lib/common'), settingsCache = require('../../services/settings/cache'), docName = 'settings'; @@ -255,67 +251,17 @@ settings = { }); }, - /** - * The `routes.yaml` file offers a way to configure your Ghost blog. It's currently a setting feature - * we have added. That's why the `routes.yaml` file is treated as a "setting" right now. - * If we want to add single permissions for this file (e.g. upload/download routes.yaml), we can add later. - * - * How does it work? - * - * - we first reset all url generators (each url generator belongs to one express router) - * - we don't destroy the resources, we only release them (this avoids reloading all resources from the db again) - * - then we reload the whole site app, which will reset all routers and re-create the url generators - */ upload(options) { - const backupRoutesPath = path.join(config.getContentPath('settings'), `routes-${moment().format('YYYY-MM-DD-HH-mm-ss')}.yaml`); - return localUtils.handlePermissions('settings', 'edit')(options) .then(() => { - return fs.copy(`${config.getContentPath('settings')}/routes.yaml`, backupRoutesPath); - }) - .then(() => { - return fs.copy(options.path, `${config.getContentPath('settings')}/routes.yaml`); - }) - .then(() => { - urlService.resetGenerators({releaseResourcesOnly: true}); - }) - .then(() => { - const siteApp = require('../../web/site/app'); - - try { - return siteApp.reload(); - } catch (err) { - // bring back backup, otherwise your Ghost blog is broken - return fs.copy(backupRoutesPath, `${config.getContentPath('settings')}/routes.yaml`) - .then(() => { - return siteApp.reload(); - }) - .then(() => { - throw err; - }); - } + return frontendRouting.settings.activate(options.path); }); }, download(options) { - const routesPath = path.join(config.getContentPath('settings'), 'routes.yaml'); - return localUtils.handlePermissions('settings', 'browse')(options) .then(() => { - return fs.readFile(routesPath, 'utf-8'); - }) - .catch((err) => { - if (err.code === 'ENOENT') { - return Promise.resolve([]); - } - - if (common.errors.utils.isIgnitionError(err)) { - throw err; - } - - throw new common.errors.NotFoundError({ - err: err - }); + return frontendRouting.settings.serve(); }); } };