]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/checker.ts
Add ability to choose what policy we have for NSFW videos
[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
7 // Some checks on configuration files
8 // Return an error message, or null if everything is okay
9 function checkConfig () {
10 const defaultNSFWPolicy = config.get<string>('instance.default_nsfw_policy')
11
12 if ([ 'do_not_list', 'blur', 'display' ].indexOf(defaultNSFWPolicy) === -1) {
13 return 'NSFW policy setting should be "do_not_list" or "blur" or "display" instead of ' + defaultNSFWPolicy
14 }
15
16 return null
17 }
18
19 // Check the config files
20 function checkMissedConfig () {
21 const required = [ 'listen.port', 'listen.hostname',
22 'webserver.https', 'webserver.hostname', 'webserver.port',
23 'trust_proxy',
24 'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password',
25 'redis.hostname', 'redis.port', 'redis.auth',
26 'smtp.hostname', 'smtp.port', 'smtp.username', 'smtp.password', 'smtp.tls', 'smtp.from_address',
27 'storage.avatars', 'storage.videos', 'storage.logs', 'storage.previews', 'storage.thumbnails', 'storage.torrents', 'storage.cache',
28 'log.level',
29 'user.video_quota',
30 'cache.previews.size', 'admin.email', 'signup.enabled', 'signup.limit', 'transcoding.enabled', 'transcoding.threads',
31 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
32 'instance.default_nsfw_policy'
33 ]
34 const miss: string[] = []
35
36 for (const key of required) {
37 if (!config.has(key)) {
38 miss.push(key)
39 }
40 }
41
42 return miss
43 }
44
45 // Check the available codecs
46 // We get CONFIG by param to not import it in this file (import orders)
47 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
48 const Ffmpeg = require('fluent-ffmpeg')
49 const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
50
51 const codecs = await getAvailableCodecsPromise()
52 if (CONFIG.TRANSCODING.ENABLED === false) return undefined
53
54 const canEncode = [ 'libx264' ]
55 for (const codec of canEncode) {
56 if (codecs[codec] === undefined) {
57 throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
58 }
59
60 if (codecs[codec].canEncode !== true) {
61 throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
62 }
63 }
64 }
65
66 // We get db by param to not import it in this file (import orders)
67 async function clientsExist () {
68 const totalClients = await OAuthClientModel.countTotal()
69
70 return totalClients !== 0
71 }
72
73 // We get db by param to not import it in this file (import orders)
74 async function usersExist () {
75 const totalUsers = await UserModel.countTotal()
76
77 return totalUsers !== 0
78 }
79
80 // We get db by param to not import it in this file (import orders)
81 async function applicationExist () {
82 const totalApplication = await ApplicationModel.countTotal()
83
84 return totalApplication !== 0
85 }
86
87 // ---------------------------------------------------------------------------
88
89 export {
90 checkConfig,
91 checkFFmpeg,
92 checkMissedConfig,
93 clientsExist,
94 usersExist,
95 applicationExist
96 }