1 import { IConfig } from 'config'
2 import { dirname, join } from 'path'
3 import { JobType, VideoRateType } from '../../shared/models'
4 import { ActivityPubActorType } from '../../shared/models/activitypub'
5 import { FollowState } from '../../shared/models/actors'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 // Do not use barrels, remain constants as independent as possible
8 import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
10 // Use a variable to reload the configuration if we need
11 let config: IConfig = require('config')
13 // ---------------------------------------------------------------------------
15 const LAST_MIGRATION_VERSION = 190
17 // ---------------------------------------------------------------------------
20 const API_VERSION = 'v1'
22 // Number of results by default for the pagination
23 const PAGINATION_COUNT_DEFAULT = 15
25 // Sortable columns per schema
26 const SORTABLE_COLUMNS = {
27 USERS: [ 'id', 'username', 'createdAt' ],
28 ACCOUNTS: [ 'createdAt' ],
29 JOBS: [ 'createdAt' ],
30 VIDEO_ABUSES: [ 'id', 'createdAt' ],
31 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
32 VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ],
33 VIDEO_COMMENT_THREADS: [ 'createdAt' ],
34 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
35 FOLLOWERS: [ 'createdAt' ],
36 FOLLOWING: [ 'createdAt' ]
39 const OAUTH_LIFETIME = {
40 ACCESS_TOKEN: 3600 * 4, // 4 hours
41 REFRESH_TOKEN: 1209600 // 2 weeks
44 // ---------------------------------------------------------------------------
46 // Number of points we add/remove after a successful/bad request
47 const ACTOR_FOLLOW_SCORE = {
54 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
59 const REMOTE_SCHEME = {
64 const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
65 'activitypub-http-broadcast': 5,
66 'activitypub-http-unicast': 5,
67 'activitypub-http-fetcher': 5,
71 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
72 'activitypub-http-broadcast': 1,
73 'activitypub-http-unicast': 5,
74 'activitypub-http-fetcher': 1,
79 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2
82 let SCHEDULER_INTERVAL = 60000 * 60
84 // ---------------------------------------------------------------------------
87 CUSTOM_FILE: getLocalConfigFilePath(),
89 PORT: config.get<number>('listen.port')
92 DBNAME: 'peertube' + config.get<string>('database.suffix'),
93 HOSTNAME: config.get<string>('database.hostname'),
94 PORT: config.get<number>('database.port'),
95 USERNAME: config.get<string>('database.username'),
96 PASSWORD: config.get<string>('database.password')
99 HOSTNAME: config.get<string>('redis.hostname'),
100 PORT: config.get<number>('redis.port'),
101 AUTH: config.get<string>('redis.auth')
104 HOSTNAME: config.get<string>('smtp.hostname'),
105 PORT: config.get<number>('smtp.port'),
106 USERNAME: config.get<string>('smtp.username'),
107 PASSWORD: config.get<string>('smtp.password'),
108 TLS: config.get<boolean>('smtp.tls'),
109 CA_FILE: config.get<string>('smtp.ca_file'),
110 FROM_ADDRESS: config.get<string>('smtp.from_address')
113 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
114 LOG_DIR: buildPath(config.get<string>('storage.logs')),
115 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
116 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
117 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
118 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
119 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
122 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
123 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
124 HOSTNAME: config.get<string>('webserver.hostname'),
125 PORT: config.get<number>('webserver.port'),
130 LEVEL: config.get<string>('log.level')
133 get EMAIL () { return config.get<string>('admin.email') }
136 get ENABLED () { return config.get<boolean>('signup.enabled') },
137 get LIMIT () { return config.get<number>('signup.limit') }
140 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
143 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
144 get THREADS () { return config.get<number>('transcoding.threads') },
146 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
147 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
148 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
149 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
150 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
155 get SIZE () { return config.get<number>('cache.previews.size') }
160 // ---------------------------------------------------------------------------
162 const CONSTRAINTS_FIELDS = {
164 USERNAME: { min: 3, max: 20 }, // Length
165 PASSWORD: { min: 6, max: 255 }, // Length
166 VIDEO_QUOTA: { min: -1 }
169 REASON: { min: 2, max: 300 } // Length
172 NAME: { min: 3, max: 120 }, // Length
173 DESCRIPTION: { min: 3, max: 250 }, // Length
174 URL: { min: 3, max: 2000 } // Length
177 NAME: { min: 3, max: 120 }, // Length
178 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
179 DESCRIPTION: { min: 3, max: 3000 }, // Length
180 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
181 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
182 DURATION: { min: 1 }, // Number
183 TAGS: { min: 0, max: 5 }, // Number of total tags
184 TAG: { min: 2, max: 30 }, // Length
185 THUMBNAIL: { min: 2, max: 30 },
186 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
189 DISLIKES: { min: 0 },
190 FILE_SIZE: { min: 10 },
191 URL: { min: 3, max: 2000 } // Length
194 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
195 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
196 URL: { min: 3, max: 2000 }, // Length
198 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
200 max: 2 * 1024 * 1024 // 2MB
208 TEXT: { min: 2, max: 3000 }, // Length
209 URL: { min: 3, max: 2000 } // Length
212 URL: { min: 3, max: 2000 } // Length
216 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
221 const VIDEO_CATEGORIES = {
236 15: 'Science & Technology',
242 // See https://creativecommons.org/licenses/?lang=en
243 const VIDEO_LICENCES = {
245 2: 'Attribution - Share Alike',
246 3: 'Attribution - No Derivatives',
247 4: 'Attribution - Non Commercial',
248 5: 'Attribution - Non Commercial - Share Alike',
249 6: 'Attribution - Non Commercial - No Derivatives',
250 7: 'Public Domain Dedication'
253 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
254 const VIDEO_LANGUAGES = {
271 const VIDEO_PRIVACIES = {
272 [VideoPrivacy.PUBLIC]: 'Public',
273 [VideoPrivacy.UNLISTED]: 'Unlisted',
274 [VideoPrivacy.PRIVATE]: 'Private'
277 const VIDEO_MIMETYPE_EXT = {
278 'video/webm': '.webm',
283 const AVATAR_MIMETYPE_EXT = {
289 // ---------------------------------------------------------------------------
291 const SERVER_ACTOR_NAME = 'peertube'
293 const ACTIVITY_PUB = {
294 POTENTIAL_ACCEPT_HEADERS: [
295 'application/activity+json',
296 'application/ld+json',
297 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
299 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
300 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
301 COLLECTION_ITEMS_PER_PAGE: 10,
302 FETCH_PAGE_LIMIT: 100,
304 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
305 TORRENT: [ 'application/x-bittorrent' ],
306 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
308 MAX_RECURSION_COMMENTS: 100,
309 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
312 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
315 APPLICATION: 'Application'
318 // ---------------------------------------------------------------------------
320 const PRIVATE_RSA_KEY_SIZE = 2048
322 // Password encryption
323 const BCRYPT_SALT_SIZE = 10
325 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
327 // ---------------------------------------------------------------------------
329 // Express static paths (router)
330 const STATIC_PATHS = {
331 PREVIEWS: '/static/previews/',
332 THUMBNAILS: '/static/thumbnails/',
333 TORRENTS: '/static/torrents/',
334 WEBSEED: '/static/webseed/',
335 AVATARS: '/static/avatars/'
339 let STATIC_MAX_AGE = '30d'
341 // Videos thumbnail size
342 const THUMBNAILS_SIZE = {
346 const PREVIEWS_SIZE = {
350 const AVATARS_SIZE = {
360 // Sub folders of cache directory
363 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
367 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
369 // ---------------------------------------------------------------------------
371 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
373 // ---------------------------------------------------------------------------
375 // Special constants for a test instance
376 if (isTestInstance() === true) {
377 ACTOR_FOLLOW_SCORE.BASE = 20
378 REMOTE_SCHEME.HTTP = 'http'
379 REMOTE_SCHEME.WS = 'ws'
381 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
382 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
383 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
384 SCHEDULER_INTERVAL = 10000
387 updateWebserverConfig()
389 // ---------------------------------------------------------------------------
402 LAST_MIGRATION_VERSION,
404 OPENGRAPH_AND_OEMBED_COMMENT,
405 PAGINATION_COUNT_DEFAULT,
411 PRIVATE_RSA_KEY_SIZE,
416 ACTIVITY_PUB_ACTOR_TYPES,
424 USER_PASSWORD_RESET_LIFETIME,
427 JOB_COMPLETED_LIFETIME
430 // ---------------------------------------------------------------------------
432 function getLocalConfigFilePath () {
433 const configSources = config.util.getConfigSources()
434 if (configSources.length === 0) throw new Error('Invalid config source.')
436 let filename = 'local'
437 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
438 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
440 return join(dirname(configSources[ 0 ].name), filename + '.json')
443 function updateWebserverConfig () {
444 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
445 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
448 export function reloadConfig () {
450 function directory () {
451 if (process.env.NODE_CONFIG_DIR) {
452 return process.env.NODE_CONFIG_DIR
455 return join(root(), 'config')
459 for (const fileName in require.cache) {
460 if (-1 === fileName.indexOf(directory())) {
464 delete require.cache[fileName]
467 delete require.cache[require.resolve('config')]
472 config = require('config')
474 updateWebserverConfig()