settings.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { readFileSync } from 'fs';
  2. import { ServerSettings } from '../database/models.js';
  3. export const Settings = async (req, res) => {
  4. let settings = readFileSync('views/partials/settings.html', 'utf8');
  5. let links = await ServerSettings.findOne({ where: {key: 'links'}});
  6. try {
  7. if (links.value != 'localhost' && links.value != '') {
  8. settings = settings.replaceAll('data-LinkMode', 'checked');
  9. settings = settings.replaceAll('data-LinkValue', `value="${links.value}"`);
  10. }
  11. } catch {
  12. console.log(`Container Links: No Value Set`)
  13. }
  14. let registration = await ServerSettings.findOne({ where: {key: 'registration'}});
  15. try {
  16. if (registration.value != 'off' && registration.value != '') {
  17. settings = settings.replaceAll('data-UserReg', 'checked');
  18. settings = settings.replaceAll('data-Passphrase', `value="${registration.value}"`);
  19. }
  20. } catch {
  21. console.log(`User Registration: No Value Set`);
  22. }
  23. async function hostInfo(host) {
  24. let info = await ServerSettings.findOne({ where: {key: host}});
  25. try {
  26. if (info.value != 'off' && info.value != '') {
  27. let values = info.value.split(',');
  28. return { tag: values[0], ip: values[1], port: values[2] };
  29. }
  30. } catch {
  31. console.log(`${host}: No Value Set`);
  32. }
  33. }
  34. let host2 = await hostInfo('host2');
  35. if (host2) {
  36. settings = settings.replaceAll('data-Host2', 'checked');
  37. settings = settings.replaceAll('data-Tag2', `value="${host2.tag}"`);
  38. settings = settings.replaceAll('data-Ip2', `value="${host2.ip}"`);
  39. settings = settings.replaceAll('data-Port2', `value="${host2.port}"`);
  40. }
  41. let host3 = await hostInfo('host3');
  42. if (host3) {
  43. settings = settings.replaceAll('data-Host3', 'checked');
  44. settings = settings.replaceAll('data-Tag3', `value="${host3.tag}"`);
  45. settings = settings.replaceAll('data-Ip3', `value="${host3.ip}"`);
  46. settings = settings.replaceAll('data-Port3', `value="${host3.port}"`);
  47. }
  48. let host4 = await hostInfo('host4');
  49. if (host4) {
  50. settings = settings.replaceAll('data-Host4', 'checked');
  51. settings = settings.replaceAll('data-Tag4', `value="${host4.tag}"`);
  52. settings = settings.replaceAll('data-Ip4', `value="${host4.ip}"`);
  53. settings = settings.replaceAll('data-Port4', `value="${host4.port}"`);
  54. }
  55. res.render("settings", {
  56. username: req.session.username,
  57. role: req.session.role,
  58. avatar: req.session.username.charAt(0).toUpperCase(),
  59. alert: '',
  60. settings: settings,
  61. link1: '',
  62. link2: '',
  63. link3: '',
  64. link4: '',
  65. link5: '',
  66. link6: '',
  67. link7: '',
  68. link8: '',
  69. link9: '',
  70. });
  71. }
  72. export const updateSettings = async (req, res) => {
  73. let trigger = req.header('hx-trigger');
  74. if (trigger == 'updated') {
  75. let update = `<button class="btn btn-primary" id="submit" hx-trigger="click" hx-post="/settings" hx-swap="outerHTML" hx-target="#submit">
  76. Update
  77. </button>`
  78. res.send(update);
  79. return;
  80. }
  81. // Container links
  82. let { link_mode, link } = req.body;
  83. if (link_mode) {
  84. let exists = await ServerSettings.findOne({ where: {key: 'links'}});
  85. if (exists) {
  86. const setting = await ServerSettings.update({value: link}, {where: {key: 'links'}});
  87. } else {
  88. const newSetting = await ServerSettings.create({ key: 'links', value: link});
  89. }
  90. console.log('Custom links on');
  91. } else if (!link_mode) {
  92. let exists = await ServerSettings.findOne({ where: {key: 'links'}});
  93. if (exists) {
  94. const setting = await ServerSettings.update({value: 'localhost'}, {where: {key: 'links'}});
  95. }
  96. console.log('Custom links off');
  97. }
  98. // User registration
  99. let { user_registration, passphrase} = req.body;
  100. if (user_registration) {
  101. let exists = await ServerSettings.findOne({ where: {key: 'registration'}});
  102. if (exists) {
  103. const setting = await ServerSettings.update({value: passphrase}, {where: {key: 'registration'}});
  104. } else {
  105. const newSetting = await ServerSettings.create({ key: 'registration', value: passphrase});
  106. }
  107. console.log('registration on');
  108. } else if (!user_registration) {
  109. let exists = await ServerSettings.findOne({ where: {key: 'registration'}});
  110. if (exists) {
  111. const setting = await ServerSettings.update({value: 'off'}, {where: {key: 'registration'}});
  112. }
  113. console.log('registration off');
  114. }
  115. // Host 2
  116. let { host2, tag2, ip2, port2 } = req.body;
  117. if (host2) {
  118. let exists = await ServerSettings.findOne({ where: {key: 'host2'}});
  119. if (exists) {
  120. const setting = await ServerSettings.update({value: `${tag2},${ip2},${port2}`}, {where: {key: 'host2'}});
  121. } else {
  122. const newSetting = await ServerSettings.create({ key: 'host2', value: `${tag2},${ip2},${port2}`});
  123. }
  124. console.log('host2 on');
  125. } else if (!host2) {
  126. let exists = await ServerSettings.findOne({ where: {key: 'host2'}});
  127. if (exists) {
  128. const setting = await ServerSettings.update({value: 'off'}, {where: {key: 'host2'}});
  129. }
  130. console.log('host2 off');
  131. }
  132. // Host 3
  133. let { host3, tag3, ip3, port3 } = req.body;
  134. if (host3) {
  135. let exists = await ServerSettings.findOne({ where: {key: 'host3'}});
  136. if (exists) {
  137. const setting = await ServerSettings.update({value: `${tag3},${ip3},${port3}`}, {where: {key: 'host3'}});
  138. } else {
  139. const newSetting = await ServerSettings.create({ key: 'host3', value: `${tag3},${ip3},${port3}`});
  140. }
  141. console.log('host3 on');
  142. } else if (!host3) {
  143. let exists = await ServerSettings.findOne({ where: {key: 'host3'}});
  144. if (exists) {
  145. const setting = await ServerSettings.update({value: 'off'}, {where: {key: 'host3'}});
  146. }
  147. console.log('host3 off');
  148. }
  149. // Host 4
  150. let { host4, tag4, ip4, port4 } = req.body;
  151. if (host4) {
  152. let exists = await ServerSettings.findOne({ where: {key: 'host4'}});
  153. if (exists) {
  154. const setting = await ServerSettings.update({value: `${tag4},${ip4},${port4}`}, {where: {key: 'host4'}});
  155. } else {
  156. const newSetting = await ServerSettings.create({ key: 'host4', value: `${tag4},${ip4},${port4}`});
  157. }
  158. console.log('host4 on');
  159. } else if (!host4) {
  160. let exists = await ServerSettings.findOne({ where: {key: 'host4'}});
  161. if (exists) {
  162. const setting = await ServerSettings.update({value: 'off'}, {where: {key: 'host4'}});
  163. }
  164. console.log('host4 off');
  165. }
  166. let success = `<button class="btn btn-success" id="updated" hx-trigger="load delay:2s" hx-post="/settings" hx-swap="outerHTML" hx-target="#updated">
  167. Update
  168. </button>`
  169. res.send(success);
  170. }