]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/checker-before-init.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / initializers / checker-before-init.ts
1 import * as config from 'config'
2 import { promisify0 } from '../helpers/core-utils'
3 import { logger } from '../helpers/logger'
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 'email.body.signature', 'email.subject.prefix',
15 'storage.avatars', 'storage.videos', 'storage.logs', 'storage.previews', 'storage.thumbnails', 'storage.torrents', 'storage.cache',
16 'storage.redundancy', 'storage.tmp', 'storage.streaming_playlists', 'storage.plugins',
17 'log.level',
18 'user.video_quota', 'user.video_quota_daily',
19 'csp.enabled', 'csp.report_only', 'csp.report_uri',
20 'cache.previews.size', 'admin.email', 'contact_form.enabled',
21 'signup.enabled', 'signup.limit', 'signup.requires_email_verification',
22 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist',
23 'redundancy.videos.strategies', 'redundancy.videos.check_interval',
24 'transcoding.enabled', 'transcoding.threads', 'transcoding.allow_additional_extensions', 'transcoding.hls.enabled',
25 'import.videos.http.enabled', 'import.videos.torrent.enabled', 'auto_blacklist.videos.of_users.enabled',
26 'trending.videos.interval_days',
27 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
28 'instance.is_nsfw', 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt',
29 'services.twitter.username', 'services.twitter.whitelisted',
30 'followers.instance.enabled', 'followers.instance.manual_approval',
31 'tracker.enabled', 'tracker.private', 'tracker.reject_too_many_announces',
32 'history.videos.max_age', 'views.videos.remote.max_age',
33 'rates_limit.login.window', 'rates_limit.login.max', 'rates_limit.ask_send_email.window', 'rates_limit.ask_send_email.max',
34 'theme.default'
35 ]
36 const requiredAlternatives = [
37 [ // set
38 [ 'redis.hostname', 'redis.port' ], // alternative
39 [ 'redis.socket' ]
40 ]
41 ]
42 const miss: string[] = []
43
44 for (const key of required) {
45 if (!config.has(key)) {
46 miss.push(key)
47 }
48 }
49
50 const redundancyVideos = config.get<any>('redundancy.videos.strategies')
51
52 if (Array.isArray(redundancyVideos)) {
53 for (const r of redundancyVideos) {
54 if (!r.size) miss.push('redundancy.videos.strategies.size')
55 if (!r.min_lifetime) miss.push('redundancy.videos.strategies.min_lifetime')
56 }
57 }
58
59 const missingAlternatives = requiredAlternatives.filter(
60 set => !set.find(alternative => !alternative.find(key => !config.has(key)))
61 )
62
63 missingAlternatives
64 .forEach(set => set[0].forEach(key => miss.push(key)))
65
66 return miss
67 }
68
69 // Check the available codecs
70 // We get CONFIG by param to not import it in this file (import orders)
71 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
72 if (CONFIG.TRANSCODING.ENABLED === false) return undefined
73
74 const Ffmpeg = require('fluent-ffmpeg')
75 const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
76 const codecs = await getAvailableCodecsPromise()
77 const canEncode = [ 'libx264' ]
78
79 for (const codec of canEncode) {
80 if (codecs[codec] === undefined) {
81 throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
82 }
83
84 if (codecs[codec].canEncode !== true) {
85 throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
86 }
87 }
88
89 return checkFFmpegEncoders()
90 }
91
92 // Optional encoders, if present, can be used to improve transcoding
93 // Here we ask ffmpeg if it detects their presence on the system, so that we can later use them
94 let supportedOptionalEncoders: Map<string, boolean>
95 async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
96 if (supportedOptionalEncoders !== undefined) {
97 return supportedOptionalEncoders
98 }
99
100 const Ffmpeg = require('fluent-ffmpeg')
101 const getAvailableEncodersPromise = promisify0(Ffmpeg.getAvailableEncoders)
102 const encoders = await getAvailableEncodersPromise()
103 const optionalEncoders = [ 'libfdk_aac' ]
104 supportedOptionalEncoders = new Map<string, boolean>()
105
106 for (const encoder of optionalEncoders) {
107 supportedOptionalEncoders.set(encoder, encoders[encoder] !== undefined)
108 }
109
110 return supportedOptionalEncoders
111 }
112
113 function checkNodeVersion () {
114 const v = process.version
115 const majorString = v.split('.')[0].replace('v', '')
116 const major = parseInt(majorString, 10)
117
118 logger.debug('Checking NodeJS version %s.', v)
119
120 if (major < 10) {
121 logger.warn('Your NodeJS version %s is deprecated. Please use Node 10.', v)
122 }
123 }
124
125 // ---------------------------------------------------------------------------
126
127 export {
128 checkFFmpeg,
129 checkFFmpegEncoders,
130 checkMissedConfig,
131 checkNodeVersion
132 }