1 import { IConfig } from 'config'
2 import { dirname, join } from 'path'
3 import { JobCategory, JobState, 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 = 175
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: [ 'id', '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_STATES: { [ id: string ]: JobState } = {
66 PROCESSING: 'processing',
70 const JOB_CATEGORIES: { [ id: string ]: JobCategory } = {
71 TRANSCODING: 'transcoding',
72 ACTIVITYPUB_HTTP: 'activitypub-http'
74 // How many maximum jobs we fetch from the database per cycle
75 const JOBS_FETCH_LIMIT_PER_CYCLE = {
80 let JOBS_FETCHING_INTERVAL = 60000
83 let SCHEDULER_INTERVAL = 60000 * 60
85 // ---------------------------------------------------------------------------
88 CUSTOM_FILE: getLocalConfigFilePath(),
90 PORT: config.get<number>('listen.port')
93 DBNAME: 'peertube' + config.get<string>('database.suffix'),
94 HOSTNAME: config.get<string>('database.hostname'),
95 PORT: config.get<number>('database.port'),
96 USERNAME: config.get<string>('database.username'),
97 PASSWORD: config.get<string>('database.password')
100 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
101 LOG_DIR: buildPath(config.get<string>('storage.logs')),
102 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
103 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
104 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
105 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
106 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
109 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
110 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
111 HOSTNAME: config.get<string>('webserver.hostname'),
112 PORT: config.get<number>('webserver.port'),
117 LEVEL: config.get<string>('log.level')
120 get EMAIL () { return config.get<string>('admin.email') }
123 get ENABLED () { return config.get<boolean>('signup.enabled') },
124 get LIMIT () { return config.get<number>('signup.limit') }
127 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
130 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
131 get THREADS () { return config.get<number>('transcoding.threads') },
133 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
134 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
135 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
136 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
137 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
142 get SIZE () { return config.get<number>('cache.previews.size') }
147 // ---------------------------------------------------------------------------
149 const CONSTRAINTS_FIELDS = {
151 USERNAME: { min: 3, max: 20 }, // Length
152 PASSWORD: { min: 6, max: 255 }, // Length
153 VIDEO_QUOTA: { min: -1 }
156 REASON: { min: 2, max: 300 } // Length
159 NAME: { min: 3, max: 120 }, // Length
160 DESCRIPTION: { min: 3, max: 250 }, // Length
161 URL: { min: 3, max: 2000 } // Length
164 NAME: { min: 3, max: 120 }, // Length
165 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
166 DESCRIPTION: { min: 3, max: 3000 }, // Length
167 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
168 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
169 DURATION: { min: 1 }, // Number
170 TAGS: { min: 0, max: 5 }, // Number of total tags
171 TAG: { min: 2, max: 30 }, // Length
172 THUMBNAIL: { min: 2, max: 30 },
173 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
176 DISLIKES: { min: 0 },
177 FILE_SIZE: { min: 10 },
178 URL: { min: 3, max: 2000 } // Length
181 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
182 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
183 URL: { min: 3, max: 2000 }, // Length
185 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
187 max: 2 * 1024 * 1024 // 2MB
195 TEXT: { min: 2, max: 3000 }, // Length
196 URL: { min: 3, max: 2000 } // Length
200 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
205 const VIDEO_CATEGORIES = {
220 15: 'Science & Technology',
226 // See https://creativecommons.org/licenses/?lang=en
227 const VIDEO_LICENCES = {
229 2: 'Attribution - Share Alike',
230 3: 'Attribution - No Derivatives',
231 4: 'Attribution - Non Commercial',
232 5: 'Attribution - Non Commercial - Share Alike',
233 6: 'Attribution - Non Commercial - No Derivatives',
234 7: 'Public Domain Dedication'
237 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
238 const VIDEO_LANGUAGES = {
255 const VIDEO_PRIVACIES = {
256 [VideoPrivacy.PUBLIC]: 'Public',
257 [VideoPrivacy.UNLISTED]: 'Unlisted',
258 [VideoPrivacy.PRIVATE]: 'Private'
261 const VIDEO_MIMETYPE_EXT = {
262 'video/webm': '.webm',
267 const AVATAR_MIMETYPE_EXT = {
273 // ---------------------------------------------------------------------------
275 const SERVER_ACTOR_NAME = 'peertube'
277 const ACTIVITY_PUB = {
278 POTENTIAL_ACCEPT_HEADERS: [
279 'application/activity+json',
280 'application/ld+json',
281 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
283 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
284 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
285 COLLECTION_ITEMS_PER_PAGE: 10,
286 FETCH_PAGE_LIMIT: 100,
289 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
290 TORRENT: [ 'application/x-bittorrent' ],
291 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
293 MAX_RECURSION_COMMENTS: 100,
294 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
297 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
300 APPLICATION: 'Application'
303 // ---------------------------------------------------------------------------
305 const PRIVATE_RSA_KEY_SIZE = 2048
307 // Password encryption
308 const BCRYPT_SALT_SIZE = 10
310 // ---------------------------------------------------------------------------
312 // Express static paths (router)
313 const STATIC_PATHS = {
314 PREVIEWS: '/static/previews/',
315 THUMBNAILS: '/static/thumbnails/',
316 TORRENTS: '/static/torrents/',
317 WEBSEED: '/static/webseed/',
318 AVATARS: '/static/avatars/'
322 let STATIC_MAX_AGE = '30d'
324 // Videos thumbnail size
325 const THUMBNAILS_SIZE = {
329 const PREVIEWS_SIZE = {
333 const AVATARS_SIZE = {
343 // Sub folders of cache directory
346 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
350 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
352 // ---------------------------------------------------------------------------
354 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
356 // ---------------------------------------------------------------------------
358 // Special constants for a test instance
359 if (isTestInstance() === true) {
360 ACTOR_FOLLOW_SCORE.BASE = 20
361 JOBS_FETCHING_INTERVAL = 1000
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 // ---------------------------------------------------------------------------
385 JOBS_FETCH_LIMIT_PER_CYCLE,
386 JOBS_FETCHING_INTERVAL,
388 LAST_MIGRATION_VERSION,
390 OPENGRAPH_AND_OEMBED_COMMENT,
391 PAGINATION_COUNT_DEFAULT,
397 PRIVATE_RSA_KEY_SIZE,
402 ACTIVITY_PUB_ACTOR_TYPES,
414 // ---------------------------------------------------------------------------
416 function getLocalConfigFilePath () {
417 const configSources = config.util.getConfigSources()
418 if (configSources.length === 0) throw new Error('Invalid config source.')
420 let filename = 'local'
421 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
422 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
424 return join(dirname(configSources[ 0 ].name), filename + '.json')
427 function updateWebserverConfig () {
428 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
429 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
432 export function reloadConfig () {
434 function directory () {
435 if (process.env.NODE_CONFIG_DIR) {
436 return process.env.NODE_CONFIG_DIR
439 return join(root(), 'config')
443 for (const fileName in require.cache) {
444 if (-1 === fileName.indexOf(directory())) {
448 delete require.cache[fileName]
451 delete require.cache[require.resolve('config')]
456 config = require('config')
458 updateWebserverConfig()