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'
11 async function checkActivityPubUrls () {
12 const actor = await getServerActor()
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')
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}` : ''
29 // Some checks on configuration files
30 // Return an error message, or null if everything is okay
31 function checkConfig () {
32 const defaultNSFWPolicy = config.get<string>('instance.default_nsfw_policy')
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
41 // Check the config files
42 function checkMissedConfig () {
43 const required = [ 'listen.port', 'listen.hostname',
44 'webserver.https', 'webserver.hostname', 'webserver.port',
46 'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password', 'database.pool.max',
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',
50 'user.video_quota', 'user.video_quota_daily',
51 'cache.previews.size', 'admin.email',
52 'signup.enabled', 'signup.limit', 'signup.requires_email_verification',
53 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist',
54 'transcoding.enabled', 'transcoding.threads',
55 'import.videos.http.enabled',
56 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
57 'instance.default_nsfw_policy', 'instance.robots',
58 'services.twitter.username', 'services.twitter.whitelisted'
60 const requiredAlternatives = [
62 ['redis.hostname', 'redis.port'], // alternative
66 const miss: string[] = []
68 for (const key of required) {
69 if (!config.has(key)) {
74 const missingAlternatives = requiredAlternatives.filter(
75 set => !set.find(alternative => !alternative.find(key => !config.has(key)))
79 .forEach(set => set[0].forEach(key => miss.push(key)))
84 // Check the available codecs
85 // We get CONFIG by param to not import it in this file (import orders)
86 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
87 const Ffmpeg = require('fluent-ffmpeg')
88 const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
89 const codecs = await getAvailableCodecsPromise()
90 const canEncode = [ 'libx264' ]
92 if (CONFIG.TRANSCODING.ENABLED === false) return undefined
94 for (const codec of canEncode) {
95 if (codecs[codec] === undefined) {
96 throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
99 if (codecs[codec].canEncode !== true) {
100 throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
104 checkFFmpegEncoders()
107 // Optional encoders, if present, can be used to improve transcoding
108 // Here we ask ffmpeg if it detects their presence on the system, so that we can later use them
109 let supportedOptionalEncoders: Map<string, boolean>
110 async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
111 if (supportedOptionalEncoders !== undefined) {
112 return supportedOptionalEncoders
115 const Ffmpeg = require('fluent-ffmpeg')
116 const getAvailableEncodersPromise = promisify0(Ffmpeg.getAvailableEncoders)
117 const encoders = await getAvailableEncodersPromise()
118 const optionalEncoders = [ 'libfdk_aac' ]
119 supportedOptionalEncoders = new Map<string, boolean>()
121 for (const encoder of optionalEncoders) {
122 supportedOptionalEncoders.set(encoder,
123 encoders[encoder] !== undefined
128 // We get db by param to not import it in this file (import orders)
129 async function clientsExist () {
130 const totalClients = await OAuthClientModel.countTotal()
132 return totalClients !== 0
135 // We get db by param to not import it in this file (import orders)
136 async function usersExist () {
137 const totalUsers = await UserModel.countTotal()
139 return totalUsers !== 0
142 // We get db by param to not import it in this file (import orders)
143 async function applicationExist () {
144 const totalApplication = await ApplicationModel.countTotal()
146 return totalApplication !== 0
149 // ---------------------------------------------------------------------------