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