2021-07-06 13:02:37 +03:00
|
|
|
const debug = require('@tryghost/debug')('themes');
|
2019-07-01 17:56:23 +03:00
|
|
|
const fs = require('fs-extra');
|
2021-07-07 13:51:04 +03:00
|
|
|
const ObjectID = require('bson-objectid');
|
|
|
|
|
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
const logging = require('@tryghost/logging');
|
|
|
|
const errors = require('@tryghost/errors');
|
2019-07-01 17:56:23 +03:00
|
|
|
|
|
|
|
const validate = require('./validate');
|
|
|
|
const list = require('./list');
|
2019-07-09 17:35:18 +03:00
|
|
|
const ThemeStorage = require('./ThemeStorage');
|
2019-07-01 17:56:23 +03:00
|
|
|
const themeLoader = require('./loader');
|
2021-07-07 15:58:47 +03:00
|
|
|
const activator = require('./activation-bridge');
|
2019-07-01 17:56:23 +03:00
|
|
|
const toJSON = require('./to-json');
|
|
|
|
|
2021-06-30 16:56:57 +03:00
|
|
|
const settingsCache = require('../../../shared/settings-cache');
|
2019-07-01 17:56:23 +03:00
|
|
|
|
2021-07-06 17:38:28 +03:00
|
|
|
const messages = {
|
|
|
|
themeDoesNotExist: 'Theme does not exist.',
|
|
|
|
invalidThemeName: 'Please select a valid theme.',
|
|
|
|
overrideCasper: 'Please rename your zip, it\'s not allowed to override the default casper theme.',
|
|
|
|
destroyCasper: 'Deleting the default casper theme is not allowed.',
|
|
|
|
destroyActive: 'Deleting the active theme is not allowed.'
|
|
|
|
};
|
|
|
|
|
2019-07-01 17:56:23 +03:00
|
|
|
let themeStorage;
|
|
|
|
|
|
|
|
const getStorage = () => {
|
2019-07-09 17:35:18 +03:00
|
|
|
themeStorage = themeStorage || new ThemeStorage();
|
2019-07-01 17:56:23 +03:00
|
|
|
|
|
|
|
return themeStorage;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2021-07-07 13:51:04 +03:00
|
|
|
getZip: async (themeName) => {
|
2019-07-01 17:56:23 +03:00
|
|
|
const theme = list.get(themeName);
|
|
|
|
|
|
|
|
if (!theme) {
|
2021-07-07 13:51:04 +03:00
|
|
|
throw new errors.BadRequestError({
|
2021-07-06 17:38:28 +03:00
|
|
|
message: tpl(messages.invalidThemeName)
|
2021-07-07 13:51:04 +03:00
|
|
|
});
|
2019-07-01 17:56:23 +03:00
|
|
|
}
|
|
|
|
|
2021-07-07 13:51:04 +03:00
|
|
|
return await getStorage().serve({
|
2019-07-01 17:56:23 +03:00
|
|
|
name: themeName
|
|
|
|
});
|
|
|
|
},
|
2021-07-07 13:51:04 +03:00
|
|
|
setFromZip: async (zip) => {
|
2021-07-07 16:14:20 +03:00
|
|
|
const themeName = getStorage().getSanitizedFileName(zip.name.split('.zip')[0]);
|
|
|
|
const backupName = `${themeName}_${ObjectID()}`;
|
2019-07-01 17:56:23 +03:00
|
|
|
|
|
|
|
// check if zip name is casper.zip
|
|
|
|
if (zip.name === 'casper.zip') {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.ValidationError({
|
2021-07-06 17:38:28 +03:00
|
|
|
message: tpl(messages.overrideCasper)
|
2019-07-01 17:56:23 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let checkedTheme;
|
2021-07-07 13:51:04 +03:00
|
|
|
let overrideTheme;
|
2021-02-23 11:49:48 +03:00
|
|
|
let renamedExisting = false;
|
2019-07-01 17:56:23 +03:00
|
|
|
|
2021-07-07 13:51:04 +03:00
|
|
|
try {
|
2021-07-07 16:14:20 +03:00
|
|
|
checkedTheme = await validate.checkSafe(themeName, zip, true);
|
|
|
|
const themeExists = await getStorage().exists(themeName);
|
2021-07-07 13:51:04 +03:00
|
|
|
// CASE: move the existing theme to a backup folder
|
|
|
|
if (themeExists) {
|
2021-07-07 16:14:20 +03:00
|
|
|
debug('setFromZip Theme exists already');
|
2021-07-07 13:51:04 +03:00
|
|
|
renamedExisting = true;
|
2021-07-07 16:14:20 +03:00
|
|
|
await getStorage().rename(themeName, backupName);
|
2021-07-07 13:51:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: store extracted theme
|
|
|
|
await getStorage().save({
|
2021-07-07 16:14:20 +03:00
|
|
|
name: themeName,
|
2021-07-07 13:51:04 +03:00
|
|
|
path: checkedTheme.path
|
|
|
|
});
|
2021-07-11 14:38:37 +03:00
|
|
|
|
2021-07-07 13:51:04 +03:00
|
|
|
// CASE: loads the theme from the fs & sets the theme on the themeList
|
2021-07-07 16:14:20 +03:00
|
|
|
const loadedTheme = await themeLoader.loadOneTheme(themeName);
|
|
|
|
overrideTheme = (themeName === settingsCache.get('active_theme'));
|
2021-07-11 14:38:37 +03:00
|
|
|
|
2021-07-07 13:51:04 +03:00
|
|
|
// CASE: if this is the active theme, we are overriding
|
|
|
|
if (overrideTheme) {
|
2021-07-11 14:38:37 +03:00
|
|
|
debug('setFromZip Theme is active already');
|
2021-10-21 17:02:42 +03:00
|
|
|
await activator.activateFromAPIOverride(themeName, loadedTheme, checkedTheme);
|
2021-07-07 13:51:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: unify the name across gscan and Ghost!
|
|
|
|
return {
|
|
|
|
themeOverridden: overrideTheme,
|
2021-07-07 16:14:20 +03:00
|
|
|
theme: toJSON(themeName, checkedTheme)
|
2021-07-07 13:51:04 +03:00
|
|
|
};
|
|
|
|
} catch (error) {
|
|
|
|
// restore backup if we renamed an existing theme but saving failed
|
|
|
|
if (renamedExisting) {
|
2021-07-07 16:14:20 +03:00
|
|
|
return getStorage().exists(themeName).then((themeExists) => {
|
2021-07-07 13:51:04 +03:00
|
|
|
if (!themeExists) {
|
2021-07-07 16:14:20 +03:00
|
|
|
return getStorage().rename(backupName, themeName).then(() => {
|
2021-07-07 13:51:04 +03:00
|
|
|
throw error;
|
2019-07-01 17:56:23 +03:00
|
|
|
});
|
2021-07-07 13:51:04 +03:00
|
|
|
}
|
2021-07-09 21:27:49 +03:00
|
|
|
|
|
|
|
throw error;
|
2021-07-07 13:51:04 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
// @TODO: we should probably do this as part of saving the theme
|
|
|
|
// CASE: remove extracted dir from gscan happens in background
|
|
|
|
if (checkedTheme) {
|
|
|
|
fs.remove(checkedTheme.path)
|
2020-06-03 18:29:34 +03:00
|
|
|
.catch((err) => {
|
|
|
|
logging.error(new errors.GhostError({err: err}));
|
|
|
|
});
|
2021-07-07 13:51:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: remove the backup we created earlier
|
|
|
|
getStorage()
|
|
|
|
.delete(backupName)
|
|
|
|
.catch((err) => {
|
|
|
|
logging.error(new errors.GhostError({err: err}));
|
|
|
|
});
|
|
|
|
}
|
2019-07-01 17:56:23 +03:00
|
|
|
},
|
2021-07-07 13:51:04 +03:00
|
|
|
destroy: async function (themeName) {
|
2019-07-01 17:56:23 +03:00
|
|
|
if (themeName === 'casper') {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.ValidationError({
|
2021-07-06 17:38:28 +03:00
|
|
|
message: tpl(messages.destroyCasper)
|
2019-07-01 17:56:23 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (themeName === settingsCache.get('active_theme')) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.ValidationError({
|
2021-07-06 17:38:28 +03:00
|
|
|
message: tpl(messages.destroyActive)
|
2019-07-01 17:56:23 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const theme = list.get(themeName);
|
|
|
|
|
|
|
|
if (!theme) {
|
2020-05-22 21:22:20 +03:00
|
|
|
throw new errors.NotFoundError({
|
2021-07-06 17:38:28 +03:00
|
|
|
message: tpl(messages.themeDoesNotExist)
|
2019-07-01 17:56:23 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-07 13:51:04 +03:00
|
|
|
let result = await getStorage().delete(themeName);
|
|
|
|
list.del(themeName);
|
|
|
|
return result;
|
2019-07-01 17:56:23 +03:00
|
|
|
}
|
|
|
|
};
|