]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/checker.ts
fixing tests to deal with new transcoding parameters
[github/Chocobozzz/PeerTube.git] / server / initializers / checker.ts
CommitLineData
4d4e5cd4 1import * as config from 'config'
da854ddd 2import { promisify0 } from '../helpers/core-utils'
3fd3ab2d
C
3import { UserModel } from '../models/account/user'
4import { ApplicationModel } from '../models/application/application'
5import { OAuthClientModel } from '../models/oauth/oauth-client'
23687332
C
6import { parse } from 'url'
7import { CONFIG } from './constants'
8import { logger } from '../helpers/logger'
9import { getServerActor } from '../helpers/utils'
10
11async function checkActivityPubUrls () {
12 const actor = await getServerActor()
13
14 const parsed = parse(actor.url)
15 if (CONFIG.WEBSERVER.HOST !== parsed.host) {
16 const NODE_ENV = config.util.getEnv('NODE_ENV')
17 const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
18
19 logger.warn(
20 'It seems PeerTube was started (and created some data) with another domain name. ' +
21 'This means you will not be able to federate! ' +
22 'Please use %s %s npm run update-host to fix this.',
23 NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
24 NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
25 )
26 }
27}
9f10b292 28
b65c27aa 29// Some checks on configuration files
0883b324 30// Return an error message, or null if everything is okay
9f10b292 31function checkConfig () {
0883b324 32 const defaultNSFWPolicy = config.get<string>('instance.default_nsfw_policy')
b65c27aa 33
0883b324
C
34 if ([ 'do_not_list', 'blur', 'display' ].indexOf(defaultNSFWPolicy) === -1) {
35 return 'NSFW policy setting should be "do_not_list" or "blur" or "display" instead of ' + defaultNSFWPolicy
b65c27aa
C
36 }
37
38 return null
39}
40
41// Check the config files
42function checkMissedConfig () {
cff8b272 43 const required = [ 'listen.port', 'listen.hostname',
3737bbaf 44 'webserver.https', 'webserver.hostname', 'webserver.port',
490b595a 45 'trust_proxy',
1c3386e8 46 'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password', 'database.pool.max',
a465bf5f
C
47 'smtp.hostname', 'smtp.port', 'smtp.username', 'smtp.password', 'smtp.tls', 'smtp.from_address',
48 'storage.avatars', 'storage.videos', 'storage.logs', 'storage.previews', 'storage.thumbnails', 'storage.torrents', 'storage.cache',
49 'log.level',
50 'user.video_quota',
ff2c1fe8
RK
51 'cache.previews.size', 'admin.email',
52 'signup.enabled', 'signup.limit', 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist',
53 'transcoding.enabled', 'transcoding.threads',
0883b324 54 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
ac235c37 55 'instance.default_nsfw_policy', 'instance.robots',
8be1afa1 56 'services.twitter.username', 'services.twitter.whitelisted'
56835348 57 ]
19f7b248
RK
58 const requiredAlternatives = [
59 [ // set
60 ['redis.hostname', 'redis.port'], // alternative
61 ['redis.socket']
62 ]
63 ]
69818c93 64 const miss: string[] = []
9f10b292 65
f0f5567b 66 for (const key of required) {
9f10b292
C
67 if (!config.has(key)) {
68 miss.push(key)
8c308c2b 69 }
8c308c2b
C
70 }
71
19f7b248
RK
72 const missingAlternatives = requiredAlternatives.filter(
73 set => !set.find(alternative => !alternative.find(key => !config.has(key)))
74 )
75
76 missingAlternatives
77 .forEach(set => set[0].forEach(key => miss.push(key)))
78
9f10b292
C
79 return miss
80}
81
e5b88539 82// Check the available codecs
3482688c 83// We get CONFIG by param to not import it in this file (import orders)
f5028693 84async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
e5b88539 85 const Ffmpeg = require('fluent-ffmpeg')
6fcd19ba 86 const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
f5028693 87 const codecs = await getAvailableCodecsPromise()
4176e227
RK
88 const canEncode = [ 'libx264' ]
89
f5028693
C
90 if (CONFIG.TRANSCODING.ENABLED === false) return undefined
91
f5028693
C
92 for (const codec of canEncode) {
93 if (codecs[codec] === undefined) {
94 throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
95 }
96
97 if (codecs[codec].canEncode !== true) {
98 throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
99 }
100 }
4176e227
RK
101
102 checkFFmpegEncoders()
103}
104
105// Optional encoders, if present, can be used to improve transcoding
106// Here we ask ffmpeg if it detects their presence on the system, so that we can later use them
107let supportedOptionalEncoders: Map<string, boolean>
108async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
109 if (supportedOptionalEncoders !== undefined) {
110 return supportedOptionalEncoders
111 }
112
113 const Ffmpeg = require('fluent-ffmpeg')
114 const getAvailableEncodersPromise = promisify0(Ffmpeg.getAvailableEncoders)
115 const encoders = await getAvailableEncodersPromise()
116 const optionalEncoders = [ 'libfdk_aac' ]
117 supportedOptionalEncoders = new Map<string, boolean>()
118
119 for (const encoder of optionalEncoders) {
120 supportedOptionalEncoders.set(encoder,
121 encoders[encoder] !== undefined
122 )
123 }
e5b88539
C
124}
125
3482688c 126// We get db by param to not import it in this file (import orders)
3fd3ab2d
C
127async function clientsExist () {
128 const totalClients = await OAuthClientModel.countTotal()
f5028693
C
129
130 return totalClients !== 0
37dc07b2
C
131}
132
3482688c 133// We get db by param to not import it in this file (import orders)
3fd3ab2d
C
134async function usersExist () {
135 const totalUsers = await UserModel.countTotal()
f5028693
C
136
137 return totalUsers !== 0
9f10b292 138}
8c308c2b 139
350e31d6 140// We get db by param to not import it in this file (import orders)
3fd3ab2d
C
141async function applicationExist () {
142 const totalApplication = await ApplicationModel.countTotal()
350e31d6
C
143
144 return totalApplication !== 0
145}
146
9f10b292 147// ---------------------------------------------------------------------------
c45f7f84 148
65fcc311
C
149export {
150 checkConfig,
151 checkFFmpeg,
4176e227 152 checkFFmpegEncoders,
65fcc311
C
153 checkMissedConfig,
154 clientsExist,
350e31d6 155 usersExist,
23687332
C
156 applicationExist,
157 checkActivityPubUrls
65fcc311 158}