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