checker-before-init.ts 4.5 KB

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