]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/checker-after-init.ts
/!\ Use a dedicated config file for development
[github/Chocobozzz/PeerTube.git] / server / initializers / checker-after-init.ts
CommitLineData
d41a3805 1import config from 'config'
ae71acca 2import { uniq } from 'lodash'
a1587156 3import { URL } from 'url'
c729caf6 4import { getFFmpegVersion } from '@server/helpers/ffmpeg'
ae71acca 5import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type'
d1105b97 6import { RecentlyAddedStrategy } from '../../shared/models/redundancy'
9452d4fd 7import { isProdInstance, parseSemVersion } from '../helpers/core-utils'
e5565833 8import { isArray } from '../helpers/custom-validators/misc'
ae71acca 9import { logger } from '../helpers/logger'
ae71acca
C
10import { ApplicationModel, getServerActor } from '../models/application/application'
11import { OAuthClientModel } from '../models/oauth/oauth-client'
d41a3805 12import { UserModel } from '../models/user/user'
ae71acca 13import { CONFIG, isEmailEnabled } from './config'
6dd9de95 14import { WEBSERVER } from './constants'
e5565833
C
15
16async function checkActivityPubUrls () {
17 const actor = await getServerActor()
18
a1587156 19 const parsed = new URL(actor.url)
6dd9de95 20 if (WEBSERVER.HOST !== parsed.host) {
d41a3805
C
21 const NODE_ENV = config.util.getEnv('NODE_ENV')
22 const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
e5565833
C
23
24 logger.warn(
25 'It seems PeerTube was started (and created some data) with another domain name. ' +
26 'This means you will not be able to federate! ' +
27 'Please use %s %s npm run update-host to fix this.',
28 NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
29 NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
30 )
31 }
32}
33
c729caf6 34// Some checks on configuration files or throw if there is an error
e5565833 35function checkConfig () {
d3e56c0c 36
9452d4fd
C
37 const configFiles = config.util.getConfigSources().map(s => s.name).join(' -> ')
38 logger.info('Using following configuration file hierarchy: %s.', configFiles)
39
539d3f4f 40 // Moved configuration keys
d41a3805 41 if (config.has('services.csp-logger')) {
539d3f4f
C
42 logger.warn('services.csp-logger configuration has been renamed to csp.report_uri. Please update your configuration file.')
43 }
44
c729caf6
C
45 checkEmailConfig()
46 checkNSFWPolicyConfig()
47 checkLocalRedundancyConfig()
48 checkRemoteRedundancyConfig()
49 checkStorageConfig()
50 checkTranscodingConfig()
51 checkBroadcastMessageConfig()
52 checkSearchConfig()
53 checkLiveConfig()
54 checkObjectStorageConfig()
92e66e04 55 checkVideoStudioConfig()
c729caf6
C
56}
57
58// We get db by param to not import it in this file (import orders)
59async function clientsExist () {
60 const totalClients = await OAuthClientModel.countTotal()
61
62 return totalClients !== 0
63}
64
65// We get db by param to not import it in this file (import orders)
66async function usersExist () {
67 const totalUsers = await UserModel.countTotal()
68
69 return totalUsers !== 0
70}
71
72// We get db by param to not import it in this file (import orders)
73async function applicationExist () {
74 const totalApplication = await ApplicationModel.countTotal()
75
76 return totalApplication !== 0
77}
78
79async function checkFFmpegVersion () {
80 const version = await getFFmpegVersion()
81 const { major, minor } = parseSemVersion(version)
82
83 if (major < 4 || (major === 4 && minor < 1)) {
84 logger.warn('Your ffmpeg version (%s) is outdated. PeerTube supports ffmpeg >= 4.1. Please upgrade.', version)
85 }
86}
87
88// ---------------------------------------------------------------------------
89
90export {
91 checkConfig,
92 clientsExist,
93 checkFFmpegVersion,
94 usersExist,
95 applicationExist,
96 checkActivityPubUrls
97}
98
99// ---------------------------------------------------------------------------
100
101function checkEmailConfig () {
4c1c1709 102 if (!isEmailEnabled()) {
d3e56c0c 103 if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
c729caf6 104 throw new Error('Emailer is disabled but you require signup email verification.')
d3e56c0c
C
105 }
106
107 if (CONFIG.CONTACT_FORM.ENABLED) {
108 logger.warn('Emailer is disabled so the contact form will not work.')
109 }
110 }
c729caf6 111}
e5565833 112
c729caf6 113function checkNSFWPolicyConfig () {
d3e56c0c 114 const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
c729caf6
C
115
116 const available = [ 'do_not_list', 'blur', 'display' ]
117 if (available.includes(defaultNSFWPolicy) === false) {
118 throw new Error('NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy)
e5565833 119 }
c729caf6 120}
e5565833 121
c729caf6 122function checkLocalRedundancyConfig () {
e5565833 123 const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
c729caf6 124
e5565833
C
125 if (isArray(redundancyVideos)) {
126 const available = [ 'most-views', 'trending', 'recently-added' ]
c729caf6 127
e5565833 128 for (const r of redundancyVideos) {
bdd428a6 129 if (available.includes(r.strategy) === false) {
c729caf6 130 throw new Error('Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy)
e5565833
C
131 }
132
133 // Lifetime should not be < 10 hours
9452d4fd 134 if (isProdInstance() && r.minLifetime < 1000 * 3600 * 10) {
c729caf6 135 throw new Error('Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy)
e5565833
C
136 }
137 }
138
139 const filtered = uniq(redundancyVideos.map(r => r.strategy))
140 if (filtered.length !== redundancyVideos.length) {
c729caf6 141 throw new Error('Redundancy video entries should have unique strategies')
e5565833
C
142 }
143
144 const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
145 if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
c729caf6 146 throw new Error('Min views in recently added strategy is not a number')
e5565833 147 }
d85798c4 148 } else {
c729caf6 149 throw new Error('Videos redundancy should be an array (you must uncomment lines containing - too)')
e5565833 150 }
c729caf6 151}
e5565833 152
c729caf6 153function checkRemoteRedundancyConfig () {
8c9e7875
C
154 const acceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM
155 const acceptFromValues = new Set<VideoRedundancyConfigFilter>([ 'nobody', 'anybody', 'followings' ])
c729caf6 156
8c9e7875 157 if (acceptFromValues.has(acceptFrom) === false) {
c729caf6 158 throw new Error('remote_redundancy.videos.accept_from has an incorrect value')
8c9e7875 159 }
c729caf6 160}
8c9e7875 161
c729caf6 162function checkStorageConfig () {
d3e56c0c 163 // Check storage directory locations
e5565833 164 if (isProdInstance()) {
d41a3805 165 const configStorage = config.get('storage')
e5565833
C
166 for (const key of Object.keys(configStorage)) {
167 if (configStorage[key].startsWith('storage/')) {
168 logger.warn(
169 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
170 key
171 )
172 }
173 }
174 }
175
72c33e71
C
176 if (CONFIG.STORAGE.VIDEOS_DIR === CONFIG.STORAGE.REDUNDANCY_DIR) {
177 logger.warn('Redundancy directory should be different than the videos folder.')
178 }
c729caf6 179}
72c33e71 180
c729caf6 181function checkTranscodingConfig () {
d7a25329
C
182 if (CONFIG.TRANSCODING.ENABLED) {
183 if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false && CONFIG.TRANSCODING.HLS.ENABLED === false) {
c729caf6 184 throw new Error('You need to enable at least WebTorrent transcoding or HLS transcoding.')
d7a25329 185 }
9129b769
C
186
187 if (CONFIG.TRANSCODING.CONCURRENCY <= 0) {
c729caf6 188 throw new Error('Transcoding concurrency should be > 0')
9129b769
C
189 }
190 }
191
192 if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED || CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED) {
193 if (CONFIG.IMPORT.VIDEOS.CONCURRENCY <= 0) {
c729caf6 194 throw new Error('Video import concurrency should be > 0')
9129b769 195 }
d7a25329 196 }
c729caf6 197}
d7a25329 198
c729caf6 199function checkBroadcastMessageConfig () {
72c33e71
C
200 if (CONFIG.BROADCAST_MESSAGE.ENABLED) {
201 const currentLevel = CONFIG.BROADCAST_MESSAGE.LEVEL
31a91119 202 const available = [ 'info', 'warning', 'error' ]
72c33e71
C
203
204 if (available.includes(currentLevel) === false) {
c729caf6 205 throw new Error('Broadcast message level should be ' + available.join(' or ') + ' instead of ' + currentLevel)
72c33e71 206 }
2034c3aa 207 }
c729caf6 208}
2034c3aa 209
c729caf6 210function checkSearchConfig () {
5fb2e288
C
211 if (CONFIG.SEARCH.SEARCH_INDEX.ENABLED === true) {
212 if (CONFIG.SEARCH.REMOTE_URI.USERS === false) {
c729caf6 213 throw new Error('You cannot enable search index without enabling remote URI search for users.')
5fb2e288
C
214 }
215 }
c729caf6 216}
5fb2e288 217
c729caf6 218function checkLiveConfig () {
fb719404
C
219 if (CONFIG.LIVE.ENABLED === true) {
220 if (CONFIG.LIVE.ALLOW_REPLAY === true && CONFIG.TRANSCODING.ENABLED === false) {
c729caf6 221 throw new Error('Live allow replay cannot be enabled if transcoding is not enabled.')
fb719404 222 }
df1db951
C
223
224 if (CONFIG.LIVE.RTMP.ENABLED === false && CONFIG.LIVE.RTMPS.ENABLED === false) {
c729caf6 225 throw new Error('You must enable at least RTMP or RTMPS')
df1db951
C
226 }
227
228 if (CONFIG.LIVE.RTMPS.ENABLED) {
229 if (!CONFIG.LIVE.RTMPS.KEY_FILE) {
c729caf6 230 throw new Error('You must specify a key file to enabled RTMPS')
df1db951
C
231 }
232
233 if (!CONFIG.LIVE.RTMPS.CERT_FILE) {
c729caf6 234 throw new Error('You must specify a cert file to enable RTMPS')
df1db951
C
235 }
236 }
fb719404 237 }
c729caf6 238}
fb719404 239
c729caf6 240function checkObjectStorageConfig () {
0305db28
JB
241 if (CONFIG.OBJECT_STORAGE.ENABLED === true) {
242
243 if (!CONFIG.OBJECT_STORAGE.VIDEOS.BUCKET_NAME) {
c729caf6 244 throw new Error('videos_bucket should be set when object storage support is enabled.')
0305db28
JB
245 }
246
247 if (!CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME) {
c729caf6 248 throw new Error('streaming_playlists_bucket should be set when object storage support is enabled.')
0305db28
JB
249 }
250
251 if (
252 CONFIG.OBJECT_STORAGE.VIDEOS.BUCKET_NAME === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME &&
253 CONFIG.OBJECT_STORAGE.VIDEOS.PREFIX === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.PREFIX
254 ) {
255 if (CONFIG.OBJECT_STORAGE.VIDEOS.PREFIX === '') {
c729caf6 256 throw new Error('Object storage bucket prefixes should be set when the same bucket is used for both types of video.')
0305db28 257 }
c729caf6
C
258
259 throw new Error(
260 'Object storage bucket prefixes should be set to different values when the same bucket is used for both types of video.'
261 )
0305db28
JB
262 }
263 }
e5565833
C
264}
265
92e66e04
C
266function checkVideoStudioConfig () {
267 if (CONFIG.VIDEO_STUDIO.ENABLED === true && CONFIG.TRANSCODING.ENABLED === false) {
268 throw new Error('Video studio cannot be enabled if transcoding is disabled')
ae71acca
C
269 }
270}