config.ts 7.6 KB

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