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