]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/constants.ts
Only accept public comments
[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
94a5ff8a 15const LAST_MIGRATION_VERSION = 180
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,
68 'video-file': 1
225a89c2 69}
94a5ff8a
C
70const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
71 'activitypub-http-broadcast': 1,
72 'activitypub-http-unicast': 5,
73 'activitypub-http-fetcher': 1,
74 'video-file': 1
225a89c2 75}
94a5ff8a
C
76// 2 days
77const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2
225a89c2 78
60650c77
C
79// 1 hour
80let SCHEDULER_INTERVAL = 60000 * 60
81
225a89c2
C
82// ---------------------------------------------------------------------------
83
e861452f 84const CONFIG = {
fd206f0b 85 CUSTOM_FILE: getLocalConfigFilePath(),
d16b5172 86 LISTEN: {
65fcc311 87 PORT: config.get<number>('listen.port')
d16b5172 88 },
e861452f 89 DATABASE: {
65fcc311
C
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')
e861452f 95 },
94a5ff8a
C
96 REDIS: {
97 HOSTNAME: config.get<string>('redis.hostname'),
98 PORT: config.get<string>('redis.port'),
99 AUTH: config.get<string>('redis.auth')
100 },
e861452f 101 STORAGE: {
0b4204f9
C
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'))
e861452f
C
109 },
110 WEBSERVER: {
65fcc311
C
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'),
115 URL: '',
116 HOST: ''
4793c343 117 },
23e27dd5
C
118 LOG: {
119 LEVEL: config.get<string>('log.level')
120 },
4793c343 121 ADMIN: {
fd206f0b 122 get EMAIL () { return config.get<string>('admin.email') }
e22528ac
C
123 },
124 SIGNUP: {
fd206f0b
C
125 get ENABLED () { return config.get<boolean>('signup.enabled') },
126 get LIMIT () { return config.get<number>('signup.limit') }
b0f9f39e
C
127 },
128 USER: {
fd206f0b 129 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
227d02fe
C
130 },
131 TRANSCODING: {
fd206f0b
C
132 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
133 get THREADS () { return config.get<number>('transcoding.threads') },
40298b02 134 RESOLUTIONS: {
fd206f0b
C
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') }
40298b02 140 }
f981dae8
C
141 },
142 CACHE: {
143 PREVIEWS: {
fd206f0b 144 get SIZE () { return config.get<number>('cache.previews.size') }
f981dae8 145 }
e861452f
C
146 }
147}
e861452f 148
9f6bae3a
C
149// ---------------------------------------------------------------------------
150
e4c55619
C
151const CONSTRAINTS_FIELDS = {
152 USERS: {
153 USERNAME: { min: 3, max: 20 }, // Length
b0f9f39e
C
154 PASSWORD: { min: 6, max: 255 }, // Length
155 VIDEO_QUOTA: { min: -1 }
e4c55619 156 },
55fa55a9
C
157 VIDEO_ABUSES: {
158 REASON: { min: 2, max: 300 } // Length
159 },
72c7248b 160 VIDEO_CHANNELS: {
a265f7f3 161 NAME: { min: 3, max: 120 }, // Length
e34c85e5
C
162 DESCRIPTION: { min: 3, max: 250 }, // Length
163 URL: { min: 3, max: 2000 } // Length
72c7248b 164 },
e4c55619 165 VIDEOS: {
a265f7f3 166 NAME: { min: 3, max: 120 }, // Length
9567011b
C
167 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
168 DESCRIPTION: { min: 3, max: 3000 }, // Length
feb4bdfd 169 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
0e1dc3e7 170 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
c60774b0 171 DURATION: { min: 1 }, // Number
a265f7f3
C
172 TAGS: { min: 0, max: 5 }, // Number of total tags
173 TAG: { min: 2, max: 30 }, // Length
e4c55619 174 THUMBNAIL: { min: 2, max: 30 },
e4c87ec2
C
175 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
176 VIEWS: { min: 0 },
177 LIKES: { min: 0 },
93e1258c 178 DISLIKES: { min: 0 },
c60774b0 179 FILE_SIZE: { min: 10 },
e34c85e5
C
180 URL: { min: 3, max: 2000 } // Length
181 },
01de67b9 182 ACTORS: {
e34c85e5
C
183 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
184 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
c5911fd3
C
185 URL: { min: 3, max: 2000 }, // Length
186 AVATAR: {
01de67b9
C
187 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
188 FILE_SIZE: {
189 max: 2 * 1024 * 1024 // 2MB
190 }
c5911fd3 191 }
e4c87ec2
C
192 },
193 VIDEO_EVENTS: {
194 COUNT: { min: 0 }
6d852470 195 },
bf1f6508
C
196 VIDEO_COMMENTS: {
197 TEXT: { min: 2, max: 3000 }, // Length
6d852470 198 URL: { min: 3, max: 2000 } // Length
e4c55619
C
199 }
200}
201
ee9e7b61 202const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
d38b8281
C
203 LIKE: 'like',
204 DISLIKE: 'dislike'
205}
206
6e07c3de
C
207const VIDEO_CATEGORIES = {
208 1: 'Music',
209 2: 'Films',
210 3: 'Vehicles',
211 4: 'Art',
212 5: 'Sports',
213 6: 'Travels',
214 7: 'Gaming',
215 8: 'People',
216 9: 'Comedy',
217 10: 'Entertainment',
218 11: 'News',
40298b02 219 12: 'How To',
6e07c3de
C
220 13: 'Education',
221 14: 'Activism',
222 15: 'Science & Technology',
223 16: 'Animals',
224 17: 'Kids',
225 18: 'Food'
226}
227
6f0c39e2
C
228// See https://creativecommons.org/licenses/?lang=en
229const VIDEO_LICENCES = {
230 1: 'Attribution',
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'
237}
238
3092476e
C
239// See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
240const VIDEO_LANGUAGES = {
241 1: 'English',
242 2: 'Spanish',
243 3: 'Mandarin',
244 4: 'Hindi',
245 5: 'Arabic',
246 6: 'Portuguese',
247 7: 'Bengali',
248 8: 'Russian',
249 9: 'Japanese',
250 10: 'Punjabi',
251 11: 'German',
252 12: 'Korean',
253 13: 'French',
40298b02 254 14: 'Italian'
3092476e
C
255}
256
fd45e8f4
C
257const VIDEO_PRIVACIES = {
258 [VideoPrivacy.PUBLIC]: 'Public',
259 [VideoPrivacy.UNLISTED]: 'Unlisted',
260 [VideoPrivacy.PRIVATE]: 'Private'
261}
262
0d0e8dd0 263const VIDEO_MIMETYPE_EXT = {
efc32059
C
264 'video/webm': '.webm',
265 'video/ogg': '.ogv',
266 'video/mp4': '.mp4'
0d0e8dd0
C
267}
268
c5911fd3
C
269const AVATAR_MIMETYPE_EXT = {
270 'image/png': '.png',
271 'image/jpg': '.jpg',
272 'image/jpeg': '.jpg'
273}
274
9f6bae3a
C
275// ---------------------------------------------------------------------------
276
50d6de9c 277const SERVER_ACTOR_NAME = 'peertube'
350e31d6 278
e4f97bab 279const ACTIVITY_PUB = {
1b5b10d1
C
280 POTENTIAL_ACCEPT_HEADERS: [
281 'application/activity+json',
9a8cbd82
C
282 'application/ld+json',
283 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
165cdc75 284 ],
1b5b10d1 285 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
9a27cdc2 286 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
0d0e8dd0 287 COLLECTION_ITEMS_PER_PAGE: 10,
c986175d 288 FETCH_PAGE_LIMIT: 100,
20494f12 289 URL_MIME_TYPES: {
f05a1c30 290 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
20494f12
C
291 TORRENT: [ 'application/x-bittorrent' ],
292 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
a5625b41 293 },
2ccaeeb3 294 MAX_RECURSION_COMMENTS: 100,
7bc29171 295 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
e4f97bab
C
296}
297
50d6de9c
C
298const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
299 GROUP: 'Group',
300 PERSON: 'Person',
301 APPLICATION: 'Application'
302}
303
9f6bae3a
C
304// ---------------------------------------------------------------------------
305
e4f97bab 306const PRIVATE_RSA_KEY_SIZE = 2048
bdfbd4f1 307
9f6bae3a
C
308// Password encryption
309const BCRYPT_SALT_SIZE = 10
a877d5ac 310
bdfbd4f1
C
311// ---------------------------------------------------------------------------
312
052937db
C
313// Express static paths (router)
314const STATIC_PATHS = {
f285faa0
C
315 PREVIEWS: '/static/previews/',
316 THUMBNAILS: '/static/thumbnails/',
052937db 317 TORRENTS: '/static/torrents/',
c5911fd3
C
318 WEBSEED: '/static/webseed/',
319 AVATARS: '/static/avatars/'
052937db
C
320}
321
dc009132
C
322// Cache control
323let STATIC_MAX_AGE = '30d'
324
cbe2f7c3 325// Videos thumbnail size
d8755eed
C
326const THUMBNAILS_SIZE = {
327 width: 200,
328 height: 110
329}
330const PREVIEWS_SIZE = {
164174a6
C
331 width: 560,
332 height: 315
333}
e8e12200
C
334const AVATARS_SIZE = {
335 width: 120,
336 height: 120
337}
164174a6
C
338
339const EMBED_SIZE = {
340 width: 560,
341 height: 315
d8755eed 342}
cbe2f7c3 343
980246ea 344// Sub folders of cache directory
f981dae8
C
345const CACHE = {
346 DIRECTORIES: {
347 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
348 }
349}
350
e12a0092 351const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
4f491371 352
bdfbd4f1
C
353// ---------------------------------------------------------------------------
354
d8755eed 355const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
709756b8
C
356
357// ---------------------------------------------------------------------------
358
9f10b292
C
359// Special constants for a test instance
360if (isTestInstance() === true) {
60650c77 361 ACTOR_FOLLOW_SCORE.BASE = 20
f285faa0
C
362 REMOTE_SCHEME.HTTP = 'http'
363 REMOTE_SCHEME.WS = 'ws'
65fcc311 364 STATIC_MAX_AGE = '0'
c46edbc2 365 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
7bc29171 366 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
01de67b9 367 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
60650c77 368 SCHEDULER_INTERVAL = 10000
9f10b292
C
369}
370
fd206f0b 371updateWebserverConfig()
225a89c2 372
9f10b292
C
373// ---------------------------------------------------------------------------
374
65fcc311 375export {
9f6bae3a 376 API_VERSION,
e8e12200 377 AVATARS_SIZE,
4f491371 378 ACCEPT_HEADERS,
9f6bae3a 379 BCRYPT_SALT_SIZE,
f981dae8 380 CACHE,
9f6bae3a
C
381 CONFIG,
382 CONSTRAINTS_FIELDS,
164174a6 383 EMBED_SIZE,
94a5ff8a
C
384 JOB_CONCURRENCY,
385 JOB_ATTEMPTS,
b769007f 386 LAST_MIGRATION_VERSION,
9f6bae3a 387 OAUTH_LIFETIME,
d8755eed 388 OPENGRAPH_AND_OEMBED_COMMENT,
9f6bae3a 389 PAGINATION_COUNT_DEFAULT,
60650c77 390 ACTOR_FOLLOW_SCORE,
f285faa0
C
391 PREVIEWS_SIZE,
392 REMOTE_SCHEME,
7a7724e6 393 FOLLOW_STATES,
50d6de9c 394 SERVER_ACTOR_NAME,
e4f97bab 395 PRIVATE_RSA_KEY_SIZE,
9f6bae3a 396 SORTABLE_COLUMNS,
dc009132 397 STATIC_MAX_AGE,
a6375e69 398 STATIC_PATHS,
e4f97bab 399 ACTIVITY_PUB,
50d6de9c 400 ACTIVITY_PUB_ACTOR_TYPES,
9f6bae3a 401 THUMBNAILS_SIZE,
6e07c3de 402 VIDEO_CATEGORIES,
3092476e 403 VIDEO_LANGUAGES,
fd45e8f4 404 VIDEO_PRIVACIES,
6f0c39e2 405 VIDEO_LICENCES,
0d0e8dd0 406 VIDEO_RATE_TYPES,
c5911fd3 407 VIDEO_MIMETYPE_EXT,
60650c77 408 AVATAR_MIMETYPE_EXT,
94a5ff8a
C
409 SCHEDULER_INTERVAL,
410 JOB_COMPLETED_LIFETIME
9f10b292 411}
fd206f0b
C
412
413// ---------------------------------------------------------------------------
414
415function getLocalConfigFilePath () {
416 const configSources = config.util.getConfigSources()
417 if (configSources.length === 0) throw new Error('Invalid config source.')
418
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}`
422
423 return join(dirname(configSources[ 0 ].name), filename + '.json')
424}
425
426function 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)
429}
430
431export function reloadConfig () {
432
433 function directory () {
434 if (process.env.NODE_CONFIG_DIR) {
435 return process.env.NODE_CONFIG_DIR
436 }
437
438 return join(root(), 'config')
439 }
440
441 function purge () {
442 for (const fileName in require.cache) {
443 if (-1 === fileName.indexOf(directory())) {
444 continue
445 }
446
447 delete require.cache[fileName]
448 }
449
450 delete require.cache[require.resolve('config')]
451 }
452
453 purge()
454
455 config = require('config')
456
457 updateWebserverConfig()
458}