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