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