]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/constants.ts
59c30fdee32fadcd05a3f6025c361cb4074f3a4f
[github/Chocobozzz/PeerTube.git] / server / initializers / constants.ts
1 import { IConfig } from 'config'
2 import { dirname, join } from 'path'
3 import { JobType, VideoRateType, VideoState, VideosRedundancy } from '../../shared/models'
4 import { ActivityPubActorType } from '../../shared/models/activitypub'
5 import { FollowState } from '../../shared/models/actors'
6 import { VideoAbuseState, VideoImportState, VideoPrivacy, VideoTranscodingFPS } from '../../shared/models/videos'
7 // Do not use barrels, remain constants as independent as possible
8 import { buildPath, isTestInstance, parseDuration, parseBytes, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
9 import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
10 import { invert } from 'lodash'
11 import { CronRepeatOptions, EveryRepeatOptions } from 'bull'
12 import * as bytes from 'bytes'
13 import { VideoPlaylistPrivacy } from '../../shared/models/videos/playlist/video-playlist-privacy.model'
14 import { VideoPlaylistType } from '../../shared/models/videos/playlist/video-playlist-type.model'
15
16 // Use a variable to reload the configuration if we need
17 let config: IConfig = require('config')
18
19 // ---------------------------------------------------------------------------
20
21 const LAST_MIGRATION_VERSION = 340
22
23 // ---------------------------------------------------------------------------
24
25 // API version
26 const API_VERSION = 'v1'
27
28 const PAGINATION = {
29 COUNT: {
30 DEFAULT: 15,
31 MAX: 100
32 }
33 }
34
35 // Sortable columns per schema
36 const SORTABLE_COLUMNS = {
37 USERS: [ 'id', 'username', 'createdAt' ],
38 USER_SUBSCRIPTIONS: [ 'id', 'createdAt' ],
39 ACCOUNTS: [ 'createdAt' ],
40 JOBS: [ 'createdAt' ],
41 VIDEO_ABUSES: [ 'id', 'createdAt', 'state' ],
42 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
43 VIDEO_IMPORTS: [ 'createdAt' ],
44 VIDEO_COMMENT_THREADS: [ 'createdAt' ],
45 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
46 FOLLOWERS: [ 'createdAt' ],
47 FOLLOWING: [ 'createdAt' ],
48
49 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes', 'trending' ],
50
51 VIDEOS_SEARCH: [ 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes', 'match' ],
52 VIDEO_CHANNELS_SEARCH: [ 'match', 'displayName', 'createdAt' ],
53
54 ACCOUNTS_BLOCKLIST: [ 'createdAt' ],
55 SERVERS_BLOCKLIST: [ 'createdAt' ],
56
57 USER_NOTIFICATIONS: [ 'createdAt' ],
58
59 VIDEO_PLAYLISTS: [ 'createdAt' ]
60 }
61
62 const OAUTH_LIFETIME = {
63 ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
64 REFRESH_TOKEN: 1209600 // 2 weeks
65 }
66
67 const ROUTE_CACHE_LIFETIME = {
68 FEEDS: '15 minutes',
69 ROBOTS: '2 hours',
70 SITEMAP: '1 day',
71 SECURITYTXT: '2 hours',
72 NODEINFO: '10 minutes',
73 DNT_POLICY: '1 week',
74 OVERVIEWS: {
75 VIDEOS: '1 hour'
76 },
77 ACTIVITY_PUB: {
78 VIDEOS: '1 second' // 1 second, cache concurrent requests after a broadcast for example
79 },
80 STATS: '4 hours'
81 }
82
83 // ---------------------------------------------------------------------------
84
85 // Number of points we add/remove after a successful/bad request
86 const ACTOR_FOLLOW_SCORE = {
87 PENALTY: -10,
88 BONUS: 10,
89 BASE: 1000,
90 MAX: 10000
91 }
92
93 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
94 PENDING: 'pending',
95 ACCEPTED: 'accepted'
96 }
97
98 const REMOTE_SCHEME = {
99 HTTP: 'https',
100 WS: 'wss'
101 }
102
103 const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
104 'activitypub-http-broadcast': 5,
105 'activitypub-http-unicast': 5,
106 'activitypub-http-fetcher': 5,
107 'activitypub-follow': 5,
108 'video-file-import': 1,
109 'video-file': 1,
110 'video-import': 1,
111 'email': 5,
112 'videos-views': 1,
113 'activitypub-refresher': 1
114 }
115 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
116 'activitypub-http-broadcast': 1,
117 'activitypub-http-unicast': 5,
118 'activitypub-http-fetcher': 1,
119 'activitypub-follow': 3,
120 'video-file-import': 1,
121 'video-file': 1,
122 'video-import': 1,
123 'email': 5,
124 'videos-views': 1,
125 'activitypub-refresher': 1
126 }
127 const JOB_TTL: { [ id in JobType ]: number } = {
128 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
129 'activitypub-http-unicast': 60000 * 10, // 10 minutes
130 'activitypub-http-fetcher': 60000 * 10, // 10 minutes
131 'activitypub-follow': 60000 * 10, // 10 minutes
132 'video-file-import': 1000 * 3600, // 1 hour
133 'video-file': 1000 * 3600 * 48, // 2 days, transcoding could be long
134 'video-import': 1000 * 3600 * 2, // hours
135 'email': 60000 * 10, // 10 minutes
136 'videos-views': undefined, // Unlimited
137 'activitypub-refresher': 60000 * 10 // 10 minutes
138 }
139 const REPEAT_JOBS: { [ id: string ]: EveryRepeatOptions | CronRepeatOptions } = {
140 'videos-views': {
141 cron: '1 * * * *' // At 1 minute past the hour
142 }
143 }
144
145 const BROADCAST_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-http-broadcast job
146 const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
147 const JOB_REQUEST_TIMEOUT = 3000 // 3 seconds
148 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
149 const VIDEO_IMPORT_TIMEOUT = 1000 * 3600 // 1 hour
150
151 // 1 hour
152 let SCHEDULER_INTERVALS_MS = {
153 actorFollowScores: 60000 * 60, // 1 hour
154 removeOldJobs: 60000 * 60, // 1 hour
155 updateVideos: 60000, // 1 minute
156 youtubeDLUpdate: 60000 * 60 * 24 // 1 day
157 }
158
159 // ---------------------------------------------------------------------------
160
161 const CONFIG = {
162 CUSTOM_FILE: getLocalConfigFilePath(),
163 LISTEN: {
164 PORT: config.get<number>('listen.port'),
165 HOSTNAME: config.get<string>('listen.hostname')
166 },
167 DATABASE: {
168 DBNAME: 'peertube' + config.get<string>('database.suffix'),
169 HOSTNAME: config.get<string>('database.hostname'),
170 PORT: config.get<number>('database.port'),
171 USERNAME: config.get<string>('database.username'),
172 PASSWORD: config.get<string>('database.password'),
173 POOL: {
174 MAX: config.get<number>('database.pool.max')
175 }
176 },
177 REDIS: {
178 HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
179 PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
180 SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
181 AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
182 DB: config.has('redis.db') ? config.get<number>('redis.db') : null
183 },
184 SMTP: {
185 HOSTNAME: config.get<string>('smtp.hostname'),
186 PORT: config.get<number>('smtp.port'),
187 USERNAME: config.get<string>('smtp.username'),
188 PASSWORD: config.get<string>('smtp.password'),
189 TLS: config.get<boolean>('smtp.tls'),
190 DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
191 CA_FILE: config.get<string>('smtp.ca_file'),
192 FROM_ADDRESS: config.get<string>('smtp.from_address')
193 },
194 STORAGE: {
195 TMP_DIR: buildPath(config.get<string>('storage.tmp')),
196 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
197 LOG_DIR: buildPath(config.get<string>('storage.logs')),
198 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
199 PLAYLISTS_DIR: buildPath(config.get<string>('storage.playlists')),
200 REDUNDANCY_DIR: buildPath(config.get<string>('storage.redundancy')),
201 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
202 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
203 CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
204 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
205 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
206 },
207 WEBSERVER: {
208 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
209 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
210 HOSTNAME: config.get<string>('webserver.hostname'),
211 PORT: config.get<number>('webserver.port'),
212 URL: '',
213 HOST: ''
214 },
215 TRUST_PROXY: config.get<string[]>('trust_proxy'),
216 LOG: {
217 LEVEL: config.get<string>('log.level')
218 },
219 SEARCH: {
220 REMOTE_URI: {
221 USERS: config.get<boolean>('search.remote_uri.users'),
222 ANONYMOUS: config.get<boolean>('search.remote_uri.anonymous')
223 }
224 },
225 TRENDING: {
226 VIDEOS: {
227 INTERVAL_DAYS: config.get<number>('trending.videos.interval_days')
228 }
229 },
230 REDUNDANCY: {
231 VIDEOS: {
232 CHECK_INTERVAL: parseDuration(config.get<string>('redundancy.videos.check_interval')),
233 STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies'))
234 }
235 },
236 CSP: {
237 ENABLED: config.get<boolean>('csp.enabled'),
238 REPORT_ONLY: config.get<boolean>('csp.report_only'),
239 REPORT_URI: config.get<boolean>('csp.report_uri')
240 },
241 ADMIN: {
242 get EMAIL () { return config.get<string>('admin.email') }
243 },
244 CONTACT_FORM: {
245 get ENABLED () { return config.get<boolean>('contact_form.enabled') }
246 },
247 SIGNUP: {
248 get ENABLED () { return config.get<boolean>('signup.enabled') },
249 get LIMIT () { return config.get<number>('signup.limit') },
250 get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
251 FILTERS: {
252 CIDR: {
253 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
254 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
255 }
256 }
257 },
258 USER: {
259 get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
260 get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
261 },
262 TRANSCODING: {
263 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
264 get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') },
265 get THREADS () { return config.get<number>('transcoding.threads') },
266 RESOLUTIONS: {
267 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
268 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
269 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
270 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
271 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
272 },
273 HLS: {
274 get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') }
275 }
276 },
277 IMPORT: {
278 VIDEOS: {
279 HTTP: {
280 get ENABLED () { return config.get<boolean>('import.videos.http.enabled') }
281 },
282 TORRENT: {
283 get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
284 }
285 }
286 },
287 CACHE: {
288 PREVIEWS: {
289 get SIZE () { return config.get<number>('cache.previews.size') }
290 },
291 VIDEO_CAPTIONS: {
292 get SIZE () { return config.get<number>('cache.captions.size') }
293 }
294 },
295 INSTANCE: {
296 get NAME () { return config.get<string>('instance.name') },
297 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
298 get DESCRIPTION () { return config.get<string>('instance.description') },
299 get TERMS () { return config.get<string>('instance.terms') },
300 get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') },
301 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
302 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
303 CUSTOMIZATIONS: {
304 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
305 get CSS () { return config.get<string>('instance.customizations.css') }
306 },
307 get ROBOTS () { return config.get<string>('instance.robots') },
308 get SECURITYTXT () { return config.get<string>('instance.securitytxt') },
309 get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') }
310 },
311 SERVICES: {
312 TWITTER: {
313 get USERNAME () { return config.get<string>('services.twitter.username') },
314 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
315 }
316 }
317 }
318
319 // ---------------------------------------------------------------------------
320
321 let CONSTRAINTS_FIELDS = {
322 USERS: {
323 NAME: { min: 1, max: 120 }, // Length
324 DESCRIPTION: { min: 3, max: 1000 }, // Length
325 USERNAME: { min: 1, max: 50 }, // Length
326 PASSWORD: { min: 6, max: 255 }, // Length
327 VIDEO_QUOTA: { min: -1 },
328 VIDEO_QUOTA_DAILY: { min: -1 },
329 BLOCKED_REASON: { min: 3, max: 250 } // Length
330 },
331 VIDEO_ABUSES: {
332 REASON: { min: 2, max: 3000 }, // Length
333 MODERATION_COMMENT: { min: 2, max: 3000 } // Length
334 },
335 VIDEO_BLACKLIST: {
336 REASON: { min: 2, max: 300 } // Length
337 },
338 VIDEO_CHANNELS: {
339 NAME: { min: 1, max: 120 }, // Length
340 DESCRIPTION: { min: 3, max: 1000 }, // Length
341 SUPPORT: { min: 3, max: 1000 }, // Length
342 URL: { min: 3, max: 2000 } // Length
343 },
344 VIDEO_CAPTIONS: {
345 CAPTION_FILE: {
346 EXTNAME: [ '.vtt', '.srt' ],
347 FILE_SIZE: {
348 max: 2 * 1024 * 1024 // 2MB
349 }
350 }
351 },
352 VIDEO_IMPORTS: {
353 URL: { min: 3, max: 2000 }, // Length
354 TORRENT_NAME: { min: 3, max: 255 }, // Length
355 TORRENT_FILE: {
356 EXTNAME: [ '.torrent' ],
357 FILE_SIZE: {
358 max: 1024 * 200 // 200 KB
359 }
360 }
361 },
362 VIDEOS_REDUNDANCY: {
363 URL: { min: 3, max: 2000 } // Length
364 },
365 VIDEO_RATES: {
366 URL: { min: 3, max: 2000 } // Length
367 },
368 VIDEOS: {
369 NAME: { min: 3, max: 120 }, // Length
370 LANGUAGE: { min: 1, max: 10 }, // Length
371 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
372 DESCRIPTION: { min: 3, max: 10000 }, // Length
373 SUPPORT: { min: 3, max: 1000 }, // Length
374 IMAGE: {
375 EXTNAME: [ '.jpg', '.jpeg' ],
376 FILE_SIZE: {
377 max: 2 * 1024 * 1024 // 2MB
378 }
379 },
380 EXTNAME: buildVideosExtname(),
381 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
382 DURATION: { min: 0 }, // Number
383 TAGS: { min: 0, max: 5 }, // Number of total tags
384 TAG: { min: 2, max: 30 }, // Length
385 THUMBNAIL: { min: 2, max: 30 },
386 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
387 VIEWS: { min: 0 },
388 LIKES: { min: 0 },
389 DISLIKES: { min: 0 },
390 FILE_SIZE: { min: 10 },
391 URL: { min: 3, max: 2000 } // Length
392 },
393 VIDEO_PLAYLISTS: {
394 NAME: { min: 1, max: 120 }, // Length
395 DESCRIPTION: { min: 3, max: 1000 }, // Length
396 URL: { min: 3, max: 2000 }, // Length
397 IMAGE: {
398 EXTNAME: [ '.jpg', '.jpeg' ],
399 FILE_SIZE: {
400 max: 2 * 1024 * 1024 // 2MB
401 }
402 }
403 },
404 ACTORS: {
405 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
406 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
407 URL: { min: 3, max: 2000 }, // Length
408 AVATAR: {
409 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
410 FILE_SIZE: {
411 max: 2 * 1024 * 1024 // 2MB
412 }
413 }
414 },
415 VIDEO_EVENTS: {
416 COUNT: { min: 0 }
417 },
418 VIDEO_COMMENTS: {
419 TEXT: { min: 1, max: 3000 }, // Length
420 URL: { min: 3, max: 2000 } // Length
421 },
422 VIDEO_SHARE: {
423 URL: { min: 3, max: 2000 } // Length
424 },
425 CONTACT_FORM: {
426 FROM_NAME: { min: 1, max: 120 }, // Length
427 BODY: { min: 3, max: 5000 } // Length
428 }
429 }
430
431 const RATES_LIMIT = {
432 LOGIN: {
433 WINDOW_MS: 5 * 60 * 1000, // 5 minutes
434 MAX: 15 // 15 attempts
435 },
436 ASK_SEND_EMAIL: {
437 WINDOW_MS: 5 * 60 * 1000, // 5 minutes
438 MAX: 3 // 3 attempts
439 }
440 }
441
442 let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
443 let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour
444
445 const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
446 MIN: 10,
447 AVERAGE: 30,
448 MAX: 60,
449 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
450 }
451
452 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
453 LIKE: 'like',
454 DISLIKE: 'dislike'
455 }
456
457 const FFMPEG_NICE: { [ id: string ]: number } = {
458 THUMBNAIL: 2, // 2 just for don't blocking servers
459 TRANSCODING: 15
460 }
461
462 const VIDEO_CATEGORIES = {
463 1: 'Music',
464 2: 'Films',
465 3: 'Vehicles',
466 4: 'Art',
467 5: 'Sports',
468 6: 'Travels',
469 7: 'Gaming',
470 8: 'People',
471 9: 'Comedy',
472 10: 'Entertainment',
473 11: 'News & Politics',
474 12: 'How To',
475 13: 'Education',
476 14: 'Activism',
477 15: 'Science & Technology',
478 16: 'Animals',
479 17: 'Kids',
480 18: 'Food'
481 }
482
483 // See https://creativecommons.org/licenses/?lang=en
484 const VIDEO_LICENCES = {
485 1: 'Attribution',
486 2: 'Attribution - Share Alike',
487 3: 'Attribution - No Derivatives',
488 4: 'Attribution - Non Commercial',
489 5: 'Attribution - Non Commercial - Share Alike',
490 6: 'Attribution - Non Commercial - No Derivatives',
491 7: 'Public Domain Dedication'
492 }
493
494 const VIDEO_LANGUAGES = buildLanguages()
495
496 const VIDEO_PRIVACIES = {
497 [VideoPrivacy.PUBLIC]: 'Public',
498 [VideoPrivacy.UNLISTED]: 'Unlisted',
499 [VideoPrivacy.PRIVATE]: 'Private'
500 }
501
502 const VIDEO_STATES = {
503 [VideoState.PUBLISHED]: 'Published',
504 [VideoState.TO_TRANSCODE]: 'To transcode',
505 [VideoState.TO_IMPORT]: 'To import'
506 }
507
508 const VIDEO_IMPORT_STATES = {
509 [VideoImportState.FAILED]: 'Failed',
510 [VideoImportState.PENDING]: 'Pending',
511 [VideoImportState.SUCCESS]: 'Success'
512 }
513
514 const VIDEO_ABUSE_STATES = {
515 [VideoAbuseState.PENDING]: 'Pending',
516 [VideoAbuseState.REJECTED]: 'Rejected',
517 [VideoAbuseState.ACCEPTED]: 'Accepted'
518 }
519
520 const VIDEO_PLAYLIST_PRIVACIES = {
521 [VideoPlaylistPrivacy.PUBLIC]: 'Public',
522 [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted',
523 [VideoPlaylistPrivacy.PRIVATE]: 'Private'
524 }
525
526 const VIDEO_PLAYLIST_TYPES = {
527 [VideoPlaylistType.REGULAR]: 'Regular',
528 [VideoPlaylistType.WATCH_LATER]: 'Watch later'
529 }
530
531 const MIMETYPES = {
532 VIDEO: {
533 MIMETYPE_EXT: buildVideoMimetypeExt(),
534 EXT_MIMETYPE: null as { [ id: string ]: string }
535 },
536 IMAGE: {
537 MIMETYPE_EXT: {
538 'image/png': '.png',
539 'image/jpg': '.jpg',
540 'image/jpeg': '.jpg'
541 }
542 },
543 VIDEO_CAPTIONS: {
544 MIMETYPE_EXT: {
545 'text/vtt': '.vtt',
546 'application/x-subrip': '.srt'
547 }
548 },
549 TORRENT: {
550 MIMETYPE_EXT: {
551 'application/x-bittorrent': '.torrent'
552 }
553 }
554 }
555 MIMETYPES.VIDEO.EXT_MIMETYPE = invert(MIMETYPES.VIDEO.MIMETYPE_EXT)
556
557 // ---------------------------------------------------------------------------
558
559 const OVERVIEWS = {
560 VIDEOS: {
561 SAMPLE_THRESHOLD: 6,
562 SAMPLES_COUNT: 2
563 }
564 }
565
566 // ---------------------------------------------------------------------------
567
568 const SERVER_ACTOR_NAME = 'peertube'
569
570 const ACTIVITY_PUB = {
571 POTENTIAL_ACCEPT_HEADERS: [
572 'application/activity+json',
573 'application/ld+json',
574 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
575 ],
576 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
577 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
578 COLLECTION_ITEMS_PER_PAGE: 10,
579 FETCH_PAGE_LIMIT: 100,
580 URL_MIME_TYPES: {
581 VIDEO: Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT),
582 TORRENT: [ 'application/x-bittorrent' ],
583 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
584 },
585 MAX_RECURSION_COMMENTS: 100,
586 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000, // 1 day
587 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
588 }
589
590 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
591 GROUP: 'Group',
592 PERSON: 'Person',
593 APPLICATION: 'Application'
594 }
595
596 const HTTP_SIGNATURE = {
597 HEADER_NAME: 'signature',
598 ALGORITHM: 'rsa-sha256',
599 HEADERS_TO_SIGN: [ '(request-target)', 'host', 'date', 'digest' ]
600 }
601
602 // ---------------------------------------------------------------------------
603
604 let PRIVATE_RSA_KEY_SIZE = 2048
605
606 // Password encryption
607 const BCRYPT_SALT_SIZE = 10
608
609 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
610
611 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
612
613 const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = {
614 DO_NOT_LIST: 'do_not_list',
615 BLUR: 'blur',
616 DISPLAY: 'display'
617 }
618
619 // ---------------------------------------------------------------------------
620
621 // Express static paths (router)
622 const STATIC_PATHS = {
623 PREVIEWS: '/static/previews/',
624 THUMBNAILS: '/static/thumbnails/',
625 TORRENTS: '/static/torrents/',
626 WEBSEED: '/static/webseed/',
627 REDUNDANCY: '/static/redundancy/',
628 PLAYLISTS: {
629 HLS: '/static/playlists/hls'
630 },
631 AVATARS: '/static/avatars/',
632 VIDEO_CAPTIONS: '/static/video-captions/'
633 }
634 const STATIC_DOWNLOAD_PATHS = {
635 TORRENTS: '/download/torrents/',
636 VIDEOS: '/download/videos/'
637 }
638
639 // Cache control
640 let STATIC_MAX_AGE = '2h'
641
642 // Videos thumbnail size
643 const THUMBNAILS_SIZE = {
644 width: 223,
645 height: 112
646 }
647 const PREVIEWS_SIZE = {
648 width: 560,
649 height: 315
650 }
651 const AVATARS_SIZE = {
652 width: 120,
653 height: 120
654 }
655
656 const EMBED_SIZE = {
657 width: 560,
658 height: 315
659 }
660
661 // Sub folders of cache directory
662 const CACHE = {
663 PREVIEWS: {
664 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
665 MAX_AGE: 1000 * 3600 * 3 // 3 hours
666 },
667 VIDEO_CAPTIONS: {
668 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
669 MAX_AGE: 1000 * 3600 * 3 // 3 hours
670 }
671 }
672
673 const HLS_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.PLAYLISTS_DIR, 'hls')
674 const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls')
675
676 const MEMOIZE_TTL = {
677 OVERVIEWS_SAMPLE: 1000 * 3600 * 4 // 4 hours
678 }
679
680 const REDUNDANCY = {
681 VIDEOS: {
682 RANDOMIZED_FACTOR: 5
683 }
684 }
685
686 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
687
688 // ---------------------------------------------------------------------------
689
690 const CUSTOM_HTML_TAG_COMMENTS = {
691 TITLE: '<!-- title tag -->',
692 DESCRIPTION: '<!-- description tag -->',
693 CUSTOM_CSS: '<!-- custom css tag -->',
694 META_TAGS: '<!-- meta tags -->'
695 }
696
697 // ---------------------------------------------------------------------------
698
699 const FEEDS = {
700 COUNT: 20
701 }
702
703 // ---------------------------------------------------------------------------
704
705 const TRACKER_RATE_LIMITS = {
706 INTERVAL: 60000 * 5, // 5 minutes
707 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
708 ANNOUNCES_PER_IP: 30 // maximum announces for all our torrents in the interval
709 }
710
711 // ---------------------------------------------------------------------------
712
713 // Special constants for a test instance
714 if (isTestInstance() === true) {
715 PRIVATE_RSA_KEY_SIZE = 1024
716
717 ACTOR_FOLLOW_SCORE.BASE = 20
718
719 REMOTE_SCHEME.HTTP = 'http'
720 REMOTE_SCHEME.WS = 'ws'
721
722 STATIC_MAX_AGE = '0'
723
724 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
725 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
726 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
727
728 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
729
730 SCHEDULER_INTERVALS_MS.actorFollowScores = 1000
731 SCHEDULER_INTERVALS_MS.removeOldJobs = 10000
732 SCHEDULER_INTERVALS_MS.updateVideos = 5000
733 REPEAT_JOBS['videos-views'] = { every: 5000 }
734
735 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
736
737 VIDEO_VIEW_LIFETIME = 1000 // 1 second
738 CONTACT_FORM_LIFETIME = 1000 // 1 second
739
740 JOB_ATTEMPTS['email'] = 1
741
742 CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
743 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 1
744 ROUTE_CACHE_LIFETIME.OVERVIEWS.VIDEOS = '0ms'
745
746 RATES_LIMIT.LOGIN.MAX = 20
747 }
748
749 updateWebserverUrls()
750
751 // ---------------------------------------------------------------------------
752
753 export {
754 API_VERSION,
755 HLS_REDUNDANCY_DIRECTORY,
756 AVATARS_SIZE,
757 ACCEPT_HEADERS,
758 BCRYPT_SALT_SIZE,
759 TRACKER_RATE_LIMITS,
760 CACHE,
761 CONFIG,
762 CONSTRAINTS_FIELDS,
763 EMBED_SIZE,
764 REDUNDANCY,
765 JOB_CONCURRENCY,
766 JOB_ATTEMPTS,
767 LAST_MIGRATION_VERSION,
768 OAUTH_LIFETIME,
769 CUSTOM_HTML_TAG_COMMENTS,
770 BROADCAST_CONCURRENCY,
771 PAGINATION,
772 ACTOR_FOLLOW_SCORE,
773 PREVIEWS_SIZE,
774 REMOTE_SCHEME,
775 FOLLOW_STATES,
776 SERVER_ACTOR_NAME,
777 PRIVATE_RSA_KEY_SIZE,
778 ROUTE_CACHE_LIFETIME,
779 SORTABLE_COLUMNS,
780 HLS_PLAYLIST_DIRECTORY,
781 FEEDS,
782 JOB_TTL,
783 NSFW_POLICY_TYPES,
784 STATIC_MAX_AGE,
785 STATIC_PATHS,
786 VIDEO_IMPORT_TIMEOUT,
787 VIDEO_PLAYLIST_TYPES,
788 ACTIVITY_PUB,
789 ACTIVITY_PUB_ACTOR_TYPES,
790 THUMBNAILS_SIZE,
791 VIDEO_CATEGORIES,
792 VIDEO_LANGUAGES,
793 VIDEO_PRIVACIES,
794 VIDEO_LICENCES,
795 VIDEO_STATES,
796 VIDEO_RATE_TYPES,
797 VIDEO_TRANSCODING_FPS,
798 FFMPEG_NICE,
799 VIDEO_ABUSE_STATES,
800 JOB_REQUEST_TIMEOUT,
801 USER_PASSWORD_RESET_LIFETIME,
802 MEMOIZE_TTL,
803 USER_EMAIL_VERIFY_LIFETIME,
804 OVERVIEWS,
805 SCHEDULER_INTERVALS_MS,
806 REPEAT_JOBS,
807 STATIC_DOWNLOAD_PATHS,
808 RATES_LIMIT,
809 MIMETYPES,
810 CRAWL_REQUEST_CONCURRENCY,
811 JOB_COMPLETED_LIFETIME,
812 HTTP_SIGNATURE,
813 VIDEO_IMPORT_STATES,
814 VIDEO_VIEW_LIFETIME,
815 CONTACT_FORM_LIFETIME,
816 VIDEO_PLAYLIST_PRIVACIES,
817 buildLanguages
818 }
819
820 // ---------------------------------------------------------------------------
821
822 function getLocalConfigFilePath () {
823 const configSources = config.util.getConfigSources()
824 if (configSources.length === 0) throw new Error('Invalid config source.')
825
826 let filename = 'local'
827 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
828 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
829
830 return join(dirname(configSources[ 0 ].name), filename + '.json')
831 }
832
833 function buildVideoMimetypeExt () {
834 const data = {
835 'video/webm': '.webm',
836 'video/ogg': '.ogv',
837 'video/mp4': '.mp4'
838 }
839
840 if (CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
841 Object.assign(data, {
842 'video/quicktime': '.mov',
843 'video/x-msvideo': '.avi',
844 'video/x-flv': '.flv',
845 'video/x-matroska': '.mkv',
846 'application/octet-stream': '.mkv',
847 'video/avi': '.avi'
848 })
849 }
850
851 return data
852 }
853
854 function updateWebserverUrls () {
855 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
856 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
857 }
858
859 function updateWebserverConfig () {
860 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = buildVideosExtname()
861
862 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
863 MIMETYPES.VIDEO.EXT_MIMETYPE = invert(MIMETYPES.VIDEO.MIMETYPE_EXT)
864 }
865
866 function buildVideosExtname () {
867 return CONFIG.TRANSCODING.ENABLED && CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS
868 ? [ '.mp4', '.ogv', '.webm', '.mkv', '.mov', '.avi', '.flv' ]
869 : [ '.mp4', '.ogv', '.webm' ]
870 }
871
872 function buildVideosRedundancy (objs: any[]): VideosRedundancy[] {
873 if (!objs) return []
874
875 return objs.map(obj => {
876 return Object.assign({}, obj, {
877 minLifetime: parseDuration(obj.min_lifetime),
878 size: bytes.parse(obj.size),
879 minViews: obj.min_views
880 })
881 })
882 }
883
884 function buildLanguages () {
885 const iso639 = require('iso-639-3')
886
887 const languages: { [ id: string ]: string } = {}
888
889 const additionalLanguages = {
890 'sgn': true, // Sign languages (macro language)
891 'ase': true, // American sign language
892 'sdl': true, // Arabian sign language
893 'bfi': true, // British sign language
894 'bzs': true, // Brazilian sign language
895 'csl': true, // Chinese sign language
896 'cse': true, // Czech sign language
897 'dsl': true, // Danish sign language
898 'fsl': true, // French sign language
899 'gsg': true, // German sign language
900 'pks': true, // Pakistan sign language
901 'jsl': true, // Japanese sign language
902 'sfs': true, // South African sign language
903 'swl': true, // Swedish sign language
904 'rsl': true, // Russian sign language: true
905
906 'epo': true, // Esperanto
907 'tlh': true, // Klingon
908 'jbo': true, // Lojban
909 'avk': true // Kotava
910 }
911
912 // Only add ISO639-1 languages and some sign languages (ISO639-3)
913 iso639
914 .filter(l => {
915 return (l.iso6391 !== null && l.type === 'living') ||
916 additionalLanguages[l.iso6393] === true
917 })
918 .forEach(l => languages[l.iso6391 || l.iso6393] = l.name)
919
920 // Override Occitan label
921 languages['oc'] = 'Occitan'
922
923 return languages
924 }
925
926 export function reloadConfig () {
927
928 function directory () {
929 if (process.env.NODE_CONFIG_DIR) {
930 return process.env.NODE_CONFIG_DIR
931 }
932
933 return join(root(), 'config')
934 }
935
936 function purge () {
937 for (const fileName in require.cache) {
938 if (-1 === fileName.indexOf(directory())) {
939 continue
940 }
941
942 delete require.cache[fileName]
943 }
944
945 delete require.cache[require.resolve('config')]
946 }
947
948 purge()
949
950 config = require('config')
951
952 updateWebserverConfig()
953 updateWebserverUrls()
954 }