0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/api/v0.1/themes.js
Naz Gargol f3ec2fb2f7
Cleaned up theme service (#10884)
refs #10790

- Following TODO in theme index file was waiting for 2 years, and today is the day to cross it out:
- "Reduced the amount of things we expose to the outside world"
- "Made this a nice clean sensible API we can all understand!" - by @ErisDS
- Cleaned exposed methods from themes module
- Removed unused storage getter
- Removed list method
- Removed validate method
- Renamed Storage to ThemeStorage
  - Named the file the same way the class defined inside of it is named
  - Naming was conflicting with coming rename of  `settings` -> `storage`
- Renamed theme settings to storage
2019-07-09 16:35:18 +02:00

111 lines
3.2 KiB
JavaScript

// # Themes API
// RESTful API for Themes
const localUtils = require('./utils'),
common = require('../../lib/common'),
models = require('../../models'),
themeService = require('../../../frontend/services/themes');
let themes;
/**
* ## Themes API Methods
*
* **See:** [API Methods](constants.js.html#api%20methods)
*/
themes = {
/**
* Every role can browse all themes. The response contains a list of all available themes in your content folder.
* The active theme get's marked as `active:true` and contains an extra object `templates`, which
* contains the custom templates of the active theme. These custom templates are used to show a dropdown
* in the PSM to be able to choose a custom post template.
*/
browse(options) {
return localUtils
// Permissions
.handlePermissions('themes', 'browse')(options)
// Main action
.then(() => {
// Return JSON result
return themeService.getJSON();
});
},
activate(options) {
let themeName = options.name,
newSettings = [{
key: 'active_theme',
value: themeName
}];
return localUtils
// Permissions
.handlePermissions('themes', 'activate')(options)
// Validation & activation
.then(() => {
return themeService.activate(themeName);
})
// Update setting
.then((checkedTheme) => {
// @NOTE: We use the model, not the API here, as we don't want to trigger permissions
return models.Settings.edit(newSettings, options)
.then(() => checkedTheme);
})
.then((checkedTheme) => {
// Return JSON result
return themeService.getJSON(themeName, checkedTheme);
});
},
upload(options) {
options = options || {};
// consistent filename uploads
options.originalname = options.originalname.toLowerCase();
let zip = {
path: options.path,
name: options.originalname
};
return localUtils
// Permissions
.handlePermissions('themes', 'add')(options)
// Validation
.then(() => {
return themeService.storage.setFromZip(zip);
})
.then((theme) => {
common.events.emit('theme.uploaded');
return theme;
});
},
download(options) {
let themeName = options.name;
return localUtils
// Permissions
.handlePermissions('themes', 'read')(options)
.then(() => {
return themeService.storage.getZip(themeName);
});
},
/**
* remove theme zip
* remove theme folder
*/
destroy(options) {
let themeName = options.name;
return localUtils
// Permissions
.handlePermissions('themes', 'destroy')(options)
// Validation
.then(() => {
return themeService.storage.destroy(themeName);
});
}
};
module.exports = themes;