3095913a3209f79b49a9b4a85a48753cf3bee50e
[oweals/peertube.git] / server / initializers / checker-before-init.ts
1 import * as config from 'config'
2 import { promisify0 } from '../helpers/core-utils'
3
4 // ONLY USE CORE MODULES IN THIS FILE!
5
6 // Check the config files
7 function checkMissedConfig () {
8   const required = [ 'listen.port', 'listen.hostname',
9     'webserver.https', 'webserver.hostname', 'webserver.port',
10     'trust_proxy',
11     'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password', 'database.pool.max',
12     'smtp.hostname', 'smtp.port', 'smtp.username', 'smtp.password', 'smtp.tls', 'smtp.from_address',
13     'storage.avatars', 'storage.videos', 'storage.logs', 'storage.previews', 'storage.thumbnails', 'storage.torrents', 'storage.cache',
14     'storage.redundancy', 'storage.tmp', 'storage.streaming_playlists',
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   ]
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
46   if (Array.isArray(redundancyVideos)) {
47     for (const r of redundancyVideos) {
48       if (!r.size) miss.push('redundancy.videos.strategies.size')
49       if (!r.min_lifetime) miss.push('redundancy.videos.strategies.min_lifetime')
50     }
51   }
52
53   const missingAlternatives = requiredAlternatives.filter(
54     set => !set.find(alternative => !alternative.find(key => !config.has(key)))
55   )
56
57   missingAlternatives
58     .forEach(set => set[0].forEach(key => miss.push(key)))
59
60   return miss
61 }
62
63 // Check the available codecs
64 // We get CONFIG by param to not import it in this file (import orders)
65 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
66   const Ffmpeg = require('fluent-ffmpeg')
67   const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
68   const codecs = await getAvailableCodecsPromise()
69   const canEncode = [ 'libx264' ]
70
71   if (CONFIG.TRANSCODING.ENABLED === false) return undefined
72
73   for (const codec of canEncode) {
74     if (codecs[codec] === undefined) {
75       throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
76     }
77
78     if (codecs[codec].canEncode !== true) {
79       throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
80     }
81   }
82
83   return checkFFmpegEncoders()
84 }
85
86 // Optional encoders, if present, can be used to improve transcoding
87 // Here we ask ffmpeg if it detects their presence on the system, so that we can later use them
88 let supportedOptionalEncoders: Map<string, boolean>
89 async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
90   if (supportedOptionalEncoders !== undefined) {
91     return supportedOptionalEncoders
92   }
93
94   const Ffmpeg = require('fluent-ffmpeg')
95   const getAvailableEncodersPromise = promisify0(Ffmpeg.getAvailableEncoders)
96   const encoders = await getAvailableEncodersPromise()
97   const optionalEncoders = [ 'libfdk_aac' ]
98   supportedOptionalEncoders = new Map<string, boolean>()
99
100   for (const encoder of optionalEncoders) {
101     supportedOptionalEncoders.set(encoder, encoders[encoder] !== undefined)
102   }
103
104   return supportedOptionalEncoders
105 }
106
107 // ---------------------------------------------------------------------------
108
109 export {
110   checkFFmpeg,
111   checkFFmpegEncoders,
112   checkMissedConfig
113 }