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