]>
Commit | Line | Data |
---|---|---|
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' | |
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 | // 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 | |
40 | } | |
41 | ||
42 | // Redundancies | |
43 | const redundancyVideos = config.get<VideosRedundancy[]>('redundancy.videos') | |
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 | |
48 | } | |
49 | } | |
50 | ||
51 | const filtered = uniq(redundancyVideos.map(r => r.strategy)) | |
52 | if (filtered.length !== redundancyVideos.length) { | |
53 | return 'Redundancy video entries should have unique strategies' | |
54 | } | |
55 | } | |
56 | ||
57 | const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy | |
58 | if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) { | |
59 | return 'Min views in recently added strategy is not a number' | |
60 | } | |
61 | ||
62 | return null | |
63 | } | |
64 | ||
65 | // Check the config files | |
66 | function checkMissedConfig () { | |
67 | const required = [ 'listen.port', 'listen.hostname', | |
68 | 'webserver.https', 'webserver.hostname', 'webserver.port', | |
69 | 'trust_proxy', | |
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', | |
73 | 'log.level', | |
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 | 'transcoding.enabled', 'transcoding.threads', | |
79 | 'import.videos.http.enabled', 'import.videos.torrent.enabled', | |
80 | 'trending.videos.interval_days', | |
81 | 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route', | |
82 | 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt', | |
83 | 'services.twitter.username', 'services.twitter.whitelisted' | |
84 | ] | |
85 | const requiredAlternatives = [ | |
86 | [ // set | |
87 | ['redis.hostname', 'redis.port'], // alternative | |
88 | ['redis.socket'] | |
89 | ] | |
90 | ] | |
91 | const miss: string[] = [] | |
92 | ||
93 | for (const key of required) { | |
94 | if (!config.has(key)) { | |
95 | miss.push(key) | |
96 | } | |
97 | } | |
98 | ||
99 | const missingAlternatives = requiredAlternatives.filter( | |
100 | set => !set.find(alternative => !alternative.find(key => !config.has(key))) | |
101 | ) | |
102 | ||
103 | missingAlternatives | |
104 | .forEach(set => set[0].forEach(key => miss.push(key))) | |
105 | ||
106 | return miss | |
107 | } | |
108 | ||
109 | // Check the available codecs | |
110 | // We get CONFIG by param to not import it in this file (import orders) | |
111 | async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) { | |
112 | const Ffmpeg = require('fluent-ffmpeg') | |
113 | const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs) | |
114 | const codecs = await getAvailableCodecsPromise() | |
115 | const canEncode = [ 'libx264' ] | |
116 | ||
117 | if (CONFIG.TRANSCODING.ENABLED === false) return undefined | |
118 | ||
119 | for (const codec of canEncode) { | |
120 | if (codecs[codec] === undefined) { | |
121 | throw new Error('Unknown codec ' + codec + ' in FFmpeg.') | |
122 | } | |
123 | ||
124 | if (codecs[codec].canEncode !== true) { | |
125 | throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg') | |
126 | } | |
127 | } | |
128 | ||
129 | checkFFmpegEncoders() | |
130 | } | |
131 | ||
132 | // Optional encoders, if present, can be used to improve transcoding | |
133 | // Here we ask ffmpeg if it detects their presence on the system, so that we can later use them | |
134 | let supportedOptionalEncoders: Map<string, boolean> | |
135 | async function checkFFmpegEncoders (): Promise<Map<string, boolean>> { | |
136 | if (supportedOptionalEncoders !== undefined) { | |
137 | return supportedOptionalEncoders | |
138 | } | |
139 | ||
140 | const Ffmpeg = require('fluent-ffmpeg') | |
141 | const getAvailableEncodersPromise = promisify0(Ffmpeg.getAvailableEncoders) | |
142 | const encoders = await getAvailableEncodersPromise() | |
143 | const optionalEncoders = [ 'libfdk_aac' ] | |
144 | supportedOptionalEncoders = new Map<string, boolean>() | |
145 | ||
146 | for (const encoder of optionalEncoders) { | |
147 | supportedOptionalEncoders.set(encoder, | |
148 | encoders[encoder] !== undefined | |
149 | ) | |
150 | } | |
151 | } | |
152 | ||
153 | // We get db by param to not import it in this file (import orders) | |
154 | async function clientsExist () { | |
155 | const totalClients = await OAuthClientModel.countTotal() | |
156 | ||
157 | return totalClients !== 0 | |
158 | } | |
159 | ||
160 | // We get db by param to not import it in this file (import orders) | |
161 | async function usersExist () { | |
162 | const totalUsers = await UserModel.countTotal() | |
163 | ||
164 | return totalUsers !== 0 | |
165 | } | |
166 | ||
167 | // We get db by param to not import it in this file (import orders) | |
168 | async function applicationExist () { | |
169 | const totalApplication = await ApplicationModel.countTotal() | |
170 | ||
171 | return totalApplication !== 0 | |
172 | } | |
173 | ||
174 | // --------------------------------------------------------------------------- | |
175 | ||
176 | export { | |
177 | checkConfig, | |
178 | checkFFmpeg, | |
179 | checkFFmpegEncoders, | |
180 | checkMissedConfig, | |
181 | clientsExist, | |
182 | usersExist, | |
183 | applicationExist, | |
184 | checkActivityPubUrls | |
185 | } |