]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/constants.ts
Fix resolution for portrait videos
[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
2422c46b 15const LAST_MIGRATION_VERSION = 195
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 }
66b16caf
C
157 },
158 INSTANCE: {
159 get NAME () { return config.get<string>('instance.name') },
160 get DESCRIPTION () { return config.get<string>('instance.description') },
00b5556c
C
161 get TERMS () { return config.get<string>('instance.terms') },
162 CUSTOMIZATIONS: {
163 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
164 get CSS () { return config.get<string>('instance.customizations.css') }
165 }
e861452f
C
166 }
167}
e861452f 168
9f6bae3a
C
169// ---------------------------------------------------------------------------
170
e4c55619
C
171const CONSTRAINTS_FIELDS = {
172 USERS: {
173 USERNAME: { min: 3, max: 20 }, // Length
b0f9f39e 174 PASSWORD: { min: 6, max: 255 }, // Length
2422c46b 175 DESCRIPTION: { min: 3, max: 250 }, // Length
b0f9f39e 176 VIDEO_QUOTA: { min: -1 }
e4c55619 177 },
55fa55a9
C
178 VIDEO_ABUSES: {
179 REASON: { min: 2, max: 300 } // Length
180 },
72c7248b 181 VIDEO_CHANNELS: {
a265f7f3 182 NAME: { min: 3, max: 120 }, // Length
e34c85e5 183 DESCRIPTION: { min: 3, max: 250 }, // Length
2422c46b 184 SUPPORT: { min: 3, max: 300 }, // Length
e34c85e5 185 URL: { min: 3, max: 2000 } // Length
72c7248b 186 },
e4c55619 187 VIDEOS: {
a265f7f3 188 NAME: { min: 3, max: 120 }, // Length
9567011b 189 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
2422c46b
C
190 DESCRIPTION: { min: 3, max: 10000 }, // Length
191 SUPPORT: { min: 3, max: 300 }, // Length
ac81d1a0
C
192 IMAGE: {
193 EXTNAME: [ '.jpg', '.jpeg' ],
194 FILE_SIZE: {
195 max: 2 * 1024 * 1024 // 2MB
196 }
197 },
feb4bdfd 198 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
0e1dc3e7 199 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
c60774b0 200 DURATION: { min: 1 }, // Number
a265f7f3
C
201 TAGS: { min: 0, max: 5 }, // Number of total tags
202 TAG: { min: 2, max: 30 }, // Length
e4c55619 203 THUMBNAIL: { min: 2, max: 30 },
e4c87ec2
C
204 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
205 VIEWS: { min: 0 },
206 LIKES: { min: 0 },
93e1258c 207 DISLIKES: { min: 0 },
c60774b0 208 FILE_SIZE: { min: 10 },
e34c85e5
C
209 URL: { min: 3, max: 2000 } // Length
210 },
01de67b9 211 ACTORS: {
e34c85e5
C
212 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
213 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
c5911fd3
C
214 URL: { min: 3, max: 2000 }, // Length
215 AVATAR: {
01de67b9
C
216 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
217 FILE_SIZE: {
218 max: 2 * 1024 * 1024 // 2MB
219 }
c5911fd3 220 }
e4c87ec2
C
221 },
222 VIDEO_EVENTS: {
223 COUNT: { min: 0 }
6d852470 224 },
bf1f6508
C
225 VIDEO_COMMENTS: {
226 TEXT: { min: 2, max: 3000 }, // Length
6d852470 227 URL: { min: 3, max: 2000 } // Length
4ba3b8ea
C
228 },
229 VIDEO_SHARE: {
230 URL: { min: 3, max: 2000 } // Length
e4c55619
C
231 }
232}
233
b5c0e955 234let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
73c69591 235const MAX_VIDEO_TRANSCODING_FPS = 30
b5c0e955 236
ee9e7b61 237const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
d38b8281
C
238 LIKE: 'like',
239 DISLIKE: 'dislike'
240}
241
6e07c3de
C
242const VIDEO_CATEGORIES = {
243 1: 'Music',
244 2: 'Films',
245 3: 'Vehicles',
246 4: 'Art',
247 5: 'Sports',
248 6: 'Travels',
249 7: 'Gaming',
250 8: 'People',
251 9: 'Comedy',
252 10: 'Entertainment',
253 11: 'News',
40298b02 254 12: 'How To',
6e07c3de
C
255 13: 'Education',
256 14: 'Activism',
257 15: 'Science & Technology',
258 16: 'Animals',
259 17: 'Kids',
260 18: 'Food'
261}
262
6f0c39e2
C
263// See https://creativecommons.org/licenses/?lang=en
264const VIDEO_LICENCES = {
265 1: 'Attribution',
266 2: 'Attribution - Share Alike',
267 3: 'Attribution - No Derivatives',
268 4: 'Attribution - Non Commercial',
269 5: 'Attribution - Non Commercial - Share Alike',
270 6: 'Attribution - Non Commercial - No Derivatives',
271 7: 'Public Domain Dedication'
272}
273
3092476e
C
274// See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
275const VIDEO_LANGUAGES = {
276 1: 'English',
277 2: 'Spanish',
278 3: 'Mandarin',
279 4: 'Hindi',
280 5: 'Arabic',
281 6: 'Portuguese',
282 7: 'Bengali',
283 8: 'Russian',
284 9: 'Japanese',
285 10: 'Punjabi',
286 11: 'German',
287 12: 'Korean',
288 13: 'French',
40298b02 289 14: 'Italian'
3092476e
C
290}
291
fd45e8f4
C
292const VIDEO_PRIVACIES = {
293 [VideoPrivacy.PUBLIC]: 'Public',
294 [VideoPrivacy.UNLISTED]: 'Unlisted',
295 [VideoPrivacy.PRIVATE]: 'Private'
296}
297
0d0e8dd0 298const VIDEO_MIMETYPE_EXT = {
efc32059
C
299 'video/webm': '.webm',
300 'video/ogg': '.ogv',
301 'video/mp4': '.mp4'
0d0e8dd0
C
302}
303
ac81d1a0 304const IMAGE_MIMETYPE_EXT = {
c5911fd3
C
305 'image/png': '.png',
306 'image/jpg': '.jpg',
307 'image/jpeg': '.jpg'
308}
309
9f6bae3a
C
310// ---------------------------------------------------------------------------
311
50d6de9c 312const SERVER_ACTOR_NAME = 'peertube'
350e31d6 313
e4f97bab 314const ACTIVITY_PUB = {
1b5b10d1
C
315 POTENTIAL_ACCEPT_HEADERS: [
316 'application/activity+json',
9a8cbd82
C
317 'application/ld+json',
318 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
165cdc75 319 ],
1b5b10d1 320 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
9a27cdc2 321 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
0d0e8dd0 322 COLLECTION_ITEMS_PER_PAGE: 10,
c986175d 323 FETCH_PAGE_LIMIT: 100,
20494f12 324 URL_MIME_TYPES: {
f05a1c30 325 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
20494f12
C
326 TORRENT: [ 'application/x-bittorrent' ],
327 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
a5625b41 328 },
2ccaeeb3 329 MAX_RECURSION_COMMENTS: 100,
7bc29171 330 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
e4f97bab
C
331}
332
50d6de9c
C
333const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
334 GROUP: 'Group',
335 PERSON: 'Person',
336 APPLICATION: 'Application'
337}
338
9f6bae3a
C
339// ---------------------------------------------------------------------------
340
e4f97bab 341const PRIVATE_RSA_KEY_SIZE = 2048
bdfbd4f1 342
9f6bae3a
C
343// Password encryption
344const BCRYPT_SALT_SIZE = 10
a877d5ac 345
ecb4e35f
C
346const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
347
bdfbd4f1
C
348// ---------------------------------------------------------------------------
349
052937db
C
350// Express static paths (router)
351const STATIC_PATHS = {
f285faa0
C
352 PREVIEWS: '/static/previews/',
353 THUMBNAILS: '/static/thumbnails/',
052937db 354 TORRENTS: '/static/torrents/',
c5911fd3
C
355 WEBSEED: '/static/webseed/',
356 AVATARS: '/static/avatars/'
052937db
C
357}
358
dc009132
C
359// Cache control
360let STATIC_MAX_AGE = '30d'
361
cbe2f7c3 362// Videos thumbnail size
d8755eed
C
363const THUMBNAILS_SIZE = {
364 width: 200,
365 height: 110
366}
367const PREVIEWS_SIZE = {
164174a6
C
368 width: 560,
369 height: 315
370}
e8e12200
C
371const AVATARS_SIZE = {
372 width: 120,
373 height: 120
374}
164174a6
C
375
376const EMBED_SIZE = {
377 width: 560,
378 height: 315
d8755eed 379}
cbe2f7c3 380
980246ea 381// Sub folders of cache directory
f981dae8
C
382const CACHE = {
383 DIRECTORIES: {
384 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
385 }
386}
387
e12a0092 388const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
4f491371 389
bdfbd4f1
C
390// ---------------------------------------------------------------------------
391
d8755eed 392const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
709756b8
C
393
394// ---------------------------------------------------------------------------
395
9f10b292
C
396// Special constants for a test instance
397if (isTestInstance() === true) {
60650c77 398 ACTOR_FOLLOW_SCORE.BASE = 20
f285faa0
C
399 REMOTE_SCHEME.HTTP = 'http'
400 REMOTE_SCHEME.WS = 'ws'
65fcc311 401 STATIC_MAX_AGE = '0'
c46edbc2 402 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
7bc29171 403 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
01de67b9 404 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
60650c77 405 SCHEDULER_INTERVAL = 10000
b5c0e955 406 VIDEO_VIEW_LIFETIME = 1000 // 1 second
9f10b292
C
407}
408
fd206f0b 409updateWebserverConfig()
225a89c2 410
9f10b292
C
411// ---------------------------------------------------------------------------
412
65fcc311 413export {
9f6bae3a 414 API_VERSION,
e8e12200 415 AVATARS_SIZE,
4f491371 416 ACCEPT_HEADERS,
9f6bae3a 417 BCRYPT_SALT_SIZE,
f981dae8 418 CACHE,
9f6bae3a
C
419 CONFIG,
420 CONSTRAINTS_FIELDS,
164174a6 421 EMBED_SIZE,
94a5ff8a
C
422 JOB_CONCURRENCY,
423 JOB_ATTEMPTS,
b769007f 424 LAST_MIGRATION_VERSION,
9f6bae3a 425 OAUTH_LIFETIME,
d8755eed 426 OPENGRAPH_AND_OEMBED_COMMENT,
9f6bae3a 427 PAGINATION_COUNT_DEFAULT,
60650c77 428 ACTOR_FOLLOW_SCORE,
f285faa0
C
429 PREVIEWS_SIZE,
430 REMOTE_SCHEME,
7a7724e6 431 FOLLOW_STATES,
50d6de9c 432 SERVER_ACTOR_NAME,
e4f97bab 433 PRIVATE_RSA_KEY_SIZE,
9f6bae3a 434 SORTABLE_COLUMNS,
dc009132 435 STATIC_MAX_AGE,
a6375e69 436 STATIC_PATHS,
e4f97bab 437 ACTIVITY_PUB,
50d6de9c 438 ACTIVITY_PUB_ACTOR_TYPES,
9f6bae3a 439 THUMBNAILS_SIZE,
6e07c3de 440 VIDEO_CATEGORIES,
3092476e 441 VIDEO_LANGUAGES,
fd45e8f4 442 VIDEO_PRIVACIES,
6f0c39e2 443 VIDEO_LICENCES,
0d0e8dd0 444 VIDEO_RATE_TYPES,
c5911fd3 445 VIDEO_MIMETYPE_EXT,
73c69591 446 MAX_VIDEO_TRANSCODING_FPS,
ecb4e35f 447 USER_PASSWORD_RESET_LIFETIME,
ac81d1a0 448 IMAGE_MIMETYPE_EXT,
94a5ff8a 449 SCHEDULER_INTERVAL,
b5c0e955
C
450 JOB_COMPLETED_LIFETIME,
451 VIDEO_VIEW_LIFETIME
9f10b292 452}
fd206f0b
C
453
454// ---------------------------------------------------------------------------
455
456function getLocalConfigFilePath () {
457 const configSources = config.util.getConfigSources()
458 if (configSources.length === 0) throw new Error('Invalid config source.')
459
460 let filename = 'local'
461 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
462 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
463
464 return join(dirname(configSources[ 0 ].name), filename + '.json')
465}
466
467function updateWebserverConfig () {
468 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
469 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
470}
471
472export function reloadConfig () {
473
474 function directory () {
475 if (process.env.NODE_CONFIG_DIR) {
476 return process.env.NODE_CONFIG_DIR
477 }
478
479 return join(root(), 'config')
480 }
481
482 function purge () {
483 for (const fileName in require.cache) {
484 if (-1 === fileName.indexOf(directory())) {
485 continue
486 }
487
488 delete require.cache[fileName]
489 }
490
491 delete require.cache[require.resolve('config')]
492 }
493
494 purge()
495
496 config = require('config')
497
498 updateWebserverConfig()
499}