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