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 { RecentlyAddedStrategy, VideosRedundancy } from '../../shared/models/redundancy'
11 import { isArray } from '../helpers/custom-validators/misc'
12 import { uniq } from 'lodash'
14 async function checkActivityPubUrls () {
15 const actor = await getServerActor()
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')
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}` : ''
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')
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
43 const redundancyVideos = config.get<VideosRedundancy[]>('redundancy.videos.strategies')
44 if (isArray(redundancyVideos)) {
45 for (const r of redundancyVideos) {
46 if ([ 'most-views', 'trending', 'recently-added' ].indexOf(r.strategy) === -1) {
47 return 'Redundancy video entries should have "most-views" strategy instead of ' + r.strategy
51 const filtered = uniq(redundancyVideos.map(r => r.strategy))
52 if (filtered.length !== redundancyVideos.length) {
53 return 'Redundancy video entries should have unique strategies'
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'
65 // Check the config files
66 function checkMissedConfig () {
67 const required = [ 'listen.port', 'listen.hostname',
68 'webserver.https', 'webserver.hostname', 'webserver.port',
70 'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password', 'database.pool.max',
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',
74 'user.video_quota', 'user.video_quota_daily',
75 'cache.previews.size', 'admin.email',
76 'signup.enabled', 'signup.limit', 'signup.requires_email_verification',
77 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist',
78 'redundancy.videos.strategies',
79 'transcoding.enabled', 'transcoding.threads',
80 'import.videos.http.enabled', 'import.videos.torrent.enabled',
81 'trending.videos.interval_days',
82 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
83 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt',
84 'services.twitter.username', 'services.twitter.whitelisted'
86 const requiredAlternatives = [
88 ['redis.hostname', 'redis.port'], // alternative
92 const miss: string[] = []
94 for (const key of required) {
95 if (!config.has(key)) {
100 const missingAlternatives = requiredAlternatives.filter(
101 set => !set.find(alternative => !alternative.find(key => !config.has(key)))
105 .forEach(set => set[0].forEach(key => miss.push(key)))
110 // Check the available codecs
111 // We get CONFIG by param to not import it in this file (import orders)
112 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
113 const Ffmpeg = require('fluent-ffmpeg')
114 const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
115 const codecs = await getAvailableCodecsPromise()
116 const canEncode = [ 'libx264' ]
118 if (CONFIG.TRANSCODING.ENABLED === false) return undefined
120 for (const codec of canEncode) {
121 if (codecs[codec] === undefined) {
122 throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
125 if (codecs[codec].canEncode !== true) {
126 throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
130 checkFFmpegEncoders()
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
135 let supportedOptionalEncoders: Map<string, boolean>
136 async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
137 if (supportedOptionalEncoders !== undefined) {
138 return supportedOptionalEncoders
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>()
147 for (const encoder of optionalEncoders) {
148 supportedOptionalEncoders.set(encoder,
149 encoders[encoder] !== undefined
154 // We get db by param to not import it in this file (import orders)
155 async function clientsExist () {
156 const totalClients = await OAuthClientModel.countTotal()
158 return totalClients !== 0
161 // We get db by param to not import it in this file (import orders)
162 async function usersExist () {
163 const totalUsers = await UserModel.countTotal()
165 return totalUsers !== 0
168 // We get db by param to not import it in this file (import orders)
169 async function applicationExist () {
170 const totalApplication = await ApplicationModel.countTotal()
172 return totalApplication !== 0
175 // ---------------------------------------------------------------------------