]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/constants.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / initializers / constants.ts
1 import { CronRepeatOptions, EveryRepeatOptions } from 'bull'
2 import { randomBytes } from 'crypto'
3 import { invert } from 'lodash'
4 import { join } from 'path'
5 import { randomInt } from '../../shared/core-utils/miscs/miscs'
6 import {
7 AbuseState,
8 JobType,
9 VideoImportState,
10 VideoPrivacy,
11 VideoRateType,
12 VideoResolution,
13 VideoState,
14 VideoTranscodingFPS
15 } from '../../shared/models'
16 import { ActivityPubActorType } from '../../shared/models/activitypub'
17 import { FollowState } from '../../shared/models/actors'
18 import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
19 import { VideoPlaylistPrivacy } from '../../shared/models/videos/playlist/video-playlist-privacy.model'
20 import { VideoPlaylistType } from '../../shared/models/videos/playlist/video-playlist-type.model'
21 // Do not use barrels, remain constants as independent as possible
22 import { isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
23 import { CONFIG, registerConfigChangedHandler } from './config'
24
25 // ---------------------------------------------------------------------------
26
27 const LAST_MIGRATION_VERSION = 650
28
29 // ---------------------------------------------------------------------------
30
31 const API_VERSION = 'v1'
32 const PEERTUBE_VERSION: string = require(join(root(), 'package.json')).version
33
34 const PAGINATION = {
35 GLOBAL: {
36 COUNT: {
37 DEFAULT: 15,
38 MAX: 100
39 }
40 },
41 OUTBOX: {
42 COUNT: {
43 MAX: 50
44 }
45 }
46 }
47
48 const WEBSERVER = {
49 URL: '',
50 HOST: '',
51 SCHEME: '',
52 WS: '',
53 HOSTNAME: '',
54 PORT: 0,
55 RTMP_URL: ''
56 }
57
58 // Sortable columns per schema
59 const SORTABLE_COLUMNS = {
60 USERS: [ 'id', 'username', 'videoQuotaUsed', 'createdAt', 'lastLoginDate', 'role' ],
61 USER_SUBSCRIPTIONS: [ 'id', 'createdAt' ],
62 ACCOUNTS: [ 'createdAt' ],
63 JOBS: [ 'createdAt' ],
64 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
65 VIDEO_IMPORTS: [ 'createdAt' ],
66
67 VIDEO_COMMENT_THREADS: [ 'createdAt', 'totalReplies' ],
68 VIDEO_COMMENTS: [ 'createdAt' ],
69
70 VIDEO_RATES: [ 'createdAt' ],
71 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
72 FOLLOWERS: [ 'createdAt', 'state', 'score' ],
73 FOLLOWING: [ 'createdAt', 'redundancyAllowed', 'state' ],
74
75 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending', 'hot', 'best' ],
76
77 // Don't forget to update peertube-search-index with the same values
78 VIDEOS_SEARCH: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'match' ],
79 VIDEO_CHANNELS_SEARCH: [ 'match', 'displayName', 'createdAt' ],
80
81 ABUSES: [ 'id', 'createdAt', 'state' ],
82
83 ACCOUNTS_BLOCKLIST: [ 'createdAt' ],
84 SERVERS_BLOCKLIST: [ 'createdAt' ],
85
86 USER_NOTIFICATIONS: [ 'createdAt', 'read' ],
87
88 VIDEO_PLAYLISTS: [ 'name', 'displayName', 'createdAt', 'updatedAt' ],
89
90 PLUGINS: [ 'name', 'createdAt', 'updatedAt' ],
91
92 AVAILABLE_PLUGINS: [ 'npmName', 'popularity' ],
93
94 VIDEO_REDUNDANCIES: [ 'name' ]
95 }
96
97 const OAUTH_LIFETIME = {
98 ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
99 REFRESH_TOKEN: 1209600 // 2 weeks
100 }
101
102 const ROUTE_CACHE_LIFETIME = {
103 FEEDS: '15 minutes',
104 ROBOTS: '2 hours',
105 SITEMAP: '1 day',
106 SECURITYTXT: '2 hours',
107 NODEINFO: '10 minutes',
108 DNT_POLICY: '1 week',
109 ACTIVITY_PUB: {
110 VIDEOS: '1 second' // 1 second, cache concurrent requests after a broadcast for example
111 },
112 STATS: '4 hours'
113 }
114
115 // ---------------------------------------------------------------------------
116
117 // Number of points we add/remove after a successful/bad request
118 const ACTOR_FOLLOW_SCORE = {
119 PENALTY: -10,
120 BONUS: 10,
121 BASE: 1000,
122 MAX: 10000
123 }
124
125 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
126 PENDING: 'pending',
127 ACCEPTED: 'accepted'
128 }
129
130 const REMOTE_SCHEME = {
131 HTTP: 'https',
132 WS: 'wss'
133 }
134
135 const JOB_ATTEMPTS: { [id in JobType]: number } = {
136 'activitypub-http-broadcast': 5,
137 'activitypub-http-unicast': 5,
138 'activitypub-http-fetcher': 5,
139 'activitypub-follow': 5,
140 'activitypub-cleaner': 1,
141 'video-file-import': 1,
142 'video-transcoding': 1,
143 'video-import': 1,
144 'email': 5,
145 'actor-keys': 3,
146 'videos-views': 1,
147 'activitypub-refresher': 1,
148 'video-redundancy': 1,
149 'video-live-ending': 1
150 }
151 // Excluded keys are jobs that can be configured by admins
152 const JOB_CONCURRENCY: { [id in Exclude<JobType, 'video-transcoding' | 'video-import'>]: number } = {
153 'activitypub-http-broadcast': 1,
154 'activitypub-http-unicast': 5,
155 'activitypub-http-fetcher': 1,
156 'activitypub-cleaner': 1,
157 'activitypub-follow': 1,
158 'video-file-import': 1,
159 'email': 5,
160 'actor-keys': 1,
161 'videos-views': 1,
162 'activitypub-refresher': 1,
163 'video-redundancy': 1,
164 'video-live-ending': 10
165 }
166 const JOB_TTL: { [id in JobType]: number } = {
167 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
168 'activitypub-http-unicast': 60000 * 10, // 10 minutes
169 'activitypub-http-fetcher': 1000 * 3600 * 10, // 10 hours
170 'activitypub-follow': 60000 * 10, // 10 minutes
171 'activitypub-cleaner': 1000 * 3600, // 1 hour
172 'video-file-import': 1000 * 3600, // 1 hour
173 'video-transcoding': 1000 * 3600 * 48, // 2 days, transcoding could be long
174 'video-import': 1000 * 3600 * 2, // 2 hours
175 'email': 60000 * 10, // 10 minutes
176 'actor-keys': 60000 * 20, // 20 minutes
177 'videos-views': undefined, // Unlimited
178 'activitypub-refresher': 60000 * 10, // 10 minutes
179 'video-redundancy': 1000 * 3600 * 3, // 3 hours
180 'video-live-ending': 1000 * 60 * 10 // 10 minutes
181 }
182 const REPEAT_JOBS: { [ id: string ]: EveryRepeatOptions | CronRepeatOptions } = {
183 'videos-views': {
184 cron: randomInt(1, 20) + ' * * * *' // Between 1-20 minutes past the hour
185 },
186 'activitypub-cleaner': {
187 cron: '30 5 * * ' + randomInt(0, 7) // 1 time per week (random day) at 5:30 AM
188 }
189 }
190 const JOB_PRIORITY = {
191 TRANSCODING: 100
192 }
193
194 const BROADCAST_CONCURRENCY = 30 // How many requests in parallel we do in activitypub-http-broadcast job
195 const AP_CLEANER_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-cleaner job
196 const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
197 const REQUEST_TIMEOUT = 7000 // 7 seconds
198 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
199 const VIDEO_IMPORT_TIMEOUT = 1000 * 3600 // 1 hour
200
201 const SCHEDULER_INTERVALS_MS = {
202 actorFollowScores: 60000 * 60, // 1 hour
203 removeOldJobs: 60000 * 60, // 1 hour
204 updateVideos: 60000, // 1 minute
205 youtubeDLUpdate: 60000 * 60 * 24, // 1 day
206 checkPlugins: CONFIG.PLUGINS.INDEX.CHECK_LATEST_VERSIONS_INTERVAL,
207 checkPeerTubeVersion: 60000 * 60 * 24, // 1 day
208 autoFollowIndexInstances: 60000 * 60 * 24, // 1 day
209 removeOldViews: 60000 * 60 * 24, // 1 day
210 removeOldHistory: 60000 * 60 * 24, // 1 day
211 updateInboxStats: 1000 * 60, // 1 minute
212 removeDanglingResumableUploads: 60000 * 60 * 16 // 16 hours
213 }
214
215 // ---------------------------------------------------------------------------
216
217 const CONSTRAINTS_FIELDS = {
218 USERS: {
219 NAME: { min: 1, max: 120 }, // Length
220 DESCRIPTION: { min: 3, max: 1000 }, // Length
221 USERNAME: { min: 1, max: 50 }, // Length
222 PASSWORD: { min: 6, max: 255 }, // Length
223 VIDEO_QUOTA: { min: -1 },
224 VIDEO_QUOTA_DAILY: { min: -1 },
225 VIDEO_LANGUAGES: { max: 500 }, // Array length
226 BLOCKED_REASON: { min: 3, max: 250 } // Length
227 },
228 ABUSES: {
229 REASON: { min: 2, max: 3000 }, // Length
230 MODERATION_COMMENT: { min: 2, max: 3000 } // Length
231 },
232 ABUSE_MESSAGES: {
233 MESSAGE: { min: 2, max: 3000 } // Length
234 },
235 VIDEO_BLACKLIST: {
236 REASON: { min: 2, max: 300 } // Length
237 },
238 VIDEO_CHANNELS: {
239 NAME: { min: 1, max: 120 }, // Length
240 DESCRIPTION: { min: 3, max: 1000 }, // Length
241 SUPPORT: { min: 3, max: 1000 }, // Length
242 URL: { min: 3, max: 2000 } // Length
243 },
244 VIDEO_CAPTIONS: {
245 CAPTION_FILE: {
246 EXTNAME: [ '.vtt', '.srt' ],
247 FILE_SIZE: {
248 max: 2 * 1024 * 1024 // 2MB
249 }
250 }
251 },
252 VIDEO_IMPORTS: {
253 URL: { min: 3, max: 2000 }, // Length
254 TORRENT_NAME: { min: 3, max: 255 }, // Length
255 TORRENT_FILE: {
256 EXTNAME: [ '.torrent' ],
257 FILE_SIZE: {
258 max: 1024 * 200 // 200 KB
259 }
260 }
261 },
262 VIDEOS_REDUNDANCY: {
263 URL: { min: 3, max: 2000 } // Length
264 },
265 VIDEO_RATES: {
266 URL: { min: 3, max: 2000 } // Length
267 },
268 VIDEOS: {
269 NAME: { min: 3, max: 120 }, // Length
270 LANGUAGE: { min: 1, max: 10 }, // Length
271 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
272 DESCRIPTION: { min: 3, max: 10000 }, // Length
273 SUPPORT: { min: 3, max: 1000 }, // Length
274 IMAGE: {
275 EXTNAME: [ '.png', '.jpg', '.jpeg', '.webp' ],
276 FILE_SIZE: {
277 max: 2 * 1024 * 1024 // 2MB
278 }
279 },
280 EXTNAME: [] as string[],
281 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
282 DURATION: { min: 0 }, // Number
283 TAGS: { min: 0, max: 5 }, // Number of total tags
284 TAG: { min: 2, max: 30 }, // Length
285 VIEWS: { min: 0 },
286 LIKES: { min: 0 },
287 DISLIKES: { min: 0 },
288 FILE_SIZE: { min: -1 },
289 PARTIAL_UPLOAD_SIZE: { max: 50 * 1024 * 1024 * 1024 }, // 50GB
290 URL: { min: 3, max: 2000 } // Length
291 },
292 VIDEO_PLAYLISTS: {
293 NAME: { min: 1, max: 120 }, // Length
294 DESCRIPTION: { min: 3, max: 1000 }, // Length
295 URL: { min: 3, max: 2000 }, // Length
296 IMAGE: {
297 EXTNAME: [ '.jpg', '.jpeg' ],
298 FILE_SIZE: {
299 max: 2 * 1024 * 1024 // 2MB
300 }
301 }
302 },
303 ACTORS: {
304 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
305 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
306 URL: { min: 3, max: 2000 }, // Length
307 IMAGE: {
308 EXTNAME: [ '.png', '.jpeg', '.jpg', '.gif', '.webp' ],
309 FILE_SIZE: {
310 max: 2 * 1024 * 1024 // 2MB
311 }
312 }
313 },
314 VIDEO_EVENTS: {
315 COUNT: { min: 0 }
316 },
317 VIDEO_COMMENTS: {
318 TEXT: { min: 1, max: 10000 }, // Length
319 URL: { min: 3, max: 2000 } // Length
320 },
321 VIDEO_SHARE: {
322 URL: { min: 3, max: 2000 } // Length
323 },
324 CONTACT_FORM: {
325 FROM_NAME: { min: 1, max: 120 }, // Length
326 BODY: { min: 3, max: 5000 } // Length
327 },
328 PLUGINS: {
329 NAME: { min: 1, max: 214 }, // Length
330 DESCRIPTION: { min: 1, max: 20000 } // Length
331 },
332 COMMONS: {
333 URL: { min: 5, max: 2000 } // Length
334 }
335 }
336
337 const VIEW_LIFETIME = {
338 VIDEO: 60000 * 60, // 1 hour
339 LIVE: 60000 * 5 // 5 minutes
340 }
341
342 let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour
343
344 const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
345 MIN: 10,
346 STANDARD: [ 24, 25, 30 ],
347 HD_STANDARD: [ 50, 60 ],
348 AVERAGE: 30,
349 MAX: 60,
350 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
351 }
352
353 const DEFAULT_AUDIO_RESOLUTION = VideoResolution.H_480P
354
355 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
356 LIKE: 'like',
357 DISLIKE: 'dislike'
358 }
359
360 const FFMPEG_NICE: { [ id: string ]: number } = {
361 // parent process defaults to niceness = 0
362 // reminder: lower = higher priority, max value is 19, lowest is -20
363 LIVE: 5, // prioritize over VOD and THUMBNAIL
364 THUMBNAIL: 10,
365 VOD: 15
366 }
367
368 const VIDEO_CATEGORIES = {
369 1: 'Music',
370 2: 'Films',
371 3: 'Vehicles',
372 4: 'Art',
373 5: 'Sports',
374 6: 'Travels',
375 7: 'Gaming',
376 8: 'People',
377 9: 'Comedy',
378 10: 'Entertainment',
379 11: 'News & Politics',
380 12: 'How To',
381 13: 'Education',
382 14: 'Activism',
383 15: 'Science & Technology',
384 16: 'Animals',
385 17: 'Kids',
386 18: 'Food'
387 }
388
389 // See https://creativecommons.org/licenses/?lang=en
390 const VIDEO_LICENCES = {
391 1: 'Attribution',
392 2: 'Attribution - Share Alike',
393 3: 'Attribution - No Derivatives',
394 4: 'Attribution - Non Commercial',
395 5: 'Attribution - Non Commercial - Share Alike',
396 6: 'Attribution - Non Commercial - No Derivatives',
397 7: 'Public Domain Dedication'
398 }
399
400 const VIDEO_LANGUAGES: { [id: string]: string } = {}
401
402 const VIDEO_PRIVACIES: { [ id in VideoPrivacy ]: string } = {
403 [VideoPrivacy.PUBLIC]: 'Public',
404 [VideoPrivacy.UNLISTED]: 'Unlisted',
405 [VideoPrivacy.PRIVATE]: 'Private',
406 [VideoPrivacy.INTERNAL]: 'Internal'
407 }
408
409 const VIDEO_STATES: { [ id in VideoState ]: string } = {
410 [VideoState.PUBLISHED]: 'Published',
411 [VideoState.TO_TRANSCODE]: 'To transcode',
412 [VideoState.TO_IMPORT]: 'To import',
413 [VideoState.WAITING_FOR_LIVE]: 'Waiting for livestream',
414 [VideoState.LIVE_ENDED]: 'Livestream ended'
415 }
416
417 const VIDEO_IMPORT_STATES: { [ id in VideoImportState ]: string } = {
418 [VideoImportState.FAILED]: 'Failed',
419 [VideoImportState.PENDING]: 'Pending',
420 [VideoImportState.SUCCESS]: 'Success',
421 [VideoImportState.REJECTED]: 'Rejected'
422 }
423
424 const ABUSE_STATES: { [ id in AbuseState ]: string } = {
425 [AbuseState.PENDING]: 'Pending',
426 [AbuseState.REJECTED]: 'Rejected',
427 [AbuseState.ACCEPTED]: 'Accepted'
428 }
429
430 const VIDEO_PLAYLIST_PRIVACIES: { [ id in VideoPlaylistPrivacy ]: string } = {
431 [VideoPlaylistPrivacy.PUBLIC]: 'Public',
432 [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted',
433 [VideoPlaylistPrivacy.PRIVATE]: 'Private'
434 }
435
436 const VIDEO_PLAYLIST_TYPES: { [ id in VideoPlaylistType ]: string } = {
437 [VideoPlaylistType.REGULAR]: 'Regular',
438 [VideoPlaylistType.WATCH_LATER]: 'Watch later'
439 }
440
441 const MIMETYPES = {
442 AUDIO: {
443 MIMETYPE_EXT: {
444 'audio/mpeg': '.mp3',
445 'audio/mp3': '.mp3',
446 'application/ogg': '.ogg',
447 'audio/ogg': '.ogg',
448 'audio/x-ms-wma': '.wma',
449 'audio/wav': '.wav',
450 'audio/x-flac': '.flac',
451 'audio/flac': '.flac',
452 '‎audio/aac': '.aac',
453 'audio/m4a': '.m4a',
454 'audio/mp4': '.m4a',
455 'audio/x-m4a': '.m4a',
456 'audio/ac3': '.ac3'
457 },
458 EXT_MIMETYPE: null as { [ id: string ]: string }
459 },
460 VIDEO: {
461 MIMETYPE_EXT: null as { [ id: string ]: string | string[] },
462 MIMETYPES_REGEX: null as string,
463 EXT_MIMETYPE: null as { [ id: string ]: string }
464 },
465 IMAGE: {
466 MIMETYPE_EXT: {
467 'image/png': '.png',
468 'image/gif': '.gif',
469 'image/webp': '.webp',
470 'image/jpg': '.jpg',
471 'image/jpeg': '.jpg'
472 },
473 EXT_MIMETYPE: null as { [ id: string ]: string }
474 },
475 VIDEO_CAPTIONS: {
476 MIMETYPE_EXT: {
477 'text/vtt': '.vtt',
478 'application/x-subrip': '.srt',
479 'text/plain': '.srt'
480 }
481 },
482 TORRENT: {
483 MIMETYPE_EXT: {
484 'application/x-bittorrent': '.torrent'
485 }
486 }
487 }
488 MIMETYPES.AUDIO.EXT_MIMETYPE = invert(MIMETYPES.AUDIO.MIMETYPE_EXT)
489 MIMETYPES.IMAGE.EXT_MIMETYPE = invert(MIMETYPES.IMAGE.MIMETYPE_EXT)
490
491 // ---------------------------------------------------------------------------
492
493 const OVERVIEWS = {
494 VIDEOS: {
495 SAMPLE_THRESHOLD: 6,
496 SAMPLES_COUNT: 20
497 }
498 }
499
500 const VIDEO_CHANNELS = {
501 MAX_PER_USER: 20
502 }
503
504 // ---------------------------------------------------------------------------
505
506 const SERVER_ACTOR_NAME = 'peertube'
507
508 const ACTIVITY_PUB = {
509 POTENTIAL_ACCEPT_HEADERS: [
510 'application/activity+json',
511 'application/ld+json',
512 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
513 ],
514 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
515 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
516 COLLECTION_ITEMS_PER_PAGE: 10,
517 FETCH_PAGE_LIMIT: 2000,
518 URL_MIME_TYPES: {
519 VIDEO: [] as string[],
520 TORRENT: [ 'application/x-bittorrent' ],
521 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
522 },
523 MAX_RECURSION_COMMENTS: 100,
524 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
525 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
526 VIDEO_PLAYLIST_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2 // 2 days
527 }
528
529 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
530 GROUP: 'Group',
531 PERSON: 'Person',
532 APPLICATION: 'Application',
533 ORGANIZATION: 'Organization',
534 SERVICE: 'Service'
535 }
536
537 const HTTP_SIGNATURE = {
538 HEADER_NAME: 'signature',
539 ALGORITHM: 'rsa-sha256',
540 HEADERS_TO_SIGN: [ '(request-target)', 'host', 'date', 'digest' ],
541 REQUIRED_HEADERS: {
542 ALL: [ '(request-target)', 'host', 'date' ],
543 POST: [ '(request-target)', 'host', 'date', 'digest' ]
544 },
545 CLOCK_SKEW_SECONDS: 1800
546 }
547
548 // ---------------------------------------------------------------------------
549
550 let PRIVATE_RSA_KEY_SIZE = 2048
551
552 // Password encryption
553 const BCRYPT_SALT_SIZE = 10
554
555 const USER_PASSWORD_RESET_LIFETIME = 60000 * 60 // 60 minutes
556 const USER_PASSWORD_CREATE_LIFETIME = 60000 * 60 * 24 * 7 // 7 days
557
558 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
559
560 const NSFW_POLICY_TYPES: { [ id: string ]: NSFWPolicyType } = {
561 DO_NOT_LIST: 'do_not_list',
562 BLUR: 'blur',
563 DISPLAY: 'display'
564 }
565
566 // ---------------------------------------------------------------------------
567
568 // Express static paths (router)
569 const STATIC_PATHS = {
570 THUMBNAILS: '/static/thumbnails/',
571 TORRENTS: '/static/torrents/',
572 WEBSEED: '/static/webseed/',
573 REDUNDANCY: '/static/redundancy/',
574 STREAMING_PLAYLISTS: {
575 HLS: '/static/streaming-playlists/hls'
576 }
577 }
578 const STATIC_DOWNLOAD_PATHS = {
579 TORRENTS: '/download/torrents/',
580 VIDEOS: '/download/videos/',
581 HLS_VIDEOS: '/download/streaming-playlists/hls/videos/'
582 }
583 const LAZY_STATIC_PATHS = {
584 BANNERS: '/lazy-static/banners/',
585 AVATARS: '/lazy-static/avatars/',
586 PREVIEWS: '/lazy-static/previews/',
587 VIDEO_CAPTIONS: '/lazy-static/video-captions/',
588 TORRENTS: '/lazy-static/torrents/'
589 }
590
591 // Cache control
592 const STATIC_MAX_AGE = {
593 SERVER: '2h',
594 LAZY_SERVER: '2d',
595 CLIENT: '30d'
596 }
597
598 // Videos thumbnail size
599 const THUMBNAILS_SIZE = {
600 width: 280,
601 height: 157,
602 minWidth: 150
603 }
604 const PREVIEWS_SIZE = {
605 width: 850,
606 height: 480,
607 minWidth: 400
608 }
609 const ACTOR_IMAGES_SIZE = {
610 AVATARS: {
611 width: 120,
612 height: 120
613 },
614 BANNERS: {
615 width: 1920,
616 height: 317 // 6/1 ratio
617 }
618 }
619
620 const EMBED_SIZE = {
621 width: 560,
622 height: 315
623 }
624
625 // Sub folders of cache directory
626 const FILES_CACHE = {
627 PREVIEWS: {
628 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
629 MAX_AGE: 1000 * 3600 * 3 // 3 hours
630 },
631 VIDEO_CAPTIONS: {
632 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
633 MAX_AGE: 1000 * 3600 * 3 // 3 hours
634 },
635 TORRENTS: {
636 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'torrents'),
637 MAX_AGE: 1000 * 3600 * 3 // 3 hours
638 }
639 }
640
641 const LRU_CACHE = {
642 USER_TOKENS: {
643 MAX_SIZE: 1000
644 },
645 ACTOR_IMAGE_STATIC: {
646 MAX_SIZE: 500
647 }
648 }
649
650 const RESUMABLE_UPLOAD_DIRECTORY = join(CONFIG.STORAGE.TMP_DIR, 'resumable-uploads')
651 const HLS_STREAMING_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.STREAMING_PLAYLISTS_DIR, 'hls')
652 const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls')
653
654 const VIDEO_LIVE = {
655 EXTENSION: '.ts',
656 CLEANUP_DELAY: 1000 * 60 * 5, // 5 minutes
657 SEGMENT_TIME_SECONDS: 4, // 4 seconds
658 SEGMENTS_LIST_SIZE: 15, // 15 maximum segments in live playlist
659 REPLAY_DIRECTORY: 'replay',
660 EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION: 4,
661 MAX_SOCKET_WAITING_DATA: 1024 * 1000 * 100, // 100MB
662 RTMP: {
663 CHUNK_SIZE: 60000,
664 GOP_CACHE: true,
665 PING: 60,
666 PING_TIMEOUT: 30,
667 BASE_PATH: 'live'
668 }
669 }
670
671 const MEMOIZE_TTL = {
672 OVERVIEWS_SAMPLE: 1000 * 3600 * 4, // 4 hours
673 INFO_HASH_EXISTS: 1000 * 3600 * 12, // 12 hours
674 LIVE_ABLE_TO_UPLOAD: 1000 * 60, // 1 minute
675 LIVE_CHECK_SOCKET_HEALTH: 1000 * 60 // 1 minute
676 }
677
678 const MEMOIZE_LENGTH = {
679 INFO_HASH_EXISTS: 200
680 }
681
682 const QUEUE_CONCURRENCY = {
683 ACTOR_PROCESS_IMAGE: 3
684 }
685
686 const REDUNDANCY = {
687 VIDEOS: {
688 RANDOMIZED_FACTOR: 5
689 }
690 }
691
692 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
693
694 const ASSETS_PATH = {
695 DEFAULT_AUDIO_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-audio-background.jpg'),
696 DEFAULT_LIVE_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-live-background.jpg')
697 }
698
699 // ---------------------------------------------------------------------------
700
701 const CUSTOM_HTML_TAG_COMMENTS = {
702 TITLE: '<!-- title tag -->',
703 DESCRIPTION: '<!-- description tag -->',
704 CUSTOM_CSS: '<!-- custom css tag -->',
705 META_TAGS: '<!-- meta tags -->',
706 SERVER_CONFIG: '<!-- server config -->'
707 }
708
709 // ---------------------------------------------------------------------------
710
711 const FEEDS = {
712 COUNT: 20
713 }
714
715 const MAX_LOGS_OUTPUT_CHARACTERS = 10 * 1000 * 1000
716 const LOG_FILENAME = 'peertube.log'
717 const AUDIT_LOG_FILENAME = 'peertube-audit.log'
718
719 // ---------------------------------------------------------------------------
720
721 const TRACKER_RATE_LIMITS = {
722 INTERVAL: 60000 * 5, // 5 minutes
723 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
724 ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
725 BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
726 }
727
728 const P2P_MEDIA_LOADER_PEER_VERSION = 2
729
730 // ---------------------------------------------------------------------------
731
732 const PLUGIN_GLOBAL_CSS_FILE_NAME = 'plugins-global.css'
733 const PLUGIN_GLOBAL_CSS_PATH = join(CONFIG.STORAGE.TMP_DIR, PLUGIN_GLOBAL_CSS_FILE_NAME)
734
735 let PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 1000 * 60 * 5 // 5 minutes
736
737 const DEFAULT_THEME_NAME = 'default'
738 const DEFAULT_USER_THEME_NAME = 'instance-default'
739
740 // ---------------------------------------------------------------------------
741
742 const SEARCH_INDEX = {
743 ROUTES: {
744 VIDEOS: '/api/v1/search/videos',
745 VIDEO_CHANNELS: '/api/v1/search/video-channels'
746 }
747 }
748
749 // ---------------------------------------------------------------------------
750
751 // Special constants for a test instance
752 if (isTestInstance() === true) {
753 PRIVATE_RSA_KEY_SIZE = 1024
754
755 ACTOR_FOLLOW_SCORE.BASE = 20
756
757 REMOTE_SCHEME.HTTP = 'http'
758 REMOTE_SCHEME.WS = 'ws'
759
760 STATIC_MAX_AGE.SERVER = '0'
761
762 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
763 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
764 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
765 ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
766
767 CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max = 100 * 1024 // 100KB
768 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max = 400 * 1024 // 400KB
769
770 SCHEDULER_INTERVALS_MS.actorFollowScores = 1000
771 SCHEDULER_INTERVALS_MS.removeOldJobs = 10000
772 SCHEDULER_INTERVALS_MS.removeOldHistory = 5000
773 SCHEDULER_INTERVALS_MS.removeOldViews = 5000
774 SCHEDULER_INTERVALS_MS.updateVideos = 5000
775 SCHEDULER_INTERVALS_MS.autoFollowIndexInstances = 5000
776 SCHEDULER_INTERVALS_MS.updateInboxStats = 5000
777 SCHEDULER_INTERVALS_MS.checkPeerTubeVersion = 2000
778 REPEAT_JOBS['videos-views'] = { every: 5000 }
779 REPEAT_JOBS['activitypub-cleaner'] = { every: 5000 }
780
781 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
782
783 VIEW_LIFETIME.VIDEO = 1000 // 1 second
784 VIEW_LIFETIME.LIVE = 1000 * 5 // 5 second
785 CONTACT_FORM_LIFETIME = 1000 // 1 second
786
787 JOB_ATTEMPTS['email'] = 1
788
789 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
790 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 3000
791 MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD = 3000
792 OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD = 2
793
794 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 5000
795
796 VIDEO_LIVE.CLEANUP_DELAY = 5000
797 VIDEO_LIVE.SEGMENT_TIME_SECONDS = 2
798 VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION = 1
799 }
800
801 updateWebserverUrls()
802 updateWebserverConfig()
803
804 registerConfigChangedHandler(() => {
805 updateWebserverUrls()
806 updateWebserverConfig()
807 })
808
809 // ---------------------------------------------------------------------------
810
811 const FILES_CONTENT_HASH = {
812 MANIFEST: generateContentHash(),
813 FAVICON: generateContentHash(),
814 LOGO: generateContentHash()
815 }
816
817 // ---------------------------------------------------------------------------
818
819 export {
820 WEBSERVER,
821 API_VERSION,
822 VIDEO_LIVE,
823 PEERTUBE_VERSION,
824 LAZY_STATIC_PATHS,
825 SEARCH_INDEX,
826 RESUMABLE_UPLOAD_DIRECTORY,
827 HLS_REDUNDANCY_DIRECTORY,
828 P2P_MEDIA_LOADER_PEER_VERSION,
829 ACTOR_IMAGES_SIZE,
830 ACCEPT_HEADERS,
831 BCRYPT_SALT_SIZE,
832 TRACKER_RATE_LIMITS,
833 FILES_CACHE,
834 LOG_FILENAME,
835 CONSTRAINTS_FIELDS,
836 EMBED_SIZE,
837 REDUNDANCY,
838 JOB_CONCURRENCY,
839 JOB_ATTEMPTS,
840 AP_CLEANER_CONCURRENCY,
841 LAST_MIGRATION_VERSION,
842 OAUTH_LIFETIME,
843 CUSTOM_HTML_TAG_COMMENTS,
844 BROADCAST_CONCURRENCY,
845 AUDIT_LOG_FILENAME,
846 PAGINATION,
847 ACTOR_FOLLOW_SCORE,
848 PREVIEWS_SIZE,
849 REMOTE_SCHEME,
850 FOLLOW_STATES,
851 DEFAULT_USER_THEME_NAME,
852 SERVER_ACTOR_NAME,
853 PLUGIN_GLOBAL_CSS_FILE_NAME,
854 PLUGIN_GLOBAL_CSS_PATH,
855 PRIVATE_RSA_KEY_SIZE,
856 ROUTE_CACHE_LIFETIME,
857 SORTABLE_COLUMNS,
858 HLS_STREAMING_PLAYLIST_DIRECTORY,
859 FEEDS,
860 JOB_TTL,
861 DEFAULT_THEME_NAME,
862 NSFW_POLICY_TYPES,
863 STATIC_MAX_AGE,
864 STATIC_PATHS,
865 VIDEO_IMPORT_TIMEOUT,
866 VIDEO_PLAYLIST_TYPES,
867 MAX_LOGS_OUTPUT_CHARACTERS,
868 ACTIVITY_PUB,
869 ACTIVITY_PUB_ACTOR_TYPES,
870 THUMBNAILS_SIZE,
871 VIDEO_CATEGORIES,
872 MEMOIZE_LENGTH,
873 VIDEO_LANGUAGES,
874 VIDEO_PRIVACIES,
875 VIDEO_LICENCES,
876 VIDEO_STATES,
877 QUEUE_CONCURRENCY,
878 VIDEO_RATE_TYPES,
879 JOB_PRIORITY,
880 VIDEO_TRANSCODING_FPS,
881 FFMPEG_NICE,
882 ABUSE_STATES,
883 VIDEO_CHANNELS,
884 LRU_CACHE,
885 REQUEST_TIMEOUT,
886 USER_PASSWORD_RESET_LIFETIME,
887 USER_PASSWORD_CREATE_LIFETIME,
888 MEMOIZE_TTL,
889 USER_EMAIL_VERIFY_LIFETIME,
890 OVERVIEWS,
891 SCHEDULER_INTERVALS_MS,
892 REPEAT_JOBS,
893 STATIC_DOWNLOAD_PATHS,
894 MIMETYPES,
895 CRAWL_REQUEST_CONCURRENCY,
896 DEFAULT_AUDIO_RESOLUTION,
897 JOB_COMPLETED_LIFETIME,
898 HTTP_SIGNATURE,
899 VIDEO_IMPORT_STATES,
900 VIEW_LIFETIME,
901 CONTACT_FORM_LIFETIME,
902 VIDEO_PLAYLIST_PRIVACIES,
903 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
904 ASSETS_PATH,
905 FILES_CONTENT_HASH,
906 loadLanguages,
907 buildLanguages,
908 generateContentHash
909 }
910
911 // ---------------------------------------------------------------------------
912
913 function buildVideoMimetypeExt () {
914 const data = {
915 // streamable formats that warrant cross-browser compatibility
916 'video/webm': '.webm',
917 // We'll add .ogg if additional extensions are enabled
918 // We could add .ogg here but since it could be an audio file,
919 // it would be confusing for users because PeerTube will refuse their file (based on the mimetype)
920 'video/ogg': [ '.ogv' ],
921 'video/mp4': '.mp4'
922 }
923
924 if (CONFIG.TRANSCODING.ENABLED) {
925 if (CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
926 data['video/ogg'].push('.ogg')
927
928 Object.assign(data, {
929 'video/x-matroska': '.mkv',
930
931 // Developed by Apple
932 'video/quicktime': [ '.mov', '.qt', '.mqv' ], // often used as output format by editing software
933 'video/x-m4v': '.m4v',
934 'video/m4v': '.m4v',
935
936 // Developed by the Adobe Flash Platform
937 'video/x-flv': '.flv',
938 'video/x-f4v': '.f4v', // replacement for flv
939
940 // Developed by Microsoft
941 'video/x-ms-wmv': '.wmv',
942 'video/x-msvideo': '.avi',
943 'video/avi': '.avi',
944
945 // Developed by 3GPP
946 // common video formats for cell phones
947 'video/3gpp': [ '.3gp', '.3gpp' ],
948 'video/3gpp2': [ '.3g2', '.3gpp2' ],
949
950 // Developed by FFmpeg/Mplayer
951 'application/x-nut': '.nut',
952
953 // The standard video format used by many Sony and Panasonic HD camcorders.
954 // It is also used for storing high definition video on Blu-ray discs.
955 'video/mp2t': '.mts',
956 'video/m2ts': '.m2ts',
957
958 // Old formats reliant on MPEG-1/MPEG-2
959 'video/mpv': '.mpv',
960 'video/mpeg2': '.m2v',
961 'video/mpeg': [ '.m1v', '.mpg', '.mpe', '.mpeg', '.vob' ],
962 'video/dvd': '.vob',
963
964 // Could be anything
965 'application/octet-stream': null,
966 'application/mxf': '.mxf' // often used as exchange format by editing software
967 })
968 }
969
970 if (CONFIG.TRANSCODING.ALLOW_AUDIO_FILES) {
971 Object.assign(data, MIMETYPES.AUDIO.MIMETYPE_EXT)
972 }
973 }
974
975 return data
976 }
977
978 function updateWebserverUrls () {
979 WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
980 WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
981 WEBSERVER.WS = CONFIG.WEBSERVER.WS
982
983 WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME
984 WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME
985 WEBSERVER.PORT = CONFIG.WEBSERVER.PORT
986
987 WEBSERVER.RTMP_URL = 'rtmp://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.LIVE.RTMP.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
988 }
989
990 function updateWebserverConfig () {
991 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
992 MIMETYPES.VIDEO.MIMETYPES_REGEX = buildMimetypesRegex(MIMETYPES.VIDEO.MIMETYPE_EXT)
993
994 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
995
996 MIMETYPES.VIDEO.EXT_MIMETYPE = buildVideoExtMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT)
997
998 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = Object.keys(MIMETYPES.VIDEO.EXT_MIMETYPE)
999 }
1000
1001 function buildVideoExtMimetype (obj: { [ id: string ]: string | string[] }) {
1002 const result: { [id: string]: string } = {}
1003
1004 for (const mimetype of Object.keys(obj)) {
1005 const value = obj[mimetype]
1006 if (!value) continue
1007
1008 const extensions = Array.isArray(value) ? value : [ value ]
1009
1010 for (const extension of extensions) {
1011 result[extension] = mimetype
1012 }
1013 }
1014
1015 return result
1016 }
1017
1018 function buildMimetypesRegex (obj: { [id: string]: string | string[] }) {
1019 return Object.keys(obj)
1020 .map(m => `(${m})`)
1021 .join('|')
1022 }
1023
1024 function loadLanguages () {
1025 Object.assign(VIDEO_LANGUAGES, buildLanguages())
1026 }
1027
1028 function buildLanguages () {
1029 const iso639 = require('iso-639-3')
1030
1031 const languages: { [id: string]: string } = {}
1032
1033 const additionalLanguages = {
1034 sgn: true, // Sign languages (macro language)
1035 ase: true, // American sign language
1036 sdl: true, // Arabian sign language
1037 bfi: true, // British sign language
1038 bzs: true, // Brazilian sign language
1039 csl: true, // Chinese sign language
1040 cse: true, // Czech sign language
1041 dsl: true, // Danish sign language
1042 fsl: true, // French sign language
1043 gsg: true, // German sign language
1044 pks: true, // Pakistan sign language
1045 jsl: true, // Japanese sign language
1046 sfs: true, // South African sign language
1047 swl: true, // Swedish sign language
1048 rsl: true, // Russian sign language
1049
1050 kab: true, // Kabyle
1051
1052 epo: true, // Esperanto
1053 tlh: true, // Klingon
1054 jbo: true, // Lojban
1055 avk: true // Kotava
1056 }
1057
1058 // Only add ISO639-1 languages and some sign languages (ISO639-3)
1059 iso639
1060 .filter(l => {
1061 return (l.iso6391 !== undefined && l.type === 'living') ||
1062 additionalLanguages[l.iso6393] === true
1063 })
1064 .forEach(l => { languages[l.iso6391 || l.iso6393] = l.name })
1065
1066 // Override Occitan label
1067 languages['oc'] = 'Occitan'
1068 languages['el'] = 'Greek'
1069
1070 return languages
1071 }
1072
1073 function generateContentHash () {
1074 return randomBytes(20).toString('hex')
1075 }