Add concept of video state, and add ability to wait transcoding before
[oweals/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', 'redis.db',
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',
31     'signup.enabled', 'signup.limit', 'signup.filters.cidr.whitelist', 'signup.filters.cidr.blacklist',
32     'transcoding.enabled', 'transcoding.threads',
33     'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route',
34     'instance.default_nsfw_policy', 'instance.robots',
35     'services.twitter.username', 'services.twitter.whitelisted'
36   ]
37   const miss: string[] = []
38
39   for (const key of required) {
40     if (!config.has(key)) {
41       miss.push(key)
42     }
43   }
44
45   return miss
46 }
47
48 // Check the available codecs
49 // We get CONFIG by param to not import it in this file (import orders)
50 async function checkFFmpeg (CONFIG: { TRANSCODING: { ENABLED: boolean } }) {
51   const Ffmpeg = require('fluent-ffmpeg')
52   const getAvailableCodecsPromise = promisify0(Ffmpeg.getAvailableCodecs)
53
54   const codecs = await getAvailableCodecsPromise()
55   if (CONFIG.TRANSCODING.ENABLED === false) return undefined
56
57   const canEncode = [ 'libx264' ]
58   for (const codec of canEncode) {
59     if (codecs[codec] === undefined) {
60       throw new Error('Unknown codec ' + codec + ' in FFmpeg.')
61     }
62
63     if (codecs[codec].canEncode !== true) {
64       throw new Error('Unavailable encode codec ' + codec + ' in FFmpeg')
65     }
66   }
67 }
68
69 // We get db by param to not import it in this file (import orders)
70 async function clientsExist () {
71   const totalClients = await OAuthClientModel.countTotal()
72
73   return totalClients !== 0
74 }
75
76 // We get db by param to not import it in this file (import orders)
77 async function usersExist () {
78   const totalUsers = await UserModel.countTotal()
79
80   return totalUsers !== 0
81 }
82
83 // We get db by param to not import it in this file (import orders)
84 async function applicationExist () {
85   const totalApplication = await ApplicationModel.countTotal()
86
87   return totalApplication !== 0
88 }
89
90 // ---------------------------------------------------------------------------
91
92 export {
93   checkConfig,
94   checkFFmpeg,
95   checkMissedConfig,
96   clientsExist,
97   usersExist,
98   applicationExist
99 }