]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/checker.ts
Basic video redundancy implementation
[github/Chocobozzz/PeerTube.git] / server / initializers / checker.ts
1 import * as config from 'config'
2 import { promisify0 } from '../helpers/core-utils'
3 import { UserModel } from '../models/account/user'
4 import { ApplicationModel } from '../models/application/application'
5 import { OAuthClientModel } from '../models/oauth/oauth-client'
6 import { parse } from 'url'
7 import { CONFIG } from './constants'
8 import { logger } from '../helpers/logger'
9 import { getServerActor } from '../helpers/utils'
10 import { VideosRedundancy } from '../../shared/models/redundancy'
11 import { isArray } from '../helpers/custom-validators/misc'
12 import { uniq } from 'lodash'
13
14 async 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 }
31
32 // Some checks on configuration files
33 // Return an error message, or null if everything is okay
34 function checkConfig () {
35 const defaultNSFWPolicy = config.get<string>('instance.default_nsfw_policy')
36
37 if ([ 'do_not_list', 'blur', 'display' ].indexOf(defaultNSFWPolicy) === -1) {
38 return 'NSFW policy setting should be "do_not_list" or "blur" or "display" instead of ' + defaultNSFWPolicy
39 }
40
41 const redundancyVideos = config.get<VideosRedundancy[]>('redundancy.videos')
42 if (isArray(redundancyVideos)) {
43 for (const r of redundancyVideos) {
44 if ([ 'most-views' ].indexOf(r.strategy) === -1) {
45 return 'Redundancy video entries should have "most-views" strategy instead of ' + r.strategy
46 }
47 }
48
49 const filtered = uniq(redundancyVideos.map(r => r.strategy))
50 if (filtered.length !== redundancyVideos.length) {
51 return 'Redundancy video entries should have uniq strategies'
52 }
53 }
54
55 return null
56 }
57
58 // Check the config files
59 function checkMissedConfig () {
60 const required = [ 'listen.port', 'listen.hostname',
61 'webserver.https', 'webserver.hostname', 'webserver.port',
62 'trust_proxy',
63 'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password', 'database.pool.max',
64 'smtp.hostname', 'smtp.port', 'smtp.username', 'smtp.password', 'smtp.tls', 'smtp.from_address',
65 'storage.avatars', 'storage.videos', 'storage.logs', 'storage.previews', 'storage.thumbnails', 'storage.torrents', 'storage.cache',
66 'log.level',
67 'user.video_quota', 'user.video_quota_daily',
68 'cache.previews.size', 'admin.email',
69 'signup.enabled', 'signup.limit', 'signup.requires_email_verification',
70 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist',
71 'transcoding.enabled', 'transcoding.threads',
72 'import.videos.http.enabled', 'import.videos.torrent.enabled',
73 'trending.videos.interval_days',
74 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
75 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt',
76 'services.twitter.username', 'services.twitter.whitelisted'
77 ]
78 const requiredAlternatives = [
79 [ // set
80 ['redis.hostname', 'redis.port'], // alternative
81 ['redis.socket']
82 ]
83 ]
84 const miss: string[] = []
85
86 for (const key of required) {
87 if (!config.has(key)) {
88 miss.push(key)
89 }
90 }
91
92 const missingAlternatives = requiredAlternatives.filter(
93 set => !set.find(alternative => !alternative.find(key => !config.has(key)))
94 )
95
96 missingAlternatives
97 .forEach(set => set[0].forEach(key => miss.push(key)))
98
99 return miss
100 }
101
102 // Check the available codecs
103 // We get CONFIG by param to not import it in this file (import orders)
104 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
105 const Ffmpeg = require('fluent-ffmpeg')
106 const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
107 const codecs = await getAvailableCodecsPromise()
108 const canEncode = [ 'libx264' ]
109
110 if (CONFIG.TRANSCODING.ENABLED === false) return undefined
111
112 for (const codec of canEncode) {
113 if (codecs[codec] === undefined) {
114 throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
115 }
116
117 if (codecs[codec].canEncode !== true) {
118 throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
119 }
120 }
121
122 checkFFmpegEncoders()
123 }
124
125 // Optional encoders, if present, can be used to improve transcoding
126 // Here we ask ffmpeg if it detects their presence on the system, so that we can later use them
127 let supportedOptionalEncoders: Map<string, boolean>
128 async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
129 if (supportedOptionalEncoders !== undefined) {
130 return supportedOptionalEncoders
131 }
132
133 const Ffmpeg = require('fluent-ffmpeg')
134 const getAvailableEncodersPromise = promisify0(Ffmpeg.getAvailableEncoders)
135 const encoders = await getAvailableEncodersPromise()
136 const optionalEncoders = [ 'libfdk_aac' ]
137 supportedOptionalEncoders = new Map<string, boolean>()
138
139 for (const encoder of optionalEncoders) {
140 supportedOptionalEncoders.set(encoder,
141 encoders[encoder] !== undefined
142 )
143 }
144 }
145
146 // We get db by param to not import it in this file (import orders)
147 async function clientsExist () {
148 const totalClients = await OAuthClientModel.countTotal()
149
150 return totalClients !== 0
151 }
152
153 // We get db by param to not import it in this file (import orders)
154 async function usersExist () {
155 const totalUsers = await UserModel.countTotal()
156
157 return totalUsers !== 0
158 }
159
160 // We get db by param to not import it in this file (import orders)
161 async function applicationExist () {
162 const totalApplication = await ApplicationModel.countTotal()
163
164 return totalApplication !== 0
165 }
166
167 // ---------------------------------------------------------------------------
168
169 export {
170 checkConfig,
171 checkFFmpeg,
172 checkFFmpegEncoders,
173 checkMissedConfig,
174 clientsExist,
175 usersExist,
176 applicationExist,
177 checkActivityPubUrls
178 }