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 = 180
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,
70 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
71 'activitypub-http-broadcast': 1,
72 'activitypub-http-unicast': 5,
73 'activitypub-http-fetcher': 1,
77 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2
80 let SCHEDULER_INTERVAL = 60000 * 60
82 // ---------------------------------------------------------------------------
85 CUSTOM_FILE: getLocalConfigFilePath(),
87 PORT: config.get<number>('listen.port')
90 DBNAME: 'peertube' + config.get<string>('database.suffix'),
91 HOSTNAME: config.get<string>('database.hostname'),
92 PORT: config.get<number>('database.port'),
93 USERNAME: config.get<string>('database.username'),
94 PASSWORD: config.get<string>('database.password')
97 HOSTNAME: config.get<string>('redis.hostname'),
98 PORT: config.get<string>('redis.port'),
99 AUTH: config.get<string>('redis.auth')
102 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
103 LOG_DIR: buildPath(config.get<string>('storage.logs')),
104 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
105 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
106 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
107 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
108 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
111 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
112 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
113 HOSTNAME: config.get<string>('webserver.hostname'),
114 PORT: config.get<number>('webserver.port'),
119 LEVEL: config.get<string>('log.level')
122 get EMAIL () { return config.get<string>('admin.email') }
125 get ENABLED () { return config.get<boolean>('signup.enabled') },
126 get LIMIT () { return config.get<number>('signup.limit') }
129 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
132 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
133 get THREADS () { return config.get<number>('transcoding.threads') },
135 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
136 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
137 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
138 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
139 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
144 get SIZE () { return config.get<number>('cache.previews.size') }
149 // ---------------------------------------------------------------------------
151 const CONSTRAINTS_FIELDS = {
153 USERNAME: { min: 3, max: 20 }, // Length
154 PASSWORD: { min: 6, max: 255 }, // Length
155 VIDEO_QUOTA: { min: -1 }
158 REASON: { min: 2, max: 300 } // Length
161 NAME: { min: 3, max: 120 }, // Length
162 DESCRIPTION: { min: 3, max: 250 }, // Length
163 URL: { min: 3, max: 2000 } // Length
166 NAME: { min: 3, max: 120 }, // Length
167 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
168 DESCRIPTION: { min: 3, max: 3000 }, // Length
169 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
170 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
171 DURATION: { min: 1 }, // Number
172 TAGS: { min: 0, max: 5 }, // Number of total tags
173 TAG: { min: 2, max: 30 }, // Length
174 THUMBNAIL: { min: 2, max: 30 },
175 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
178 DISLIKES: { min: 0 },
179 FILE_SIZE: { min: 10 },
180 URL: { min: 3, max: 2000 } // Length
183 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
184 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
185 URL: { min: 3, max: 2000 }, // Length
187 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
189 max: 2 * 1024 * 1024 // 2MB
197 TEXT: { min: 2, max: 3000 }, // Length
198 URL: { min: 3, max: 2000 } // Length
202 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
207 const VIDEO_CATEGORIES = {
222 15: 'Science & Technology',
228 // See https://creativecommons.org/licenses/?lang=en
229 const VIDEO_LICENCES = {
231 2: 'Attribution - Share Alike',
232 3: 'Attribution - No Derivatives',
233 4: 'Attribution - Non Commercial',
234 5: 'Attribution - Non Commercial - Share Alike',
235 6: 'Attribution - Non Commercial - No Derivatives',
236 7: 'Public Domain Dedication'
239 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
240 const VIDEO_LANGUAGES = {
257 const VIDEO_PRIVACIES = {
258 [VideoPrivacy.PUBLIC]: 'Public',
259 [VideoPrivacy.UNLISTED]: 'Unlisted',
260 [VideoPrivacy.PRIVATE]: 'Private'
263 const VIDEO_MIMETYPE_EXT = {
264 'video/webm': '.webm',
269 const AVATAR_MIMETYPE_EXT = {
275 // ---------------------------------------------------------------------------
277 const SERVER_ACTOR_NAME = 'peertube'
279 const ACTIVITY_PUB = {
280 POTENTIAL_ACCEPT_HEADERS: [
281 'application/activity+json',
282 'application/ld+json',
283 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
285 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
286 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
287 COLLECTION_ITEMS_PER_PAGE: 10,
288 FETCH_PAGE_LIMIT: 100,
290 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
291 TORRENT: [ 'application/x-bittorrent' ],
292 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
294 MAX_RECURSION_COMMENTS: 100,
295 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
298 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
301 APPLICATION: 'Application'
304 // ---------------------------------------------------------------------------
306 const PRIVATE_RSA_KEY_SIZE = 2048
308 // Password encryption
309 const BCRYPT_SALT_SIZE = 10
311 // ---------------------------------------------------------------------------
313 // Express static paths (router)
314 const STATIC_PATHS = {
315 PREVIEWS: '/static/previews/',
316 THUMBNAILS: '/static/thumbnails/',
317 TORRENTS: '/static/torrents/',
318 WEBSEED: '/static/webseed/',
319 AVATARS: '/static/avatars/'
323 let STATIC_MAX_AGE = '30d'
325 // Videos thumbnail size
326 const THUMBNAILS_SIZE = {
330 const PREVIEWS_SIZE = {
334 const AVATARS_SIZE = {
344 // Sub folders of cache directory
347 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
351 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
353 // ---------------------------------------------------------------------------
355 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
357 // ---------------------------------------------------------------------------
359 // Special constants for a test instance
360 if (isTestInstance() === true) {
361 ACTOR_FOLLOW_SCORE.BASE = 20
362 REMOTE_SCHEME.HTTP = 'http'
363 REMOTE_SCHEME.WS = 'ws'
365 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
366 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
367 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
368 SCHEDULER_INTERVAL = 10000
371 updateWebserverConfig()
373 // ---------------------------------------------------------------------------
386 LAST_MIGRATION_VERSION,
388 OPENGRAPH_AND_OEMBED_COMMENT,
389 PAGINATION_COUNT_DEFAULT,
395 PRIVATE_RSA_KEY_SIZE,
400 ACTIVITY_PUB_ACTOR_TYPES,
410 JOB_COMPLETED_LIFETIME
413 // ---------------------------------------------------------------------------
415 function getLocalConfigFilePath () {
416 const configSources = config.util.getConfigSources()
417 if (configSources.length === 0) throw new Error('Invalid config source.')
419 let filename = 'local'
420 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
421 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
423 return join(dirname(configSources[ 0 ].name), filename + '.json')
426 function updateWebserverConfig () {
427 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
428 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
431 export function reloadConfig () {
433 function directory () {
434 if (process.env.NODE_CONFIG_DIR) {
435 return process.env.NODE_CONFIG_DIR
438 return join(root(), 'config')
442 for (const fileName in require.cache) {
443 if (-1 === fileName.indexOf(directory())) {
447 delete require.cache[fileName]
450 delete require.cache[require.resolve('config')]
455 config = require('config')
457 updateWebserverConfig()