Mirror of PeerTube
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

checker-before-init.ts 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import * as config from 'config'
  2. import { promisify0 } from '../helpers/core-utils'
  3. import { logger } from '../helpers/logger'
  4. // ONLY USE CORE MODULES IN THIS FILE!
  5. // Check the config files
  6. function checkMissedConfig () {
  7. const required = [ 'listen.port', 'listen.hostname',
  8. 'webserver.https', 'webserver.hostname', 'webserver.port',
  9. 'trust_proxy',
  10. 'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password', 'database.pool.max',
  11. 'smtp.hostname', 'smtp.port', 'smtp.username', 'smtp.password', 'smtp.tls', 'smtp.from_address',
  12. 'email.body.signature', 'email.subject.prefix',
  13. 'storage.avatars', 'storage.videos', 'storage.logs', 'storage.previews', 'storage.thumbnails', 'storage.torrents', 'storage.cache',
  14. 'storage.redundancy', 'storage.tmp', 'storage.streaming_playlists', 'storage.plugins',
  15. 'log.level',
  16. 'user.video_quota', 'user.video_quota_daily',
  17. 'csp.enabled', 'csp.report_only', 'csp.report_uri',
  18. 'cache.previews.size', 'admin.email', 'contact_form.enabled',
  19. 'signup.enabled', 'signup.limit', 'signup.requires_email_verification',
  20. 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist',
  21. 'redundancy.videos.strategies', 'redundancy.videos.check_interval',
  22. 'transcoding.enabled', 'transcoding.threads', 'transcoding.allow_additional_extensions',
  23. 'import.videos.http.enabled', 'import.videos.torrent.enabled', 'auto_blacklist.videos.of_users.enabled',
  24. 'trending.videos.interval_days',
  25. 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
  26. 'instance.is_nsfw', 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt',
  27. 'services.twitter.username', 'services.twitter.whitelisted',
  28. 'followers.instance.enabled', 'followers.instance.manual_approval',
  29. 'tracker.enabled', 'tracker.private', 'tracker.reject_too_many_announces',
  30. 'history.videos.max_age', 'views.videos.remote.max_age',
  31. 'rates_limit.login.window', 'rates_limit.login.max', 'rates_limit.ask_send_email.window', 'rates_limit.ask_send_email.max',
  32. 'theme.default'
  33. ]
  34. const requiredAlternatives = [
  35. [ // set
  36. ['redis.hostname', 'redis.port'], // alternative
  37. ['redis.socket']
  38. ]
  39. ]
  40. const miss: string[] = []
  41. for (const key of required) {
  42. if (!config.has(key)) {
  43. miss.push(key)
  44. }
  45. }
  46. const redundancyVideos = config.get<any>('redundancy.videos.strategies')
  47. if (Array.isArray(redundancyVideos)) {
  48. for (const r of redundancyVideos) {
  49. if (!r.size) miss.push('redundancy.videos.strategies.size')
  50. if (!r.min_lifetime) miss.push('redundancy.videos.strategies.min_lifetime')
  51. }
  52. }
  53. const missingAlternatives = requiredAlternatives.filter(
  54. set => !set.find(alternative => !alternative.find(key => !config.has(key)))
  55. )
  56. missingAlternatives
  57. .forEach(set => set[0].forEach(key => miss.push(key)))
  58. return miss
  59. }
  60. // Check the available codecs
  61. // We get CONFIG by param to not import it in this file (import orders)
  62. async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
  63. const Ffmpeg = require('fluent-ffmpeg')
  64. const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
  65. const codecs = await getAvailableCodecsPromise()
  66. const canEncode = [ 'libx264' ]
  67. if (CONFIG.TRANSCODING.ENABLED === false) return undefined
  68. for (const codec of canEncode) {
  69. if (codecs[codec] === undefined) {
  70. throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
  71. }
  72. if (codecs[codec].canEncode !== true) {
  73. throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
  74. }
  75. }
  76. return checkFFmpegEncoders()
  77. }
  78. // Optional encoders, if present, can be used to improve transcoding
  79. // Here we ask ffmpeg if it detects their presence on the system, so that we can later use them
  80. let supportedOptionalEncoders: Map<string, boolean>
  81. async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
  82. if (supportedOptionalEncoders !== undefined) {
  83. return supportedOptionalEncoders
  84. }
  85. const Ffmpeg = require('fluent-ffmpeg')
  86. const getAvailableEncodersPromise = promisify0(Ffmpeg.getAvailableEncoders)
  87. const encoders = await getAvailableEncodersPromise()
  88. const optionalEncoders = [ 'libfdk_aac' ]
  89. supportedOptionalEncoders = new Map<string, boolean>()
  90. for (const encoder of optionalEncoders) {
  91. supportedOptionalEncoders.set(encoder, encoders[encoder] !== undefined)
  92. }
  93. return supportedOptionalEncoders
  94. }
  95. function checkNodeVersion () {
  96. const v = process.version
  97. const majorString = v.split('.')[0].replace('v', '')
  98. const major = parseInt(majorString, 10)
  99. logger.debug('Checking NodeJS version %s.', v)
  100. if (major < 10) {
  101. logger.warn('Your NodeJS version %s is deprecated. Please use Node 10.', v)
  102. }
  103. }
  104. // ---------------------------------------------------------------------------
  105. export {
  106. checkFFmpeg,
  107. checkFFmpegEncoders,
  108. checkMissedConfig,
  109. checkNodeVersion
  110. }