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