]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/constants.ts
Mobile version: menu full width and autoclosed
[github/Chocobozzz/PeerTube.git] / server / initializers / constants.ts
CommitLineData
fd206f0b
C
1import { IConfig } from 'config'
2import { dirname, join } from 'path'
94a5ff8a 3import { JobType, VideoRateType } from '../../shared/models'
50d6de9c 4import { ActivityPubActorType } from '../../shared/models/activitypub'
225a89c2 5import { FollowState } from '../../shared/models/actors'
3fd3ab2d 6import { VideoPrivacy } from '../../shared/models/videos'
e02643f3 7// Do not use barrels, remain constants as independent as possible
fd206f0b
C
8import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
9
10// Use a variable to reload the configuration if we need
11let config: IConfig = require('config')
ee9e7b61 12
9f6bae3a
C
13// ---------------------------------------------------------------------------
14
4805cff1 15const LAST_MIGRATION_VERSION = 190
5804c0db
C
16
17// ---------------------------------------------------------------------------
18
9f6bae3a 19// API version
f0f5567b 20const API_VERSION = 'v1'
9f10b292 21
9f6bae3a
C
22// Number of results by default for the pagination
23const PAGINATION_COUNT_DEFAULT = 15
24
9f6bae3a
C
25// Sortable columns per schema
26const SORTABLE_COLUMNS = {
9c2c18f3 27 USERS: [ 'id', 'username', 'createdAt' ],
265ba139 28 ACCOUNTS: [ 'createdAt' ],
94a5ff8a 29 JOBS: [ 'createdAt' ],
9c2c18f3 30 VIDEO_ABUSES: [ 'id', 'createdAt' ],
72c7248b 31 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
792dbaf0 32 VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ],
bf1f6508 33 VIDEO_COMMENT_THREADS: [ 'createdAt' ],
7a7724e6
C
34 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
35 FOLLOWERS: [ 'createdAt' ],
36 FOLLOWING: [ 'createdAt' ]
9f6bae3a 37}
9f10b292 38
2f372a86
C
39const OAUTH_LIFETIME = {
40 ACCESS_TOKEN: 3600 * 4, // 4 hours
41 REFRESH_TOKEN: 1209600 // 2 weeks
42}
43
9f6bae3a 44// ---------------------------------------------------------------------------
26d7d31b 45
60650c77
C
46// Number of points we add/remove after a successful/bad request
47const ACTOR_FOLLOW_SCORE = {
225a89c2
C
48 PENALTY: -10,
49 BONUS: 10,
60650c77
C
50 BASE: 1000,
51 MAX: 10000
225a89c2
C
52}
53
54const FOLLOW_STATES: { [ id: string ]: FollowState } = {
55 PENDING: 'pending',
56 ACCEPTED: 'accepted'
57}
58
59const REMOTE_SCHEME = {
60 HTTP: 'https',
61 WS: 'wss'
62}
63
94a5ff8a
C
64const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
65 'activitypub-http-broadcast': 5,
66 'activitypub-http-unicast': 5,
67 'activitypub-http-fetcher': 5,
ecb4e35f
C
68 'video-file': 1,
69 'email': 5
225a89c2 70}
94a5ff8a
C
71const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
72 'activitypub-http-broadcast': 1,
73 'activitypub-http-unicast': 5,
74 'activitypub-http-fetcher': 1,
ecb4e35f
C
75 'video-file': 1,
76 'email': 5
225a89c2 77}
94a5ff8a
C
78// 2 days
79const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2
225a89c2 80
60650c77
C
81// 1 hour
82let SCHEDULER_INTERVAL = 60000 * 60
83
225a89c2
C
84// ---------------------------------------------------------------------------
85
e861452f 86const CONFIG = {
fd206f0b 87 CUSTOM_FILE: getLocalConfigFilePath(),
d16b5172 88 LISTEN: {
65fcc311 89 PORT: config.get<number>('listen.port')
d16b5172 90 },
e861452f 91 DATABASE: {
65fcc311
C
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')
e861452f 97 },
94a5ff8a
C
98 REDIS: {
99 HOSTNAME: config.get<string>('redis.hostname'),
ecb4e35f 100 PORT: config.get<number>('redis.port'),
94a5ff8a
C
101 AUTH: config.get<string>('redis.auth')
102 },
ecb4e35f
C
103 SMTP: {
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')
111 },
e861452f 112 STORAGE: {
0b4204f9
C
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'))
e861452f
C
120 },
121 WEBSERVER: {
65fcc311
C
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'),
126 URL: '',
127 HOST: ''
4793c343 128 },
23e27dd5
C
129 LOG: {
130 LEVEL: config.get<string>('log.level')
131 },
4793c343 132 ADMIN: {
fd206f0b 133 get EMAIL () { return config.get<string>('admin.email') }
e22528ac
C
134 },
135 SIGNUP: {
fd206f0b
C
136 get ENABLED () { return config.get<boolean>('signup.enabled') },
137 get LIMIT () { return config.get<number>('signup.limit') }
b0f9f39e
C
138 },
139 USER: {
fd206f0b 140 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
227d02fe
C
141 },
142 TRANSCODING: {
fd206f0b
C
143 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
144 get THREADS () { return config.get<number>('transcoding.threads') },
40298b02 145 RESOLUTIONS: {
fd206f0b
C
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') }
40298b02 151 }
f981dae8
C
152 },
153 CACHE: {
154 PREVIEWS: {
fd206f0b 155 get SIZE () { return config.get<number>('cache.previews.size') }
f981dae8 156 }
e861452f
C
157 }
158}
e861452f 159
9f6bae3a
C
160// ---------------------------------------------------------------------------
161
e4c55619
C
162const CONSTRAINTS_FIELDS = {
163 USERS: {
164 USERNAME: { min: 3, max: 20 }, // Length
b0f9f39e
C
165 PASSWORD: { min: 6, max: 255 }, // Length
166 VIDEO_QUOTA: { min: -1 }
e4c55619 167 },
55fa55a9
C
168 VIDEO_ABUSES: {
169 REASON: { min: 2, max: 300 } // Length
170 },
72c7248b 171 VIDEO_CHANNELS: {
a265f7f3 172 NAME: { min: 3, max: 120 }, // Length
e34c85e5
C
173 DESCRIPTION: { min: 3, max: 250 }, // Length
174 URL: { min: 3, max: 2000 } // Length
72c7248b 175 },
e4c55619 176 VIDEOS: {
a265f7f3 177 NAME: { min: 3, max: 120 }, // Length
9567011b
C
178 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
179 DESCRIPTION: { min: 3, max: 3000 }, // Length
feb4bdfd 180 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
0e1dc3e7 181 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
c60774b0 182 DURATION: { min: 1 }, // Number
a265f7f3
C
183 TAGS: { min: 0, max: 5 }, // Number of total tags
184 TAG: { min: 2, max: 30 }, // Length
e4c55619 185 THUMBNAIL: { min: 2, max: 30 },
e4c87ec2
C
186 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
187 VIEWS: { min: 0 },
188 LIKES: { min: 0 },
93e1258c 189 DISLIKES: { min: 0 },
c60774b0 190 FILE_SIZE: { min: 10 },
e34c85e5
C
191 URL: { min: 3, max: 2000 } // Length
192 },
01de67b9 193 ACTORS: {
e34c85e5
C
194 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
195 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
c5911fd3
C
196 URL: { min: 3, max: 2000 }, // Length
197 AVATAR: {
01de67b9
C
198 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
199 FILE_SIZE: {
200 max: 2 * 1024 * 1024 // 2MB
201 }
c5911fd3 202 }
e4c87ec2
C
203 },
204 VIDEO_EVENTS: {
205 COUNT: { min: 0 }
6d852470 206 },
bf1f6508
C
207 VIDEO_COMMENTS: {
208 TEXT: { min: 2, max: 3000 }, // Length
6d852470 209 URL: { min: 3, max: 2000 } // Length
4ba3b8ea
C
210 },
211 VIDEO_SHARE: {
212 URL: { min: 3, max: 2000 } // Length
e4c55619
C
213 }
214}
215
ee9e7b61 216const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
d38b8281
C
217 LIKE: 'like',
218 DISLIKE: 'dislike'
219}
220
6e07c3de
C
221const VIDEO_CATEGORIES = {
222 1: 'Music',
223 2: 'Films',
224 3: 'Vehicles',
225 4: 'Art',
226 5: 'Sports',
227 6: 'Travels',
228 7: 'Gaming',
229 8: 'People',
230 9: 'Comedy',
231 10: 'Entertainment',
232 11: 'News',
40298b02 233 12: 'How To',
6e07c3de
C
234 13: 'Education',
235 14: 'Activism',
236 15: 'Science & Technology',
237 16: 'Animals',
238 17: 'Kids',
239 18: 'Food'
240}
241
6f0c39e2
C
242// See https://creativecommons.org/licenses/?lang=en
243const VIDEO_LICENCES = {
244 1: 'Attribution',
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'
251}
252
3092476e
C
253// See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
254const VIDEO_LANGUAGES = {
255 1: 'English',
256 2: 'Spanish',
257 3: 'Mandarin',
258 4: 'Hindi',
259 5: 'Arabic',
260 6: 'Portuguese',
261 7: 'Bengali',
262 8: 'Russian',
263 9: 'Japanese',
264 10: 'Punjabi',
265 11: 'German',
266 12: 'Korean',
267 13: 'French',
40298b02 268 14: 'Italian'
3092476e
C
269}
270
fd45e8f4
C
271const VIDEO_PRIVACIES = {
272 [VideoPrivacy.PUBLIC]: 'Public',
273 [VideoPrivacy.UNLISTED]: 'Unlisted',
274 [VideoPrivacy.PRIVATE]: 'Private'
275}
276
0d0e8dd0 277const VIDEO_MIMETYPE_EXT = {
efc32059
C
278 'video/webm': '.webm',
279 'video/ogg': '.ogv',
280 'video/mp4': '.mp4'
0d0e8dd0
C
281}
282
c5911fd3
C
283const AVATAR_MIMETYPE_EXT = {
284 'image/png': '.png',
285 'image/jpg': '.jpg',
286 'image/jpeg': '.jpg'
287}
288
9f6bae3a
C
289// ---------------------------------------------------------------------------
290
50d6de9c 291const SERVER_ACTOR_NAME = 'peertube'
350e31d6 292
e4f97bab 293const ACTIVITY_PUB = {
1b5b10d1
C
294 POTENTIAL_ACCEPT_HEADERS: [
295 'application/activity+json',
9a8cbd82
C
296 'application/ld+json',
297 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
165cdc75 298 ],
1b5b10d1 299 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
9a27cdc2 300 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
0d0e8dd0 301 COLLECTION_ITEMS_PER_PAGE: 10,
c986175d 302 FETCH_PAGE_LIMIT: 100,
20494f12 303 URL_MIME_TYPES: {
f05a1c30 304 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
20494f12
C
305 TORRENT: [ 'application/x-bittorrent' ],
306 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
a5625b41 307 },
2ccaeeb3 308 MAX_RECURSION_COMMENTS: 100,
7bc29171 309 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
e4f97bab
C
310}
311
50d6de9c
C
312const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
313 GROUP: 'Group',
314 PERSON: 'Person',
315 APPLICATION: 'Application'
316}
317
9f6bae3a
C
318// ---------------------------------------------------------------------------
319
e4f97bab 320const PRIVATE_RSA_KEY_SIZE = 2048
bdfbd4f1 321
9f6bae3a
C
322// Password encryption
323const BCRYPT_SALT_SIZE = 10
a877d5ac 324
ecb4e35f
C
325const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
326
bdfbd4f1
C
327// ---------------------------------------------------------------------------
328
052937db
C
329// Express static paths (router)
330const STATIC_PATHS = {
f285faa0
C
331 PREVIEWS: '/static/previews/',
332 THUMBNAILS: '/static/thumbnails/',
052937db 333 TORRENTS: '/static/torrents/',
c5911fd3
C
334 WEBSEED: '/static/webseed/',
335 AVATARS: '/static/avatars/'
052937db
C
336}
337
dc009132
C
338// Cache control
339let STATIC_MAX_AGE = '30d'
340
cbe2f7c3 341// Videos thumbnail size
d8755eed
C
342const THUMBNAILS_SIZE = {
343 width: 200,
344 height: 110
345}
346const PREVIEWS_SIZE = {
164174a6
C
347 width: 560,
348 height: 315
349}
e8e12200
C
350const AVATARS_SIZE = {
351 width: 120,
352 height: 120
353}
164174a6
C
354
355const EMBED_SIZE = {
356 width: 560,
357 height: 315
d8755eed 358}
cbe2f7c3 359
980246ea 360// Sub folders of cache directory
f981dae8
C
361const CACHE = {
362 DIRECTORIES: {
363 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
364 }
365}
366
e12a0092 367const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
4f491371 368
bdfbd4f1
C
369// ---------------------------------------------------------------------------
370
d8755eed 371const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
709756b8
C
372
373// ---------------------------------------------------------------------------
374
9f10b292
C
375// Special constants for a test instance
376if (isTestInstance() === true) {
60650c77 377 ACTOR_FOLLOW_SCORE.BASE = 20
f285faa0
C
378 REMOTE_SCHEME.HTTP = 'http'
379 REMOTE_SCHEME.WS = 'ws'
65fcc311 380 STATIC_MAX_AGE = '0'
c46edbc2 381 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
7bc29171 382 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
01de67b9 383 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
60650c77 384 SCHEDULER_INTERVAL = 10000
9f10b292
C
385}
386
fd206f0b 387updateWebserverConfig()
225a89c2 388
9f10b292
C
389// ---------------------------------------------------------------------------
390
65fcc311 391export {
9f6bae3a 392 API_VERSION,
e8e12200 393 AVATARS_SIZE,
4f491371 394 ACCEPT_HEADERS,
9f6bae3a 395 BCRYPT_SALT_SIZE,
f981dae8 396 CACHE,
9f6bae3a
C
397 CONFIG,
398 CONSTRAINTS_FIELDS,
164174a6 399 EMBED_SIZE,
94a5ff8a
C
400 JOB_CONCURRENCY,
401 JOB_ATTEMPTS,
b769007f 402 LAST_MIGRATION_VERSION,
9f6bae3a 403 OAUTH_LIFETIME,
d8755eed 404 OPENGRAPH_AND_OEMBED_COMMENT,
9f6bae3a 405 PAGINATION_COUNT_DEFAULT,
60650c77 406 ACTOR_FOLLOW_SCORE,
f285faa0
C
407 PREVIEWS_SIZE,
408 REMOTE_SCHEME,
7a7724e6 409 FOLLOW_STATES,
50d6de9c 410 SERVER_ACTOR_NAME,
e4f97bab 411 PRIVATE_RSA_KEY_SIZE,
9f6bae3a 412 SORTABLE_COLUMNS,
dc009132 413 STATIC_MAX_AGE,
a6375e69 414 STATIC_PATHS,
e4f97bab 415 ACTIVITY_PUB,
50d6de9c 416 ACTIVITY_PUB_ACTOR_TYPES,
9f6bae3a 417 THUMBNAILS_SIZE,
6e07c3de 418 VIDEO_CATEGORIES,
3092476e 419 VIDEO_LANGUAGES,
fd45e8f4 420 VIDEO_PRIVACIES,
6f0c39e2 421 VIDEO_LICENCES,
0d0e8dd0 422 VIDEO_RATE_TYPES,
c5911fd3 423 VIDEO_MIMETYPE_EXT,
ecb4e35f 424 USER_PASSWORD_RESET_LIFETIME,
60650c77 425 AVATAR_MIMETYPE_EXT,
94a5ff8a
C
426 SCHEDULER_INTERVAL,
427 JOB_COMPLETED_LIFETIME
9f10b292 428}
fd206f0b
C
429
430// ---------------------------------------------------------------------------
431
432function getLocalConfigFilePath () {
433 const configSources = config.util.getConfigSources()
434 if (configSources.length === 0) throw new Error('Invalid config source.')
435
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}`
439
440 return join(dirname(configSources[ 0 ].name), filename + '.json')
441}
442
443function 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)
446}
447
448export function reloadConfig () {
449
450 function directory () {
451 if (process.env.NODE_CONFIG_DIR) {
452 return process.env.NODE_CONFIG_DIR
453 }
454
455 return join(root(), 'config')
456 }
457
458 function purge () {
459 for (const fileName in require.cache) {
460 if (-1 === fileName.indexOf(directory())) {
461 continue
462 }
463
464 delete require.cache[fileName]
465 }
466
467 delete require.cache[require.resolve('config')]
468 }
469
470 purge()
471
472 config = require('config')
473
474 updateWebserverConfig()
475}