]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/checker-before-init.ts
bd8f02bc02eb4f9b906a04fa33d576c45f580ff1
[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 'transcoding.resolutions.0p', 'transcoding.resolutions.240p', 'transcoding.resolutions.360p', 'transcoding.resolutions.480p',
26 'transcoding.resolutions.720p', 'transcoding.resolutions.1080p', 'transcoding.resolutions.2160p',
27 'import.videos.http.enabled', 'import.videos.torrent.enabled', 'auto_blacklist.videos.of_users.enabled',
28 'trending.videos.interval_days',
29 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
30 'instance.is_nsfw', 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt',
31 'services.twitter.username', 'services.twitter.whitelisted',
32 'followers.instance.enabled', 'followers.instance.manual_approval',
33 'tracker.enabled', 'tracker.private', 'tracker.reject_too_many_announces',
34 'history.videos.max_age', 'views.videos.remote.max_age',
35 'rates_limit.login.window', 'rates_limit.login.max', 'rates_limit.ask_send_email.window', 'rates_limit.ask_send_email.max',
36 'theme.default',
37 'remote_redundancy.videos.accept_from',
38 'federation.videos.federate_unlisted'
39 ]
40 const requiredAlternatives = [
41 [ // set
42 [ 'redis.hostname', 'redis.port' ], // alternative
43 [ 'redis.socket' ]
44 ]
45 ]
46 const miss: string[] = []
47
48 for (const key of required) {
49 if (!config.has(key)) {
50 miss.push(key)
51 }
52 }
53
54 const redundancyVideos = config.get<any>('redundancy.videos.strategies')
55
56 if (Array.isArray(redundancyVideos)) {
57 for (const r of redundancyVideos) {
58 if (!r.size) miss.push('redundancy.videos.strategies.size')
59 if (!r.min_lifetime) miss.push('redundancy.videos.strategies.min_lifetime')
60 }
61 }
62
63 const missingAlternatives = requiredAlternatives.filter(
64 set => !set.find(alternative => !alternative.find(key => !config.has(key)))
65 )
66
67 missingAlternatives
68 .forEach(set => set[0].forEach(key => miss.push(key)))
69
70 return miss
71 }
72
73 // Check the available codecs
74 // We get CONFIG by param to not import it in this file (import orders)
75 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
76 if (CONFIG.TRANSCODING.ENABLED === false) return undefined
77
78 const Ffmpeg = require('fluent-ffmpeg')
79 const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
80 const codecs = await getAvailableCodecsPromise()
81 const canEncode = [ 'libx264' ]
82
83 for (const codec of canEncode) {
84 if (codecs[codec] === undefined) {
85 throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
86 }
87
88 if (codecs[codec].canEncode !== true) {
89 throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
90 }
91 }
92
93 return checkFFmpegEncoders()
94 }
95
96 // Optional encoders, if present, can be used to improve transcoding
97 // Here we ask ffmpeg if it detects their presence on the system, so that we can later use them
98 let supportedOptionalEncoders: Map<string, boolean>
99 async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
100 if (supportedOptionalEncoders !== undefined) {
101 return supportedOptionalEncoders
102 }
103
104 const Ffmpeg = require('fluent-ffmpeg')
105 const getAvailableEncodersPromise = promisify0(Ffmpeg.getAvailableEncoders)
106 const encoders = await getAvailableEncodersPromise()
107 const optionalEncoders = [ 'libfdk_aac' ]
108 supportedOptionalEncoders = new Map<string, boolean>()
109
110 for (const encoder of optionalEncoders) {
111 supportedOptionalEncoders.set(encoder, encoders[encoder] !== undefined)
112 }
113
114 return supportedOptionalEncoders
115 }
116
117 function checkNodeVersion () {
118 const v = process.version
119 const majorString = v.split('.')[0].replace('v', '')
120 const major = parseInt(majorString, 10)
121
122 logger.debug('Checking NodeJS version %s.', v)
123
124 if (major < 10) {
125 logger.warn('Your NodeJS version %s is deprecated. Please use Node 10.', v)
126 }
127 }
128
129 // ---------------------------------------------------------------------------
130
131 export {
132 checkFFmpeg,
133 checkFFmpegEncoders,
134 checkMissedConfig,
135 checkNodeVersion
136 }