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