]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/constants.ts
Mobile version: menu full width and autoclosed
[github/Chocobozzz/PeerTube.git] / server / initializers / constants.ts
1 import { IConfig } from 'config'
2 import { dirname, join } from 'path'
3 import { JobType, VideoRateType } from '../../shared/models'
4 import { ActivityPubActorType } from '../../shared/models/activitypub'
5 import { FollowState } from '../../shared/models/actors'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 // Do not use barrels, remain constants as independent as possible
8 import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
9
10 // Use a variable to reload the configuration if we need
11 let config: IConfig = require('config')
12
13 // ---------------------------------------------------------------------------
14
15 const LAST_MIGRATION_VERSION = 190
16
17 // ---------------------------------------------------------------------------
18
19 // API version
20 const API_VERSION = 'v1'
21
22 // Number of results by default for the pagination
23 const PAGINATION_COUNT_DEFAULT = 15
24
25 // Sortable columns per schema
26 const SORTABLE_COLUMNS = {
27 USERS: [ 'id', 'username', 'createdAt' ],
28 ACCOUNTS: [ 'createdAt' ],
29 JOBS: [ 'createdAt' ],
30 VIDEO_ABUSES: [ 'id', 'createdAt' ],
31 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
32 VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ],
33 VIDEO_COMMENT_THREADS: [ 'createdAt' ],
34 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
35 FOLLOWERS: [ 'createdAt' ],
36 FOLLOWING: [ 'createdAt' ]
37 }
38
39 const OAUTH_LIFETIME = {
40 ACCESS_TOKEN: 3600 * 4, // 4 hours
41 REFRESH_TOKEN: 1209600 // 2 weeks
42 }
43
44 // ---------------------------------------------------------------------------
45
46 // Number of points we add/remove after a successful/bad request
47 const ACTOR_FOLLOW_SCORE = {
48 PENALTY: -10,
49 BONUS: 10,
50 BASE: 1000,
51 MAX: 10000
52 }
53
54 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
55 PENDING: 'pending',
56 ACCEPTED: 'accepted'
57 }
58
59 const REMOTE_SCHEME = {
60 HTTP: 'https',
61 WS: 'wss'
62 }
63
64 const 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,
69 'email': 5
70 }
71 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
72 'activitypub-http-broadcast': 1,
73 'activitypub-http-unicast': 5,
74 'activitypub-http-fetcher': 1,
75 'video-file': 1,
76 'email': 5
77 }
78 // 2 days
79 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2
80
81 // 1 hour
82 let SCHEDULER_INTERVAL = 60000 * 60
83
84 // ---------------------------------------------------------------------------
85
86 const CONFIG = {
87 CUSTOM_FILE: getLocalConfigFilePath(),
88 LISTEN: {
89 PORT: config.get<number>('listen.port')
90 },
91 DATABASE: {
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')
97 },
98 REDIS: {
99 HOSTNAME: config.get<string>('redis.hostname'),
100 PORT: config.get<number>('redis.port'),
101 AUTH: config.get<string>('redis.auth')
102 },
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 },
112 STORAGE: {
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'))
120 },
121 WEBSERVER: {
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: ''
128 },
129 LOG: {
130 LEVEL: config.get<string>('log.level')
131 },
132 ADMIN: {
133 get EMAIL () { return config.get<string>('admin.email') }
134 },
135 SIGNUP: {
136 get ENABLED () { return config.get<boolean>('signup.enabled') },
137 get LIMIT () { return config.get<number>('signup.limit') }
138 },
139 USER: {
140 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
141 },
142 TRANSCODING: {
143 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
144 get THREADS () { return config.get<number>('transcoding.threads') },
145 RESOLUTIONS: {
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') }
151 }
152 },
153 CACHE: {
154 PREVIEWS: {
155 get SIZE () { return config.get<number>('cache.previews.size') }
156 }
157 }
158 }
159
160 // ---------------------------------------------------------------------------
161
162 const CONSTRAINTS_FIELDS = {
163 USERS: {
164 USERNAME: { min: 3, max: 20 }, // Length
165 PASSWORD: { min: 6, max: 255 }, // Length
166 VIDEO_QUOTA: { min: -1 }
167 },
168 VIDEO_ABUSES: {
169 REASON: { min: 2, max: 300 } // Length
170 },
171 VIDEO_CHANNELS: {
172 NAME: { min: 3, max: 120 }, // Length
173 DESCRIPTION: { min: 3, max: 250 }, // Length
174 URL: { min: 3, max: 2000 } // Length
175 },
176 VIDEOS: {
177 NAME: { min: 3, max: 120 }, // Length
178 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
179 DESCRIPTION: { min: 3, max: 3000 }, // Length
180 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
181 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
182 DURATION: { min: 1 }, // Number
183 TAGS: { min: 0, max: 5 }, // Number of total tags
184 TAG: { min: 2, max: 30 }, // Length
185 THUMBNAIL: { min: 2, max: 30 },
186 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
187 VIEWS: { min: 0 },
188 LIKES: { min: 0 },
189 DISLIKES: { min: 0 },
190 FILE_SIZE: { min: 10 },
191 URL: { min: 3, max: 2000 } // Length
192 },
193 ACTORS: {
194 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
195 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
196 URL: { min: 3, max: 2000 }, // Length
197 AVATAR: {
198 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
199 FILE_SIZE: {
200 max: 2 * 1024 * 1024 // 2MB
201 }
202 }
203 },
204 VIDEO_EVENTS: {
205 COUNT: { min: 0 }
206 },
207 VIDEO_COMMENTS: {
208 TEXT: { min: 2, max: 3000 }, // Length
209 URL: { min: 3, max: 2000 } // Length
210 },
211 VIDEO_SHARE: {
212 URL: { min: 3, max: 2000 } // Length
213 }
214 }
215
216 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
217 LIKE: 'like',
218 DISLIKE: 'dislike'
219 }
220
221 const 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',
233 12: 'How To',
234 13: 'Education',
235 14: 'Activism',
236 15: 'Science & Technology',
237 16: 'Animals',
238 17: 'Kids',
239 18: 'Food'
240 }
241
242 // See https://creativecommons.org/licenses/?lang=en
243 const 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
253 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
254 const 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',
268 14: 'Italian'
269 }
270
271 const VIDEO_PRIVACIES = {
272 [VideoPrivacy.PUBLIC]: 'Public',
273 [VideoPrivacy.UNLISTED]: 'Unlisted',
274 [VideoPrivacy.PRIVATE]: 'Private'
275 }
276
277 const VIDEO_MIMETYPE_EXT = {
278 'video/webm': '.webm',
279 'video/ogg': '.ogv',
280 'video/mp4': '.mp4'
281 }
282
283 const AVATAR_MIMETYPE_EXT = {
284 'image/png': '.png',
285 'image/jpg': '.jpg',
286 'image/jpeg': '.jpg'
287 }
288
289 // ---------------------------------------------------------------------------
290
291 const SERVER_ACTOR_NAME = 'peertube'
292
293 const ACTIVITY_PUB = {
294 POTENTIAL_ACCEPT_HEADERS: [
295 'application/activity+json',
296 'application/ld+json',
297 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
298 ],
299 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
300 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
301 COLLECTION_ITEMS_PER_PAGE: 10,
302 FETCH_PAGE_LIMIT: 100,
303 URL_MIME_TYPES: {
304 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
305 TORRENT: [ 'application/x-bittorrent' ],
306 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
307 },
308 MAX_RECURSION_COMMENTS: 100,
309 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
310 }
311
312 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
313 GROUP: 'Group',
314 PERSON: 'Person',
315 APPLICATION: 'Application'
316 }
317
318 // ---------------------------------------------------------------------------
319
320 const PRIVATE_RSA_KEY_SIZE = 2048
321
322 // Password encryption
323 const BCRYPT_SALT_SIZE = 10
324
325 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
326
327 // ---------------------------------------------------------------------------
328
329 // Express static paths (router)
330 const STATIC_PATHS = {
331 PREVIEWS: '/static/previews/',
332 THUMBNAILS: '/static/thumbnails/',
333 TORRENTS: '/static/torrents/',
334 WEBSEED: '/static/webseed/',
335 AVATARS: '/static/avatars/'
336 }
337
338 // Cache control
339 let STATIC_MAX_AGE = '30d'
340
341 // Videos thumbnail size
342 const THUMBNAILS_SIZE = {
343 width: 200,
344 height: 110
345 }
346 const PREVIEWS_SIZE = {
347 width: 560,
348 height: 315
349 }
350 const AVATARS_SIZE = {
351 width: 120,
352 height: 120
353 }
354
355 const EMBED_SIZE = {
356 width: 560,
357 height: 315
358 }
359
360 // Sub folders of cache directory
361 const CACHE = {
362 DIRECTORIES: {
363 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
364 }
365 }
366
367 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
368
369 // ---------------------------------------------------------------------------
370
371 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
372
373 // ---------------------------------------------------------------------------
374
375 // Special constants for a test instance
376 if (isTestInstance() === true) {
377 ACTOR_FOLLOW_SCORE.BASE = 20
378 REMOTE_SCHEME.HTTP = 'http'
379 REMOTE_SCHEME.WS = 'ws'
380 STATIC_MAX_AGE = '0'
381 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
382 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
383 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
384 SCHEDULER_INTERVAL = 10000
385 }
386
387 updateWebserverConfig()
388
389 // ---------------------------------------------------------------------------
390
391 export {
392 API_VERSION,
393 AVATARS_SIZE,
394 ACCEPT_HEADERS,
395 BCRYPT_SALT_SIZE,
396 CACHE,
397 CONFIG,
398 CONSTRAINTS_FIELDS,
399 EMBED_SIZE,
400 JOB_CONCURRENCY,
401 JOB_ATTEMPTS,
402 LAST_MIGRATION_VERSION,
403 OAUTH_LIFETIME,
404 OPENGRAPH_AND_OEMBED_COMMENT,
405 PAGINATION_COUNT_DEFAULT,
406 ACTOR_FOLLOW_SCORE,
407 PREVIEWS_SIZE,
408 REMOTE_SCHEME,
409 FOLLOW_STATES,
410 SERVER_ACTOR_NAME,
411 PRIVATE_RSA_KEY_SIZE,
412 SORTABLE_COLUMNS,
413 STATIC_MAX_AGE,
414 STATIC_PATHS,
415 ACTIVITY_PUB,
416 ACTIVITY_PUB_ACTOR_TYPES,
417 THUMBNAILS_SIZE,
418 VIDEO_CATEGORIES,
419 VIDEO_LANGUAGES,
420 VIDEO_PRIVACIES,
421 VIDEO_LICENCES,
422 VIDEO_RATE_TYPES,
423 VIDEO_MIMETYPE_EXT,
424 USER_PASSWORD_RESET_LIFETIME,
425 AVATAR_MIMETYPE_EXT,
426 SCHEDULER_INTERVAL,
427 JOB_COMPLETED_LIFETIME
428 }
429
430 // ---------------------------------------------------------------------------
431
432 function 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
443 function 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
448 export 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 }