config.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import express from 'express'
  2. import { body } from 'express-validator'
  3. import { isIntOrNull } from '@server/helpers/custom-validators/misc'
  4. import { CONFIG, isEmailEnabled } from '@server/initializers/config'
  5. import { HttpStatusCode } from '@shared/models/http/http-error-codes'
  6. import { CustomConfig } from '../../../shared/models/server/custom-config.model'
  7. import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
  8. import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users'
  9. import { isThemeRegistered } from '../../lib/plugins/theme-utils'
  10. import { areValidationErrors } from './shared'
  11. const customConfigUpdateValidator = [
  12. body('instance.name').exists(),
  13. body('instance.shortDescription').exists(),
  14. body('instance.description').exists(),
  15. body('instance.terms').exists(),
  16. body('instance.defaultNSFWPolicy').custom(isUserNSFWPolicyValid),
  17. body('instance.defaultClientRoute').exists(),
  18. body('instance.customizations.css').exists(),
  19. body('instance.customizations.javascript').exists(),
  20. body('services.twitter.username').exists(),
  21. body('services.twitter.whitelisted').isBoolean(),
  22. body('cache.previews.size').isInt(),
  23. body('cache.captions.size').isInt(),
  24. body('cache.torrents.size').isInt(),
  25. body('signup.enabled').isBoolean(),
  26. body('signup.limit').isInt(),
  27. body('signup.requiresEmailVerification').isBoolean(),
  28. body('signup.requiresApproval').isBoolean(),
  29. body('signup.minimumAge').isInt(),
  30. body('admin.email').isEmail(),
  31. body('contactForm.enabled').isBoolean(),
  32. body('user.videoQuota').custom(isUserVideoQuotaValid),
  33. body('user.videoQuotaDaily').custom(isUserVideoQuotaDailyValid),
  34. body('videoChannels.maxPerUser').isInt(),
  35. body('transcoding.enabled').isBoolean(),
  36. body('transcoding.allowAdditionalExtensions').isBoolean(),
  37. body('transcoding.threads').isInt(),
  38. body('transcoding.concurrency').isInt({ min: 1 }),
  39. body('transcoding.resolutions.0p').isBoolean(),
  40. body('transcoding.resolutions.144p').isBoolean(),
  41. body('transcoding.resolutions.240p').isBoolean(),
  42. body('transcoding.resolutions.360p').isBoolean(),
  43. body('transcoding.resolutions.480p').isBoolean(),
  44. body('transcoding.resolutions.720p').isBoolean(),
  45. body('transcoding.resolutions.1080p').isBoolean(),
  46. body('transcoding.resolutions.1440p').isBoolean(),
  47. body('transcoding.resolutions.2160p').isBoolean(),
  48. body('transcoding.alwaysTranscodeOriginalResolution').isBoolean(),
  49. body('transcoding.webtorrent.enabled').isBoolean(),
  50. body('transcoding.hls.enabled').isBoolean(),
  51. body('videoStudio.enabled').isBoolean(),
  52. body('import.videos.concurrency').isInt({ min: 0 }),
  53. body('import.videos.http.enabled').isBoolean(),
  54. body('import.videos.torrent.enabled').isBoolean(),
  55. body('import.videoChannelSynchronization.enabled').isBoolean(),
  56. body('trending.videos.algorithms.default').exists(),
  57. body('trending.videos.algorithms.enabled').exists(),
  58. body('followers.instance.enabled').isBoolean(),
  59. body('followers.instance.manualApproval').isBoolean(),
  60. body('theme.default').custom(v => isThemeNameValid(v) && isThemeRegistered(v)),
  61. body('broadcastMessage.enabled').isBoolean(),
  62. body('broadcastMessage.message').exists(),
  63. body('broadcastMessage.level').exists(),
  64. body('broadcastMessage.dismissable').isBoolean(),
  65. body('live.enabled').isBoolean(),
  66. body('live.allowReplay').isBoolean(),
  67. body('live.maxDuration').isInt(),
  68. body('live.maxInstanceLives').custom(isIntOrNull),
  69. body('live.maxUserLives').custom(isIntOrNull),
  70. body('live.transcoding.enabled').isBoolean(),
  71. body('live.transcoding.threads').isInt(),
  72. body('live.transcoding.resolutions.144p').isBoolean(),
  73. body('live.transcoding.resolutions.240p').isBoolean(),
  74. body('live.transcoding.resolutions.360p').isBoolean(),
  75. body('live.transcoding.resolutions.480p').isBoolean(),
  76. body('live.transcoding.resolutions.720p').isBoolean(),
  77. body('live.transcoding.resolutions.1080p').isBoolean(),
  78. body('live.transcoding.resolutions.1440p').isBoolean(),
  79. body('live.transcoding.resolutions.2160p').isBoolean(),
  80. body('live.transcoding.alwaysTranscodeOriginalResolution').isBoolean(),
  81. body('search.remoteUri.users').isBoolean(),
  82. body('search.remoteUri.anonymous').isBoolean(),
  83. body('search.searchIndex.enabled').isBoolean(),
  84. body('search.searchIndex.url').exists(),
  85. body('search.searchIndex.disableLocalSearch').isBoolean(),
  86. body('search.searchIndex.isDefaultSearch').isBoolean(),
  87. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  88. if (areValidationErrors(req, res)) return
  89. if (!checkInvalidConfigIfEmailDisabled(req.body, res)) return
  90. if (!checkInvalidTranscodingConfig(req.body, res)) return
  91. if (!checkInvalidSynchronizationConfig(req.body, res)) return
  92. if (!checkInvalidLiveConfig(req.body, res)) return
  93. if (!checkInvalidVideoStudioConfig(req.body, res)) return
  94. return next()
  95. }
  96. ]
  97. function ensureConfigIsEditable (req: express.Request, res: express.Response, next: express.NextFunction) {
  98. if (!CONFIG.WEBADMIN.CONFIGURATION.EDITION.ALLOWED) {
  99. return res.fail({
  100. status: HttpStatusCode.METHOD_NOT_ALLOWED_405,
  101. message: 'Server configuration is static and cannot be edited'
  102. })
  103. }
  104. return next()
  105. }
  106. // ---------------------------------------------------------------------------
  107. export {
  108. customConfigUpdateValidator,
  109. ensureConfigIsEditable
  110. }
  111. function checkInvalidConfigIfEmailDisabled (customConfig: CustomConfig, res: express.Response) {
  112. if (isEmailEnabled()) return true
  113. if (customConfig.signup.requiresEmailVerification === true) {
  114. res.fail({ message: 'Emailer is disabled but you require signup email verification.' })
  115. return false
  116. }
  117. return true
  118. }
  119. function checkInvalidTranscodingConfig (customConfig: CustomConfig, res: express.Response) {
  120. if (customConfig.transcoding.enabled === false) return true
  121. if (customConfig.transcoding.webtorrent.enabled === false && customConfig.transcoding.hls.enabled === false) {
  122. res.fail({ message: 'You need to enable at least webtorrent transcoding or hls transcoding' })
  123. return false
  124. }
  125. return true
  126. }
  127. function checkInvalidSynchronizationConfig (customConfig: CustomConfig, res: express.Response) {
  128. if (customConfig.import.videoChannelSynchronization.enabled && !customConfig.import.videos.http.enabled) {
  129. res.fail({ message: 'You need to enable HTTP video import in order to enable channel synchronization' })
  130. return false
  131. }
  132. return true
  133. }
  134. function checkInvalidLiveConfig (customConfig: CustomConfig, res: express.Response) {
  135. if (customConfig.live.enabled === false) return true
  136. if (customConfig.live.allowReplay === true && customConfig.transcoding.enabled === false) {
  137. res.fail({ message: 'You cannot allow live replay if transcoding is not enabled' })
  138. return false
  139. }
  140. return true
  141. }
  142. function checkInvalidVideoStudioConfig (customConfig: CustomConfig, res: express.Response) {
  143. if (customConfig.videoStudio.enabled === false) return true
  144. if (customConfig.videoStudio.enabled === true && customConfig.transcoding.enabled === false) {
  145. res.fail({ message: 'You cannot enable video studio if transcoding is not enabled' })
  146. return false
  147. }
  148. return true
  149. }