require -> import
[oweals/peertube.git] / server / initializers / checker.ts
1 import * as config from 'config'
2
3 import { database as db } from './database'
4 import { CONFIG } from './constants'
5
6 // Some checks on configuration files
7 function checkConfig () {
8   if (config.has('webserver.host')) {
9     let errorMessage = '`host` config key was renamed to `hostname` but it seems you still have a `host` key in your configuration files!'
10     errorMessage += ' Please ensure to rename your `host` configuration to `hostname`.'
11
12     return errorMessage
13   }
14
15   return null
16 }
17
18 // Check the config files
19 function checkMissedConfig () {
20   const required = [ 'listen.port',
21     'webserver.https', 'webserver.hostname', 'webserver.port',
22     'database.hostname', 'database.port', 'database.suffix', 'database.username', 'database.password',
23     'storage.certs', 'storage.videos', 'storage.logs', 'storage.thumbnails', 'storage.previews',
24     'admin.email', 'signup.enabled', 'transcoding.enabled', 'transcoding.threads'
25   ]
26   const miss = []
27
28   for (const key of required) {
29     if (!config.has(key)) {
30       miss.push(key)
31     }
32   }
33
34   return miss
35 }
36
37 // Check the available codecs
38 function checkFFmpeg (callback) {
39   const Ffmpeg = require('fluent-ffmpeg')
40
41   Ffmpeg.getAvailableCodecs(function (err, codecs) {
42     if (err) return callback(err)
43     if (CONFIG.TRANSCODING.ENABLED === false) return callback(null)
44
45     const canEncode = [ 'libx264' ]
46     canEncode.forEach(function (codec) {
47       if (codecs[codec] === undefined) {
48         return callback(new Error('Unknown codec ' + codec + ' in FFmpeg.'))
49       }
50
51       if (codecs[codec].canEncode !== true) {
52         return callback(new Error('Unavailable encode codec ' + codec + ' in FFmpeg'))
53       }
54     })
55
56     return callback(null)
57   })
58 }
59
60 function clientsExist (callback) {
61   db.OAuthClient.countTotal(function (err, totalClients) {
62     if (err) return callback(err)
63
64     return callback(null, totalClients !== 0)
65   })
66 }
67
68 function usersExist (callback) {
69   db.User.countTotal(function (err, totalUsers) {
70     if (err) return callback(err)
71
72     return callback(null, totalUsers !== 0)
73   })
74 }
75
76 // ---------------------------------------------------------------------------
77
78 export {
79   checkConfig,
80   checkFFmpeg,
81   checkMissedConfig,
82   clientsExist,
83   usersExist
84 }