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