setting.js 2.9 KB

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