]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/checker-before-init.ts
ef12b3eea617198b404c6e1036bc29c4dd63db5e
[github/Chocobozzz/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',
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 ]
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
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
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 }