setting.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import fs from "node:fs";
  2. import errs from "../lib/error.js";
  3. import settingModel from "../models/setting.js";
  4. import internalNginx from "./nginx.js";
  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
  14. .can("settings:update", data.id)
  15. .then((/*access_data*/) => {
  16. return internalSetting.get(access, { id: data.id });
  17. })
  18. .then((row) => {
  19. if (row.id !== data.id) {
  20. // Sanity check that something crazy hasn't happened
  21. throw new errs.InternalValidationError(
  22. `Setting could not be updated, IDs do not match: ${row.id} !== ${data.id}`,
  23. );
  24. }
  25. return settingModel.query().where({ id: data.id }).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
  40. .deleteConfig("default")
  41. .then(() => {
  42. return internalNginx.generateConfig("default", row);
  43. })
  44. .then(() => {
  45. return internalNginx.test();
  46. })
  47. .then(() => {
  48. return internalNginx.reload();
  49. })
  50. .then(() => {
  51. return row;
  52. })
  53. .catch((/*err*/) => {
  54. internalNginx
  55. .deleteConfig("default")
  56. .then(() => {
  57. return internalNginx.test();
  58. })
  59. .then(() => {
  60. return internalNginx.reload();
  61. })
  62. .then(() => {
  63. // I'm being slack here I know..
  64. throw new errs.ValidationError("Could not reconfigure Nginx. Please check logs.");
  65. });
  66. });
  67. }
  68. return row;
  69. });
  70. },
  71. /**
  72. * @param {Access} access
  73. * @param {Object} data
  74. * @param {String} data.id
  75. * @return {Promise}
  76. */
  77. get: (access, data) => {
  78. return access
  79. .can("settings:get", data.id)
  80. .then(() => {
  81. return settingModel.query().where("id", data.id).first();
  82. })
  83. .then((row) => {
  84. if (row) {
  85. return row;
  86. }
  87. throw new errs.ItemNotFoundError(data.id);
  88. });
  89. },
  90. /**
  91. * This will only count the settings
  92. *
  93. * @param {Access} access
  94. * @returns {*}
  95. */
  96. getCount: (access) => {
  97. return access
  98. .can("settings:list")
  99. .then(() => {
  100. return settingModel.query().count("id as count").first();
  101. })
  102. .then((row) => {
  103. return Number.parseInt(row.count, 10);
  104. });
  105. },
  106. /**
  107. * All settings
  108. *
  109. * @param {Access} access
  110. * @returns {Promise}
  111. */
  112. getAll: (access) => {
  113. return access.can("settings:list").then(() => {
  114. return settingModel.query().orderBy("description", "ASC");
  115. });
  116. },
  117. };
  118. export default internalSetting;