|
| 1 | +const fs = require('fs'); |
| 2 | +const error = require('../lib/error'); |
| 3 | +const settingModel = require('../models/setting'); |
| 4 | +const internalNginx = require('./nginx'); |
| 5 | + |
| 6 | +const internalSetting = { |
| 7 | + |
| 8 | + /** |
| 9 | + * @param {Access} access |
| 10 | + * @param {Object} data |
| 11 | + * @param {String} data.id |
| 12 | + * @return {Promise} |
| 13 | + */ |
| 14 | + update: (access, data) => { |
| 15 | + return access.can('settings:update', data.id) |
| 16 | + .then(access_data => { |
| 17 | + return internalSetting.get(access, {id: data.id}); |
| 18 | + }) |
| 19 | + .then(row => { |
| 20 | + if (row.id !== data.id) { |
| 21 | + // Sanity check that something crazy hasn't happened |
| 22 | + throw new error.InternalValidationError('Setting could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id); |
| 23 | + } |
| 24 | + |
| 25 | + return settingModel |
| 26 | + .query() |
| 27 | + .where({id: data.id}) |
| 28 | + .patch(data); |
| 29 | + }) |
| 30 | + .then(() => { |
| 31 | + return internalSetting.get(access, { |
| 32 | + id: data.id |
| 33 | + }); |
| 34 | + }) |
| 35 | + .then(row => { |
| 36 | + if (row.id === 'default-site') { |
| 37 | + // write the html if we need to |
| 38 | + if (row.value === 'html') { |
| 39 | + fs.writeFileSync('/data/nginx/default_www/index.html', row.meta.html, {encoding: 'utf8'}); |
| 40 | + } |
| 41 | + |
| 42 | + // Configure nginx |
| 43 | + return internalNginx.deleteConfig('default') |
| 44 | + .then(() => { |
| 45 | + return internalNginx.generateConfig('default', row); |
| 46 | + }) |
| 47 | + .then(() => { |
| 48 | + return internalNginx.test(); |
| 49 | + }) |
| 50 | + .then(() => { |
| 51 | + return internalNginx.reload(); |
| 52 | + }) |
| 53 | + .then(() => { |
| 54 | + return row; |
| 55 | + }) |
| 56 | + .catch((err) => { |
| 57 | + internalNginx.deleteConfig('default') |
| 58 | + .then(() => { |
| 59 | + return internalNginx.test(); |
| 60 | + }) |
| 61 | + .then(() => { |
| 62 | + return internalNginx.reload(); |
| 63 | + }) |
| 64 | + .then(() => { |
| 65 | + // I'm being slack here I know.. |
| 66 | + throw new error.ValidationError('Could not reconfigure Nginx. Please check logs.'); |
| 67 | + }) |
| 68 | + }); |
| 69 | + } else { |
| 70 | + return row; |
| 71 | + } |
| 72 | + }); |
| 73 | + }, |
| 74 | + |
| 75 | + /** |
| 76 | + * @param {Access} access |
| 77 | + * @param {Object} data |
| 78 | + * @param {String} data.id |
| 79 | + * @return {Promise} |
| 80 | + */ |
| 81 | + get: (access, data) => { |
| 82 | + return access.can('settings:get', data.id) |
| 83 | + .then(() => { |
| 84 | + return settingModel |
| 85 | + .query() |
| 86 | + .where('id', data.id) |
| 87 | + .first(); |
| 88 | + }) |
| 89 | + .then(row => { |
| 90 | + if (row) { |
| 91 | + return row; |
| 92 | + } else { |
| 93 | + throw new error.ItemNotFoundError(data.id); |
| 94 | + } |
| 95 | + }); |
| 96 | + }, |
| 97 | + |
| 98 | + /** |
| 99 | + * This will only count the settings |
| 100 | + * |
| 101 | + * @param {Access} access |
| 102 | + * @returns {*} |
| 103 | + */ |
| 104 | + getCount: (access) => { |
| 105 | + return access.can('settings:list') |
| 106 | + .then(() => { |
| 107 | + return settingModel |
| 108 | + .query() |
| 109 | + .count('id as count') |
| 110 | + .first(); |
| 111 | + }) |
| 112 | + .then(row => { |
| 113 | + return parseInt(row.count, 10); |
| 114 | + }); |
| 115 | + }, |
| 116 | + |
| 117 | + /** |
| 118 | + * All settings |
| 119 | + * |
| 120 | + * @param {Access} access |
| 121 | + * @returns {Promise} |
| 122 | + */ |
| 123 | + getAll: (access) => { |
| 124 | + return access.can('settings:list') |
| 125 | + .then(() => { |
| 126 | + return settingModel |
| 127 | + .query() |
| 128 | + .orderBy('description', 'ASC'); |
| 129 | + }); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +module.exports = internalSetting; |
0 commit comments