Merge branch 'release/v1.2.0'
[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',
16     'log.level',
17     'user.video_quota', 'user.video_quota_daily',
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',
24     'trending.videos.interval_days',
25     'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
26     'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt',
27     'services.twitter.username', 'services.twitter.whitelisted'
28   ]
29   const requiredAlternatives = [
30     [ // set
31       ['redis.hostname', 'redis.port'], // alternative
32       ['redis.socket']
33     ]
34   ]
35   const miss: string[] = []
36
37   for (const key of required) {
38     if (!config.has(key)) {
39       miss.push(key)
40     }
41   }
42
43   const redundancyVideos = config.get<any>('redundancy.videos.strategies')
44   if (isArray(redundancyVideos)) {
45     for (const r of redundancyVideos) {
46       if (!r.size) miss.push('redundancy.videos.strategies.size')
47       if (!r.min_lifetime) miss.push('redundancy.videos.strategies.min_lifetime')
48     }
49   }
50
51   const missingAlternatives = requiredAlternatives.filter(
52     set => !set.find(alternative => !alternative.find(key => !config.has(key)))
53   )
54
55   missingAlternatives
56     .forEach(set => set[0].forEach(key => miss.push(key)))
57
58   return miss
59 }
60
61 // Check the available codecs
62 // We get CONFIG by param to not import it in this file (import orders)
63 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
64   const Ffmpeg = require('fluent-ffmpeg')
65   const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
66   const codecs = await getAvailableCodecsPromise()
67   const canEncode = [ 'libx264' ]
68
69   if (CONFIG.TRANSCODING.ENABLED === false) return undefined
70
71   for (const codec of canEncode) {
72     if (codecs[codec] === undefined) {
73       throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
74     }
75
76     if (codecs[codec].canEncode !== true) {
77       throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
78     }
79   }
80
81   return checkFFmpegEncoders()
82 }
83
84 // Optional encoders, if present, can be used to improve transcoding
85 // Here we ask ffmpeg if it detects their presence on the system, so that we can later use them
86 let supportedOptionalEncoders: Map<string, boolean>
87 async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
88   if (supportedOptionalEncoders !== undefined) {
89     return supportedOptionalEncoders
90   }
91
92   const Ffmpeg = require('fluent-ffmpeg')
93   const getAvailableEncodersPromise = promisify0(Ffmpeg.getAvailableEncoders)
94   const encoders = await getAvailableEncodersPromise()
95   const optionalEncoders = [ 'libfdk_aac' ]
96   supportedOptionalEncoders = new Map<string, boolean>()
97
98   for (const encoder of optionalEncoders) {
99     supportedOptionalEncoders.set(encoder, encoders[encoder] !== undefined)
100   }
101
102   return supportedOptionalEncoders
103 }
104
105 // ---------------------------------------------------------------------------
106
107 export {
108   checkFFmpeg,
109   checkFFmpegEncoders,
110   checkMissedConfig
111 }