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 = 195
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') }
159 get NAME () { return config.get<string>('instance.name') },
160 get DESCRIPTION () { return config.get<string>('instance.description') },
161 get TERMS () { return config.get<string>('instance.terms') }
165 // ---------------------------------------------------------------------------
167 const CONSTRAINTS_FIELDS = {
169 USERNAME: { min: 3, max: 20 }, // Length
170 PASSWORD: { min: 6, max: 255 }, // Length
171 DESCRIPTION: { min: 3, max: 250 }, // Length
172 VIDEO_QUOTA: { min: -1 }
175 REASON: { min: 2, max: 300 } // Length
178 NAME: { min: 3, max: 120 }, // Length
179 DESCRIPTION: { min: 3, max: 250 }, // Length
180 SUPPORT: { min: 3, max: 300 }, // Length
181 URL: { min: 3, max: 2000 } // Length
184 NAME: { min: 3, max: 120 }, // Length
185 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
186 DESCRIPTION: { min: 3, max: 10000 }, // Length
187 SUPPORT: { min: 3, max: 300 }, // Length
189 EXTNAME: [ '.jpg', '.jpeg' ],
191 max: 2 * 1024 * 1024 // 2MB
194 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
195 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
196 DURATION: { min: 1 }, // Number
197 TAGS: { min: 0, max: 5 }, // Number of total tags
198 TAG: { min: 2, max: 30 }, // Length
199 THUMBNAIL: { min: 2, max: 30 },
200 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
203 DISLIKES: { min: 0 },
204 FILE_SIZE: { min: 10 },
205 URL: { min: 3, max: 2000 } // Length
208 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
209 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
210 URL: { min: 3, max: 2000 }, // Length
212 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
214 max: 2 * 1024 * 1024 // 2MB
222 TEXT: { min: 2, max: 3000 }, // Length
223 URL: { min: 3, max: 2000 } // Length
226 URL: { min: 3, max: 2000 } // Length
230 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
235 const VIDEO_CATEGORIES = {
250 15: 'Science & Technology',
256 // See https://creativecommons.org/licenses/?lang=en
257 const VIDEO_LICENCES = {
259 2: 'Attribution - Share Alike',
260 3: 'Attribution - No Derivatives',
261 4: 'Attribution - Non Commercial',
262 5: 'Attribution - Non Commercial - Share Alike',
263 6: 'Attribution - Non Commercial - No Derivatives',
264 7: 'Public Domain Dedication'
267 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
268 const VIDEO_LANGUAGES = {
285 const VIDEO_PRIVACIES = {
286 [VideoPrivacy.PUBLIC]: 'Public',
287 [VideoPrivacy.UNLISTED]: 'Unlisted',
288 [VideoPrivacy.PRIVATE]: 'Private'
291 const VIDEO_MIMETYPE_EXT = {
292 'video/webm': '.webm',
297 const IMAGE_MIMETYPE_EXT = {
303 // ---------------------------------------------------------------------------
305 const SERVER_ACTOR_NAME = 'peertube'
307 const ACTIVITY_PUB = {
308 POTENTIAL_ACCEPT_HEADERS: [
309 'application/activity+json',
310 'application/ld+json',
311 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
313 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
314 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
315 COLLECTION_ITEMS_PER_PAGE: 10,
316 FETCH_PAGE_LIMIT: 100,
318 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
319 TORRENT: [ 'application/x-bittorrent' ],
320 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
322 MAX_RECURSION_COMMENTS: 100,
323 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
326 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
329 APPLICATION: 'Application'
332 // ---------------------------------------------------------------------------
334 const PRIVATE_RSA_KEY_SIZE = 2048
336 // Password encryption
337 const BCRYPT_SALT_SIZE = 10
339 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
341 // ---------------------------------------------------------------------------
343 // Express static paths (router)
344 const STATIC_PATHS = {
345 PREVIEWS: '/static/previews/',
346 THUMBNAILS: '/static/thumbnails/',
347 TORRENTS: '/static/torrents/',
348 WEBSEED: '/static/webseed/',
349 AVATARS: '/static/avatars/'
353 let STATIC_MAX_AGE = '30d'
355 // Videos thumbnail size
356 const THUMBNAILS_SIZE = {
360 const PREVIEWS_SIZE = {
364 const AVATARS_SIZE = {
374 // Sub folders of cache directory
377 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
381 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
383 // ---------------------------------------------------------------------------
385 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
387 // ---------------------------------------------------------------------------
389 // Special constants for a test instance
390 if (isTestInstance() === true) {
391 ACTOR_FOLLOW_SCORE.BASE = 20
392 REMOTE_SCHEME.HTTP = 'http'
393 REMOTE_SCHEME.WS = 'ws'
395 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
396 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
397 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
398 SCHEDULER_INTERVAL = 10000
401 updateWebserverConfig()
403 // ---------------------------------------------------------------------------
416 LAST_MIGRATION_VERSION,
418 OPENGRAPH_AND_OEMBED_COMMENT,
419 PAGINATION_COUNT_DEFAULT,
425 PRIVATE_RSA_KEY_SIZE,
430 ACTIVITY_PUB_ACTOR_TYPES,
438 USER_PASSWORD_RESET_LIFETIME,
441 JOB_COMPLETED_LIFETIME
444 // ---------------------------------------------------------------------------
446 function getLocalConfigFilePath () {
447 const configSources = config.util.getConfigSources()
448 if (configSources.length === 0) throw new Error('Invalid config source.')
450 let filename = 'local'
451 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
452 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
454 return join(dirname(configSources[ 0 ].name), filename + '.json')
457 function updateWebserverConfig () {
458 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
459 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
462 export function reloadConfig () {
464 function directory () {
465 if (process.env.NODE_CONFIG_DIR) {
466 return process.env.NODE_CONFIG_DIR
469 return join(root(), 'config')
473 for (const fileName in require.cache) {
474 if (-1 === fileName.indexOf(directory())) {
478 delete require.cache[fileName]
481 delete require.cache[require.resolve('config')]
486 config = require('config')
488 updateWebserverConfig()