2021-01-21 11:57:52 +03:00
|
|
|
const {events} = require('../../lib/common');
|
|
|
|
const themeService = require('../../../frontend/services/themes');
|
2021-03-03 15:47:09 +03:00
|
|
|
const limitService = require('../../services/limits');
|
2021-01-21 11:57:52 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'themes',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
permissions: true,
|
|
|
|
query() {
|
|
|
|
return themeService.getJSON();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
activate: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'name'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
name: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
let themeName = frame.options.name;
|
|
|
|
const newSettings = [{
|
|
|
|
key: 'active_theme',
|
|
|
|
value: themeName
|
|
|
|
}];
|
|
|
|
|
|
|
|
return themeService.activate(themeName)
|
|
|
|
.then((checkedTheme) => {
|
|
|
|
// @NOTE: we use the model, not the API here, as we don't want to trigger permissions
|
|
|
|
return models.Settings.edit(newSettings, frame.options)
|
|
|
|
.then(() => checkedTheme);
|
|
|
|
})
|
|
|
|
.then((checkedTheme) => {
|
|
|
|
return themeService.getJSON(themeName, checkedTheme);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
upload: {
|
|
|
|
headers: {},
|
|
|
|
permissions: {
|
|
|
|
method: 'add'
|
|
|
|
},
|
2021-03-03 15:47:09 +03:00
|
|
|
async query(frame) {
|
2021-03-04 14:34:11 +03:00
|
|
|
if (limitService.isLimited('customThemes')) {
|
|
|
|
await limitService.errorIfWouldGoOverLimit('customThemes');
|
2021-03-03 15:47:09 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 11:57:52 +03:00
|
|
|
// @NOTE: consistent filename uploads
|
|
|
|
frame.options.originalname = frame.file.originalname.toLowerCase();
|
|
|
|
|
|
|
|
let zip = {
|
|
|
|
path: frame.file.path,
|
|
|
|
name: frame.file.originalname
|
|
|
|
};
|
|
|
|
|
|
|
|
return themeService.storage.setFromZip(zip)
|
|
|
|
.then(({theme, themeOverridden}) => {
|
|
|
|
if (themeOverridden) {
|
|
|
|
// CASE: clear cache
|
|
|
|
this.headers.cacheInvalidate = true;
|
|
|
|
}
|
|
|
|
events.emit('theme.uploaded');
|
|
|
|
return theme;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
download: {
|
|
|
|
options: [
|
|
|
|
'name'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
name: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'read'
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
let themeName = frame.options.name;
|
|
|
|
|
|
|
|
return themeService.storage.getZip(themeName);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'name'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
name: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
let themeName = frame.options.name;
|
|
|
|
|
|
|
|
return themeService.storage.destroy(themeName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|