2020-11-04 13:55:47 +03:00
|
|
|
const Promise = require('bluebird');
|
2019-06-20 14:19:22 +03:00
|
|
|
const moment = require('moment-timezone');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
2021-09-27 17:34:15 +03:00
|
|
|
const crypto = require('crypto');
|
2021-06-28 14:09:02 +03:00
|
|
|
const urlService = require('../../../frontend/services/url');
|
2019-06-20 14:19:22 +03:00
|
|
|
|
2021-06-28 14:09:02 +03:00
|
|
|
const debug = require('@tryghost/debug')('services:route-settings');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-06-27 16:10:35 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../shared/config');
|
2021-06-27 15:38:48 +03:00
|
|
|
const bridge = require('../../../bridge');
|
2021-09-30 14:08:48 +03:00
|
|
|
const SettingsLoader = require('./settings-loader');
|
2021-09-30 15:25:36 +03:00
|
|
|
const parseYaml = require('./yaml-parser');
|
2021-09-30 14:08:48 +03:00
|
|
|
|
2021-09-30 15:25:36 +03:00
|
|
|
const settingsLoader = new SettingsLoader({parseYaml});
|
2019-06-20 14:19:22 +03:00
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const messages = {
|
|
|
|
loadError: 'Could not load {filename} file.'
|
|
|
|
};
|
|
|
|
|
2019-06-20 14:34:22 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-06-27 15:38:48 +03:00
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const filename = 'routes';
|
|
|
|
const ext = 'yaml';
|
2019-06-20 14:19:22 +03:00
|
|
|
|
2021-09-27 17:49:00 +03:00
|
|
|
const getSettingsFilePath = () => {
|
|
|
|
const settingsFolder = config.getContentPath('settings');
|
2021-06-27 16:10:35 +03:00
|
|
|
return path.join(settingsFolder, `${filename}.${ext}`);
|
2019-06-20 14:19:22 +03:00
|
|
|
};
|
|
|
|
|
2021-09-27 17:49:00 +03:00
|
|
|
const getBackupFilePath = () => {
|
|
|
|
const settingsFolder = config.getContentPath('settings');
|
2021-06-27 16:10:35 +03:00
|
|
|
return path.join(settingsFolder, `${filename}-${moment().format('YYYY-MM-DD-HH-mm-ss')}.${ext}`);
|
|
|
|
};
|
2019-06-20 14:23:58 +03:00
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const createBackupFile = async (settingsPath, backupPath) => {
|
|
|
|
return await fs.copy(settingsPath, backupPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
const restoreBackupFile = async (settingsPath, backupPath) => {
|
|
|
|
return await fs.copy(backupPath, settingsPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
const saveFile = async (filePath, settingsPath) => {
|
|
|
|
return await fs.copy(filePath, settingsPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
const readFile = (settingsFilePath) => {
|
|
|
|
return fs.readFile(settingsFilePath, 'utf-8')
|
2019-06-20 14:23:58 +03:00
|
|
|
.catch((err) => {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
|
|
|
|
2020-05-22 21:22:20 +03:00
|
|
|
if (errors.utils.isIgnitionError(err)) {
|
2019-06-20 14:23:58 +03:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.NotFoundError({
|
2019-06-20 14:23:58 +03:00
|
|
|
err: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-27 16:10:35 +03:00
|
|
|
const setFromFilePath = async (filePath) => {
|
2021-09-27 17:49:00 +03:00
|
|
|
const settingsPath = getSettingsFilePath();
|
|
|
|
const backupPath = getBackupFilePath();
|
2021-06-27 16:10:35 +03:00
|
|
|
|
|
|
|
await createBackupFile(settingsPath, backupPath);
|
|
|
|
await saveFile(filePath, settingsPath);
|
|
|
|
|
|
|
|
urlService.resetGenerators({releaseResourcesOnly: true});
|
|
|
|
|
|
|
|
const bringBackValidRoutes = async () => {
|
|
|
|
urlService.resetGenerators({releaseResourcesOnly: true});
|
|
|
|
|
|
|
|
await restoreBackupFile(settingsPath, backupPath);
|
|
|
|
|
|
|
|
return bridge.reloadFrontend();
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
bridge.reloadFrontend();
|
|
|
|
} catch (err) {
|
|
|
|
return bringBackValidRoutes()
|
|
|
|
.finally(() => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: how can we get rid of this from here?
|
|
|
|
let tries = 0;
|
|
|
|
|
|
|
|
function isBlogRunning() {
|
|
|
|
debug('waiting for blog running');
|
|
|
|
return Promise.delay(1000)
|
|
|
|
.then(() => {
|
|
|
|
debug('waited for blog running');
|
|
|
|
if (!urlService.hasFinished()) {
|
|
|
|
if (tries > 5) {
|
|
|
|
throw new errors.InternalServerError({
|
|
|
|
message: tpl(messages.loadError, {filename: `${filename}.${ext}`})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tries = tries + 1;
|
|
|
|
return isBlogRunning();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return isBlogRunning()
|
|
|
|
.catch((err) => {
|
|
|
|
return bringBackValidRoutes()
|
|
|
|
.finally(() => {
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const get = async () => {
|
|
|
|
const settingsFilePath = await getSettingsFilePath();
|
|
|
|
|
|
|
|
return readFile(settingsFilePath);
|
|
|
|
};
|
|
|
|
|
2021-09-27 17:34:15 +03:00
|
|
|
/**
|
|
|
|
* md5 hashes of default routes settings
|
|
|
|
*/
|
|
|
|
const defaultRoutesSettingHash = '3d180d52c663d173a6be791ef411ed01';
|
|
|
|
|
|
|
|
const calculateHash = (data) => {
|
|
|
|
return crypto.createHash('md5')
|
|
|
|
.update(data, 'binary')
|
|
|
|
.digest('hex');
|
|
|
|
};
|
|
|
|
|
2021-09-28 10:47:42 +03:00
|
|
|
const getDefaultHash = () => {
|
2021-09-27 17:34:15 +03:00
|
|
|
return defaultRoutesSettingHash;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getCurrentHash = async () => {
|
2021-09-30 14:08:48 +03:00
|
|
|
const data = await settingsLoader.loadSettings();
|
2021-09-27 17:34:15 +03:00
|
|
|
|
|
|
|
return calculateHash(JSON.stringify(data));
|
|
|
|
};
|
|
|
|
|
2021-09-28 10:47:42 +03:00
|
|
|
module.exports = {
|
|
|
|
getDefaultHash: getDefaultHash,
|
2021-09-23 19:31:13 +03:00
|
|
|
setFromFilePath: setFromFilePath,
|
2021-09-27 17:34:15 +03:00
|
|
|
get: get,
|
|
|
|
getCurrentHash: getCurrentHash
|
2021-09-23 19:31:13 +03:00
|
|
|
};
|