]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/constants.ts
Fix migrations
[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,
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
4ba3b8ea
C
199 },
200 VIDEO_SHARE: {
201 URL: { min: 3, max: 2000 } // Length
e4c55619
C
202 }
203}
204
ee9e7b61 205const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
d38b8281
C
206 LIKE: 'like',
207 DISLIKE: 'dislike'
208}
209
6e07c3de
C
210const VIDEO_CATEGORIES = {
211 1: 'Music',
212 2: 'Films',
213 3: 'Vehicles',
214 4: 'Art',
215 5: 'Sports',
216 6: 'Travels',
217 7: 'Gaming',
218 8: 'People',
219 9: 'Comedy',
220 10: 'Entertainment',
221 11: 'News',
40298b02 222 12: 'How To',
6e07c3de
C
223 13: 'Education',
224 14: 'Activism',
225 15: 'Science & Technology',
226 16: 'Animals',
227 17: 'Kids',
228 18: 'Food'
229}
230
6f0c39e2
C
231// See https://creativecommons.org/licenses/?lang=en
232const VIDEO_LICENCES = {
233 1: 'Attribution',
234 2: 'Attribution - Share Alike',
235 3: 'Attribution - No Derivatives',
236 4: 'Attribution - Non Commercial',
237 5: 'Attribution - Non Commercial - Share Alike',
238 6: 'Attribution - Non Commercial - No Derivatives',
239 7: 'Public Domain Dedication'
240}
241
3092476e
C
242// See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
243const VIDEO_LANGUAGES = {
244 1: 'English',
245 2: 'Spanish',
246 3: 'Mandarin',
247 4: 'Hindi',
248 5: 'Arabic',
249 6: 'Portuguese',
250 7: 'Bengali',
251 8: 'Russian',
252 9: 'Japanese',
253 10: 'Punjabi',
254 11: 'German',
255 12: 'Korean',
256 13: 'French',
40298b02 257 14: 'Italian'
3092476e
C
258}
259
fd45e8f4
C
260const VIDEO_PRIVACIES = {
261 [VideoPrivacy.PUBLIC]: 'Public',
262 [VideoPrivacy.UNLISTED]: 'Unlisted',
263 [VideoPrivacy.PRIVATE]: 'Private'
264}
265
0d0e8dd0 266const VIDEO_MIMETYPE_EXT = {
efc32059
C
267 'video/webm': '.webm',
268 'video/ogg': '.ogv',
269 'video/mp4': '.mp4'
0d0e8dd0
C
270}
271
c5911fd3
C
272const AVATAR_MIMETYPE_EXT = {
273 'image/png': '.png',
274 'image/jpg': '.jpg',
275 'image/jpeg': '.jpg'
276}
277
9f6bae3a
C
278// ---------------------------------------------------------------------------
279
50d6de9c 280const SERVER_ACTOR_NAME = 'peertube'
350e31d6 281
e4f97bab 282const ACTIVITY_PUB = {
1b5b10d1
C
283 POTENTIAL_ACCEPT_HEADERS: [
284 'application/activity+json',
9a8cbd82
C
285 'application/ld+json',
286 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
165cdc75 287 ],
1b5b10d1 288 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
9a27cdc2 289 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
0d0e8dd0 290 COLLECTION_ITEMS_PER_PAGE: 10,
c986175d 291 FETCH_PAGE_LIMIT: 100,
20494f12 292 URL_MIME_TYPES: {
f05a1c30 293 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
20494f12
C
294 TORRENT: [ 'application/x-bittorrent' ],
295 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
a5625b41 296 },
2ccaeeb3 297 MAX_RECURSION_COMMENTS: 100,
7bc29171 298 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
e4f97bab
C
299}
300
50d6de9c
C
301const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
302 GROUP: 'Group',
303 PERSON: 'Person',
304 APPLICATION: 'Application'
305}
306
9f6bae3a
C
307// ---------------------------------------------------------------------------
308
e4f97bab 309const PRIVATE_RSA_KEY_SIZE = 2048
bdfbd4f1 310
9f6bae3a
C
311// Password encryption
312const BCRYPT_SALT_SIZE = 10
a877d5ac 313
bdfbd4f1
C
314// ---------------------------------------------------------------------------
315
052937db
C
316// Express static paths (router)
317const STATIC_PATHS = {
f285faa0
C
318 PREVIEWS: '/static/previews/',
319 THUMBNAILS: '/static/thumbnails/',
052937db 320 TORRENTS: '/static/torrents/',
c5911fd3
C
321 WEBSEED: '/static/webseed/',
322 AVATARS: '/static/avatars/'
052937db
C
323}
324
dc009132
C
325// Cache control
326let STATIC_MAX_AGE = '30d'
327
cbe2f7c3 328// Videos thumbnail size
d8755eed
C
329const THUMBNAILS_SIZE = {
330 width: 200,
331 height: 110
332}
333const PREVIEWS_SIZE = {
164174a6
C
334 width: 560,
335 height: 315
336}
e8e12200
C
337const AVATARS_SIZE = {
338 width: 120,
339 height: 120
340}
164174a6
C
341
342const EMBED_SIZE = {
343 width: 560,
344 height: 315
d8755eed 345}
cbe2f7c3 346
980246ea 347// Sub folders of cache directory
f981dae8
C
348const CACHE = {
349 DIRECTORIES: {
350 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
351 }
352}
353
e12a0092 354const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
4f491371 355
bdfbd4f1
C
356// ---------------------------------------------------------------------------
357
d8755eed 358const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
709756b8
C
359
360// ---------------------------------------------------------------------------
361
9f10b292
C
362// Special constants for a test instance
363if (isTestInstance() === true) {
60650c77 364 ACTOR_FOLLOW_SCORE.BASE = 20
f285faa0
C
365 REMOTE_SCHEME.HTTP = 'http'
366 REMOTE_SCHEME.WS = 'ws'
65fcc311 367 STATIC_MAX_AGE = '0'
c46edbc2 368 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
7bc29171 369 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
01de67b9 370 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
60650c77 371 SCHEDULER_INTERVAL = 10000
9f10b292
C
372}
373
fd206f0b 374updateWebserverConfig()
225a89c2 375
9f10b292
C
376// ---------------------------------------------------------------------------
377
65fcc311 378export {
9f6bae3a 379 API_VERSION,
e8e12200 380 AVATARS_SIZE,
4f491371 381 ACCEPT_HEADERS,
9f6bae3a 382 BCRYPT_SALT_SIZE,
f981dae8 383 CACHE,
9f6bae3a
C
384 CONFIG,
385 CONSTRAINTS_FIELDS,
164174a6 386 EMBED_SIZE,
94a5ff8a
C
387 JOB_CONCURRENCY,
388 JOB_ATTEMPTS,
b769007f 389 LAST_MIGRATION_VERSION,
9f6bae3a 390 OAUTH_LIFETIME,
d8755eed 391 OPENGRAPH_AND_OEMBED_COMMENT,
9f6bae3a 392 PAGINATION_COUNT_DEFAULT,
60650c77 393 ACTOR_FOLLOW_SCORE,
f285faa0
C
394 PREVIEWS_SIZE,
395 REMOTE_SCHEME,
7a7724e6 396 FOLLOW_STATES,
50d6de9c 397 SERVER_ACTOR_NAME,
e4f97bab 398 PRIVATE_RSA_KEY_SIZE,
9f6bae3a 399 SORTABLE_COLUMNS,
dc009132 400 STATIC_MAX_AGE,
a6375e69 401 STATIC_PATHS,
e4f97bab 402 ACTIVITY_PUB,
50d6de9c 403 ACTIVITY_PUB_ACTOR_TYPES,
9f6bae3a 404 THUMBNAILS_SIZE,
6e07c3de 405 VIDEO_CATEGORIES,
3092476e 406 VIDEO_LANGUAGES,
fd45e8f4 407 VIDEO_PRIVACIES,
6f0c39e2 408 VIDEO_LICENCES,
0d0e8dd0 409 VIDEO_RATE_TYPES,
c5911fd3 410 VIDEO_MIMETYPE_EXT,
60650c77 411 AVATAR_MIMETYPE_EXT,
94a5ff8a
C
412 SCHEDULER_INTERVAL,
413 JOB_COMPLETED_LIFETIME
9f10b292 414}
fd206f0b
C
415
416// ---------------------------------------------------------------------------
417
418function getLocalConfigFilePath () {
419 const configSources = config.util.getConfigSources()
420 if (configSources.length === 0) throw new Error('Invalid config source.')
421
422 let filename = 'local'
423 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
424 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
425
426 return join(dirname(configSources[ 0 ].name), filename + '.json')
427}
428
429function updateWebserverConfig () {
430 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
431 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
432}
433
434export function reloadConfig () {
435
436 function directory () {
437 if (process.env.NODE_CONFIG_DIR) {
438 return process.env.NODE_CONFIG_DIR
439 }
440
441 return join(root(), 'config')
442 }
443
444 function purge () {
445 for (const fileName in require.cache) {
446 if (-1 === fileName.indexOf(directory())) {
447 continue
448 }
449
450 delete require.cache[fileName]
451 }
452
453 delete require.cache[require.resolve('config')]
454 }
455
456 purge()
457
458 config = require('config')
459
460 updateWebserverConfig()
461}