]>
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 | ||
11 | async function checkActivityPubUrls () { | |
12 | const actor = await getServerActor() | |
13 | ||
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') | |
18 | ||
19 | logger.warn( | |
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}` : '' | |
25 | ) | |
26 | } | |
27 | } | |
28 | ||
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') | |
33 | ||
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 | |
36 | } | |
37 | ||
38 | return null | |
39 | } | |
40 | ||
41 | // Check the config files | |
42 | function checkMissedConfig () { | |
43 | const required = [ 'listen.port', 'listen.hostname', | |
44 | 'webserver.https', 'webserver.hostname', 'webserver.port', | |
45 | 'trust_proxy', | |
46 | 'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password', | |
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', | |
49 | 'log.level', | |
50 | 'user.video_quota', | |
51 | 'cache.previews.size', 'admin.email', | |
52 | 'signup.enabled', 'signup.limit', 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist', | |
53 | 'transcoding.enabled', 'transcoding.threads', | |
54 | 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route', | |
55 | 'instance.default_nsfw_policy', 'instance.robots', | |
56 | 'services.twitter.username', 'services.twitter.whitelisted' | |
57 | ] | |
58 | const requiredAlternatives = [ | |
59 | [ // set | |
60 | ['redis.hostname', 'redis.port'], // alternative | |
61 | ['redis.socket'] | |
62 | ] | |
63 | ] | |
64 | const miss: string[] = [] | |
65 | ||
66 | for (const key of required) { | |
67 | if (!config.has(key)) { | |
68 | miss.push(key) | |
69 | } | |
70 | } | |
71 | ||
72 | const missingAlternatives = requiredAlternatives.filter( | |
73 | set => !set.find(alternative => !alternative.find(key => !config.has(key))) | |
74 | ) | |
75 | ||
76 | missingAlternatives | |
77 | .forEach(set => set[0].forEach(key => miss.push(key))) | |
78 | ||
79 | return miss | |
80 | } | |
81 | ||
82 | // Check the available codecs | |
83 | // We get CONFIG by param to not import it in this file (import orders) | |
84 | async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) { | |
85 | const Ffmpeg = require('fluent-ffmpeg') | |
86 | const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs) | |
87 | ||
88 | const codecs = await getAvailableCodecsPromise() | |
89 | if (CONFIG.TRANSCODING.ENABLED === false) return undefined | |
90 | ||
91 | const canEncode = [ 'libx264' ] | |
92 | for (const codec of canEncode) { | |
93 | if (codecs[codec] === undefined) { | |
94 | throw new Error('Unknown codec ' + codec + ' in FFmpeg.') | |
95 | } | |
96 | ||
97 | if (codecs[codec].canEncode !== true) { | |
98 | throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg') | |
99 | } | |
100 | } | |
101 | } | |
102 | ||
103 | // We get db by param to not import it in this file (import orders) | |
104 | async function clientsExist () { | |
105 | const totalClients = await OAuthClientModel.countTotal() | |
106 | ||
107 | return totalClients !== 0 | |
108 | } | |
109 | ||
110 | // We get db by param to not import it in this file (import orders) | |
111 | async function usersExist () { | |
112 | const totalUsers = await UserModel.countTotal() | |
113 | ||
114 | return totalUsers !== 0 | |
115 | } | |
116 | ||
117 | // We get db by param to not import it in this file (import orders) | |
118 | async function applicationExist () { | |
119 | const totalApplication = await ApplicationModel.countTotal() | |
120 | ||
121 | return totalApplication !== 0 | |
122 | } | |
123 | ||
124 | // --------------------------------------------------------------------------- | |
125 | ||
126 | export { | |
127 | checkConfig, | |
128 | checkFFmpeg, | |
129 | checkMissedConfig, | |
130 | clientsExist, | |
131 | usersExist, | |
132 | applicationExist, | |
133 | checkActivityPubUrls | |
134 | } |