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'
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'
25 // ---------------------------------------------------------------------------
27 const LAST_MIGRATION_VERSION = 715
29 // ---------------------------------------------------------------------------
31 const API_VERSION = 'v1'
32 const PEERTUBE_VERSION: string = require(join(root(), 'package.json')).version
59 // Sortable columns per schema
60 const SORTABLE_COLUMNS = {
61 ADMIN_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' ],
68 VIDEO_COMMENT_THREADS: [ 'createdAt', 'totalReplies' ],
69 VIDEO_COMMENTS: [ 'createdAt' ],
71 VIDEO_RATES: [ 'createdAt' ],
72 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
74 INSTANCE_FOLLOWERS: [ 'createdAt', 'state', 'score' ],
75 INSTANCE_FOLLOWING: [ 'createdAt', 'redundancyAllowed', 'state' ],
76 ACCOUNT_FOLLOWERS: [ 'createdAt' ],
77 CHANNEL_FOLLOWERS: [ 'createdAt' ],
79 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending', 'hot', 'best' ],
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' ],
86 ABUSES: [ 'id', 'createdAt', 'state' ],
88 ACCOUNTS_BLOCKLIST: [ 'createdAt' ],
89 SERVERS_BLOCKLIST: [ 'createdAt' ],
91 USER_NOTIFICATIONS: [ 'createdAt', 'read' ],
93 VIDEO_PLAYLISTS: [ 'name', 'displayName', 'createdAt', 'updatedAt' ],
95 PLUGINS: [ 'name', 'createdAt', 'updatedAt' ],
97 AVAILABLE_PLUGINS: [ 'npmName', 'popularity' ],
99 VIDEO_REDUNDANCIES: [ 'name' ]
102 const OAUTH_LIFETIME = {
103 ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
104 REFRESH_TOKEN: 1209600 // 2 weeks
107 const ROUTE_CACHE_LIFETIME = {
111 SECURITYTXT: '2 hours',
112 NODEINFO: '10 minutes',
113 DNT_POLICY: '1 week',
115 VIDEOS: '1 second' // 1 second, cache concurrent requests after a broadcast for example
120 // ---------------------------------------------------------------------------
122 // Number of points we add/remove after a successful/bad request
123 const ACTOR_FOLLOW_SCORE = {
130 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
135 const REMOTE_SCHEME = {
140 const JOB_ATTEMPTS: { [id in JobType]: number } = {
141 'activitypub-http-broadcast': 1,
142 'activitypub-http-broadcast-parallel': 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,
152 'videos-views-stats': 1,
153 'activitypub-refresher': 1,
154 'video-redundancy': 1,
155 'video-live-ending': 1,
156 'video-studio-edition': 1,
157 'manage-video-torrent': 1,
158 'move-to-object-storage': 3
160 // Excluded keys are jobs that can be configured by admins
161 const JOB_CONCURRENCY: { [id in Exclude<JobType, 'video-transcoding' | 'video-import'>]: number } = {
162 'activitypub-http-broadcast': 1,
163 'activitypub-http-broadcast-parallel': 30,
164 'activitypub-http-unicast': 10,
165 'activitypub-http-fetcher': 3,
166 'activitypub-cleaner': 1,
167 'activitypub-follow': 1,
168 'video-file-import': 1,
171 'videos-views-stats': 1,
172 'activitypub-refresher': 1,
173 'video-redundancy': 1,
174 'video-live-ending': 10,
175 'video-studio-edition': 1,
176 'manage-video-torrent': 1,
177 'move-to-object-storage': 1
179 const JOB_TTL: { [id in JobType]: number } = {
180 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
181 'activitypub-http-broadcast-parallel': 60000 * 10, // 10 minutes
182 'activitypub-http-unicast': 60000 * 10, // 10 minutes
183 'activitypub-http-fetcher': 1000 * 3600 * 10, // 10 hours
184 'activitypub-follow': 60000 * 10, // 10 minutes
185 'activitypub-cleaner': 1000 * 3600, // 1 hour
186 'video-file-import': 1000 * 3600, // 1 hour
187 'video-transcoding': 1000 * 3600 * 48, // 2 days, transcoding could be long
188 'video-studio-edition': 1000 * 3600 * 10, // 10 hours
189 'video-import': CONFIG.IMPORT.VIDEOS.TIMEOUT,
190 'email': 60000 * 10, // 10 minutes
191 'actor-keys': 60000 * 20, // 20 minutes
192 'videos-views-stats': undefined, // Unlimited
193 'activitypub-refresher': 60000 * 10, // 10 minutes
194 'video-redundancy': 1000 * 3600 * 3, // 3 hours
195 'video-live-ending': 1000 * 60 * 10, // 10 minutes
196 'manage-video-torrent': 1000 * 3600 * 3, // 3 hours
197 'move-to-object-storage': 1000 * 60 * 60 * 3 // 3 hours
199 const REPEAT_JOBS: { [ id in JobType ]?: EveryRepeatOptions | CronRepeatOptions } = {
200 'videos-views-stats': {
201 cron: randomInt(1, 20) + ' * * * *' // Between 1-20 minutes past the hour
203 'activitypub-cleaner': {
204 cron: '30 5 * * ' + randomInt(0, 7) // 1 time per week (random day) at 5:30 AM
207 const JOB_PRIORITY = {
211 const BROADCAST_CONCURRENCY = 30 // How many requests in parallel we do in activitypub-http-broadcast job
212 const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
215 CONCURRENCY: 10, // How many requests in parallel we do in activitypub-cleaner job
216 UNAVAILABLE_TRESHOLD: 3, // How many attempts we do before removing an unavailable remote resource
217 PERIOD: parseDurationToMs('1 week') // /!\ Has to be sync with REPEAT_JOBS
220 const REQUEST_TIMEOUTS = {
221 DEFAULT: 7000, // 7 seconds
222 FILE: 30000, // 30 seconds
223 REDUNDANCY: JOB_TTL['video-redundancy']
226 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
227 const VIDEO_IMPORT_TIMEOUT = Math.floor(JOB_TTL['video-import'] * 0.9)
229 const SCHEDULER_INTERVALS_MS = {
230 ACTOR_FOLLOW_SCORES: 60000 * 60, // 1 hour
231 REMOVE_OLD_JOBS: 60000 * 60, // 1 hour
232 UPDATE_VIDEOS: 60000, // 1 minute
233 YOUTUBE_DL_UPDATE: 60000 * 60 * 24, // 1 day
234 GEO_IP_UPDATE: 60000 * 60 * 24, // 1 day
235 VIDEO_VIEWS_BUFFER_UPDATE: CONFIG.VIEWS.VIDEOS.LOCAL_BUFFER_UPDATE_INTERVAL,
236 CHECK_PLUGINS: CONFIG.PLUGINS.INDEX.CHECK_LATEST_VERSIONS_INTERVAL,
237 CHECK_PEERTUBE_VERSION: 60000 * 60 * 24, // 1 day
238 AUTO_FOLLOW_INDEX_INSTANCES: 60000 * 60 * 24, // 1 day
239 REMOVE_OLD_VIEWS: 60000 * 60 * 24, // 1 day
240 REMOVE_OLD_HISTORY: 60000 * 60 * 24, // 1 day
241 UPDATE_INBOX_STATS: 1000 * 60, // 1 minute
242 REMOVE_DANGLING_RESUMABLE_UPLOADS: 60000 * 60 // 1 hour
245 // ---------------------------------------------------------------------------
247 const CONSTRAINTS_FIELDS = {
249 NAME: { min: 1, max: 120 }, // Length
250 DESCRIPTION: { min: 3, max: 1000 }, // Length
251 USERNAME: { min: 1, max: 50 }, // Length
252 PASSWORD: { min: 6, max: 255 }, // Length
253 VIDEO_QUOTA: { min: -1 },
254 VIDEO_QUOTA_DAILY: { min: -1 },
255 VIDEO_LANGUAGES: { max: 500 }, // Array length
256 BLOCKED_REASON: { min: 3, max: 250 } // Length
259 REASON: { min: 2, max: 3000 }, // Length
260 MODERATION_COMMENT: { min: 2, max: 3000 } // Length
263 MESSAGE: { min: 2, max: 3000 } // Length
266 REASON: { min: 2, max: 300 } // Length
269 NAME: { min: 1, max: 120 }, // Length
270 DESCRIPTION: { min: 3, max: 1000 }, // Length
271 SUPPORT: { min: 3, max: 1000 }, // Length
272 URL: { min: 3, max: 2000 } // Length
276 EXTNAME: [ '.vtt', '.srt' ],
278 max: 20 * 1024 * 1024 // 20MB
283 URL: { min: 3, max: 2000 }, // Length
284 TORRENT_NAME: { min: 3, max: 255 }, // Length
286 EXTNAME: [ '.torrent' ],
288 max: 1024 * 200 // 200 KB
293 URL: { min: 3, max: 2000 } // Length
296 URL: { min: 3, max: 2000 } // Length
299 NAME: { min: 3, max: 120 }, // Length
300 LANGUAGE: { min: 1, max: 10 }, // Length
301 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
302 DESCRIPTION: { min: 3, max: 10000 }, // Length
303 SUPPORT: { min: 3, max: 1000 }, // Length
305 EXTNAME: [ '.png', '.jpg', '.jpeg', '.webp' ],
307 max: 4 * 1024 * 1024 // 4MB
310 EXTNAME: [] as string[],
311 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
312 DURATION: { min: 0 }, // Number
313 TAGS: { min: 0, max: 5 }, // Number of total tags
314 TAG: { min: 2, max: 30 }, // Length
317 DISLIKES: { min: 0 },
318 FILE_SIZE: { min: -1 },
319 PARTIAL_UPLOAD_SIZE: { max: 50 * 1024 * 1024 * 1024 }, // 50GB
320 URL: { min: 3, max: 2000 } // Length
323 NAME: { min: 1, max: 120 }, // Length
324 DESCRIPTION: { min: 3, max: 1000 }, // Length
325 URL: { min: 3, max: 2000 }, // Length
327 EXTNAME: [ '.jpg', '.jpeg' ],
329 max: 4 * 1024 * 1024 // 4MB
334 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
335 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
336 URL: { min: 3, max: 2000 }, // Length
338 EXTNAME: [ '.png', '.jpeg', '.jpg', '.gif', '.webp' ],
340 max: 4 * 1024 * 1024 // 4MB
348 TEXT: { min: 1, max: 10000 }, // Length
349 URL: { min: 3, max: 2000 } // Length
352 URL: { min: 3, max: 2000 } // Length
355 FROM_NAME: { min: 1, max: 120 }, // Length
356 BODY: { min: 3, max: 5000 } // Length
359 NAME: { min: 1, max: 214 }, // Length
360 DESCRIPTION: { min: 1, max: 20000 } // Length
363 URL: { min: 5, max: 2000 } // Length
366 TASKS: { min: 1, max: 10 }, // Number of tasks
367 CUT_TIME: { min: 0 } // Value
371 const VIEW_LIFETIME = {
372 VIEW: CONFIG.VIEWS.VIDEOS.IP_VIEW_EXPIRATION,
373 VIEWER_COUNTER: 60000 * 2, // 2 minutes
374 VIEWER_STATS: 60000 * 60 // 1 hour
377 const MAX_LOCAL_VIEWER_WATCH_SECTIONS = 100
379 let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour
381 const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
383 STANDARD: [ 24, 25, 30 ],
384 HD_STANDARD: [ 50, 60 ],
388 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
391 const DEFAULT_AUDIO_RESOLUTION = VideoResolution.H_480P
393 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
398 const FFMPEG_NICE: { [ id: string ]: number } = {
399 // parent process defaults to niceness = 0
400 // reminder: lower = higher priority, max value is 19, lowest is -20
401 LIVE: 5, // prioritize over VOD and THUMBNAIL
406 const VIDEO_CATEGORIES = {
417 11: 'News & Politics',
421 15: 'Science & Technology',
427 // See https://creativecommons.org/licenses/?lang=en
428 const VIDEO_LICENCES = {
430 2: 'Attribution - Share Alike',
431 3: 'Attribution - No Derivatives',
432 4: 'Attribution - Non Commercial',
433 5: 'Attribution - Non Commercial - Share Alike',
434 6: 'Attribution - Non Commercial - No Derivatives',
435 7: 'Public Domain Dedication'
438 const VIDEO_LANGUAGES: { [id: string]: string } = {}
440 const VIDEO_PRIVACIES: { [ id in VideoPrivacy ]: string } = {
441 [VideoPrivacy.PUBLIC]: 'Public',
442 [VideoPrivacy.UNLISTED]: 'Unlisted',
443 [VideoPrivacy.PRIVATE]: 'Private',
444 [VideoPrivacy.INTERNAL]: 'Internal'
447 const VIDEO_STATES: { [ id in VideoState ]: string } = {
448 [VideoState.PUBLISHED]: 'Published',
449 [VideoState.TO_TRANSCODE]: 'To transcode',
450 [VideoState.TO_IMPORT]: 'To import',
451 [VideoState.WAITING_FOR_LIVE]: 'Waiting for livestream',
452 [VideoState.LIVE_ENDED]: 'Livestream ended',
453 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE]: 'To move to an external storage',
454 [VideoState.TRANSCODING_FAILED]: 'Transcoding failed',
455 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED]: 'External storage move failed',
456 [VideoState.TO_EDIT]: 'To edit*'
459 const VIDEO_IMPORT_STATES: { [ id in VideoImportState ]: string } = {
460 [VideoImportState.FAILED]: 'Failed',
461 [VideoImportState.PENDING]: 'Pending',
462 [VideoImportState.SUCCESS]: 'Success',
463 [VideoImportState.REJECTED]: 'Rejected',
464 [VideoImportState.CANCELLED]: 'Cancelled',
465 [VideoImportState.PROCESSING]: 'Processing'
468 const ABUSE_STATES: { [ id in AbuseState ]: string } = {
469 [AbuseState.PENDING]: 'Pending',
470 [AbuseState.REJECTED]: 'Rejected',
471 [AbuseState.ACCEPTED]: 'Accepted'
474 const VIDEO_PLAYLIST_PRIVACIES: { [ id in VideoPlaylistPrivacy ]: string } = {
475 [VideoPlaylistPrivacy.PUBLIC]: 'Public',
476 [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted',
477 [VideoPlaylistPrivacy.PRIVATE]: 'Private'
480 const VIDEO_PLAYLIST_TYPES: { [ id in VideoPlaylistType ]: string } = {
481 [VideoPlaylistType.REGULAR]: 'Regular',
482 [VideoPlaylistType.WATCH_LATER]: 'Watch later'
488 'audio/mpeg': '.mp3',
491 'application/ogg': '.ogg',
494 'audio/x-ms-wma': '.wma',
496 'audio/x-wav': '.wav',
498 'audio/x-flac': '.flac',
499 'audio/flac': '.flac',
501 'audio/vnd.dlna.adts': '.aac',
506 'audio/x-m4a': '.m4a',
508 'audio/vnd.dolby.dd-raw': '.ac3',
511 EXT_MIMETYPE: null as { [ id: string ]: string }
514 MIMETYPE_EXT: null as { [ id: string ]: string | string[] },
515 MIMETYPES_REGEX: null as string,
516 EXT_MIMETYPE: null as { [ id: string ]: string }
522 'image/webp': '.webp',
526 EXT_MIMETYPE: null as { [ id: string ]: string }
531 'application/x-subrip': '.srt',
537 'application/x-bittorrent': '.torrent'
541 MIMETYPES.AUDIO.EXT_MIMETYPE = invert(MIMETYPES.AUDIO.MIMETYPE_EXT)
542 MIMETYPES.IMAGE.EXT_MIMETYPE = invert(MIMETYPES.IMAGE.MIMETYPE_EXT)
544 const BINARY_CONTENT_TYPES = new Set([
545 'binary/octet-stream',
546 'application/octet-stream',
547 'application/x-binary'
550 // ---------------------------------------------------------------------------
559 // ---------------------------------------------------------------------------
561 const SERVER_ACTOR_NAME = 'peertube'
563 const ACTIVITY_PUB = {
564 POTENTIAL_ACCEPT_HEADERS: [
565 'application/activity+json',
566 'application/ld+json',
567 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
569 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
570 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
571 COLLECTION_ITEMS_PER_PAGE: 10,
572 FETCH_PAGE_LIMIT: 2000,
574 VIDEO: [] as string[],
575 TORRENT: [ 'application/x-bittorrent' ],
576 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
578 MAX_RECURSION_COMMENTS: 100,
579 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
580 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
581 VIDEO_PLAYLIST_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2 // 2 days
584 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
587 APPLICATION: 'Application',
588 ORGANIZATION: 'Organization',
592 const HTTP_SIGNATURE = {
593 HEADER_NAME: 'signature',
594 ALGORITHM: 'rsa-sha256',
595 HEADERS_TO_SIGN: [ '(request-target)', 'host', 'date', 'digest' ],
596 CLOCK_SKEW_SECONDS: 1800
599 // ---------------------------------------------------------------------------
601 let PRIVATE_RSA_KEY_SIZE = 2048
603 // Password encryption
604 const BCRYPT_SALT_SIZE = 10
606 const USER_PASSWORD_RESET_LIFETIME = 60000 * 60 // 60 minutes
607 const USER_PASSWORD_CREATE_LIFETIME = 60000 * 60 * 24 * 7 // 7 days
609 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
611 const NSFW_POLICY_TYPES: { [ id: string ]: NSFWPolicyType } = {
612 DO_NOT_LIST: 'do_not_list',
617 // ---------------------------------------------------------------------------
619 // Express static paths (router)
620 const STATIC_PATHS = {
621 THUMBNAILS: '/static/thumbnails/',
622 WEBSEED: '/static/webseed/',
623 REDUNDANCY: '/static/redundancy/',
624 STREAMING_PLAYLISTS: {
625 HLS: '/static/streaming-playlists/hls'
628 const STATIC_DOWNLOAD_PATHS = {
629 TORRENTS: '/download/torrents/',
630 VIDEOS: '/download/videos/',
631 HLS_VIDEOS: '/download/streaming-playlists/hls/videos/'
633 const LAZY_STATIC_PATHS = {
634 BANNERS: '/lazy-static/banners/',
635 AVATARS: '/lazy-static/avatars/',
636 PREVIEWS: '/lazy-static/previews/',
637 VIDEO_CAPTIONS: '/lazy-static/video-captions/',
638 TORRENTS: '/lazy-static/torrents/'
642 const STATIC_MAX_AGE = {
648 // Videos thumbnail size
649 const THUMBNAILS_SIZE = {
654 const PREVIEWS_SIZE = {
659 const ACTOR_IMAGES_SIZE: { [key in ActorImageType]: { width: number, height: number }[]} = {
660 [ActorImageType.AVATAR]: [
670 [ActorImageType.BANNER]: [
673 height: 317 // 6/1 ratio
683 // Sub folders of cache directory
684 const FILES_CACHE = {
686 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
687 MAX_AGE: 1000 * 3600 * 3 // 3 hours
690 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
691 MAX_AGE: 1000 * 3600 * 3 // 3 hours
694 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'torrents'),
695 MAX_AGE: 1000 * 3600 * 3 // 3 hours
703 ACTOR_IMAGE_STATIC: {
708 const RESUMABLE_UPLOAD_DIRECTORY = join(CONFIG.STORAGE.TMP_DIR, 'resumable-uploads')
709 const HLS_STREAMING_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.STREAMING_PLAYLISTS_DIR, 'hls')
710 const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls')
712 const RESUMABLE_UPLOAD_SESSION_LIFETIME = SCHEDULER_INTERVALS_MS.REMOVE_DANGLING_RESUMABLE_UPLOADS
716 CLEANUP_DELAY: 1000 * 60 * 5, // 5 minutes
717 SEGMENT_TIME_SECONDS: {
718 DEFAULT_LATENCY: 4, // 4 seconds
719 SMALL_LATENCY: 2 // 2 seconds
721 SEGMENTS_LIST_SIZE: 15, // 15 maximum segments in live playlist
722 REPLAY_DIRECTORY: 'replay',
723 EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION: 4,
724 MAX_SOCKET_WAITING_DATA: 1024 * 1000 * 100, // 100MB
734 const MEMOIZE_TTL = {
735 OVERVIEWS_SAMPLE: 1000 * 3600 * 4, // 4 hours
736 INFO_HASH_EXISTS: 1000 * 3600 * 12, // 12 hours
737 VIDEO_DURATION: 1000 * 10, // 10 seconds
738 LIVE_ABLE_TO_UPLOAD: 1000 * 60, // 1 minute
739 LIVE_CHECK_SOCKET_HEALTH: 1000 * 60 // 1 minute
742 const MEMOIZE_LENGTH = {
743 INFO_HASH_EXISTS: 200,
747 const QUEUE_CONCURRENCY = {
748 ACTOR_PROCESS_IMAGE: 3
757 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
759 const ASSETS_PATH = {
760 DEFAULT_AUDIO_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-audio-background.jpg'),
761 DEFAULT_LIVE_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-live-background.jpg')
764 // ---------------------------------------------------------------------------
766 const CUSTOM_HTML_TAG_COMMENTS = {
767 TITLE: '<!-- title tag -->',
768 DESCRIPTION: '<!-- description tag -->',
769 CUSTOM_CSS: '<!-- custom css tag -->',
770 META_TAGS: '<!-- meta tags -->',
771 SERVER_CONFIG: '<!-- server config -->'
774 const MAX_LOGS_OUTPUT_CHARACTERS = 10 * 1000 * 1000
775 const LOG_FILENAME = 'peertube.log'
776 const AUDIT_LOG_FILENAME = 'peertube-audit.log'
778 // ---------------------------------------------------------------------------
780 const TRACKER_RATE_LIMITS = {
781 INTERVAL: 60000 * 5, // 5 minutes
782 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
783 ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
784 BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
787 const P2P_MEDIA_LOADER_PEER_VERSION = 2
789 // ---------------------------------------------------------------------------
791 const PLUGIN_GLOBAL_CSS_FILE_NAME = 'plugins-global.css'
792 const PLUGIN_GLOBAL_CSS_PATH = join(CONFIG.STORAGE.TMP_DIR, PLUGIN_GLOBAL_CSS_FILE_NAME)
794 let PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 1000 * 60 * 5 // 5 minutes
796 const DEFAULT_THEME_NAME = 'default'
797 const DEFAULT_USER_THEME_NAME = 'instance-default'
799 // ---------------------------------------------------------------------------
801 const SEARCH_INDEX = {
803 VIDEOS: '/api/v1/search/videos',
804 VIDEO_CHANNELS: '/api/v1/search/video-channels'
808 // ---------------------------------------------------------------------------
810 const STATS_TIMESERIE = {
811 MAX_DAYS: 365 * 10 // Around 10 years
814 // ---------------------------------------------------------------------------
816 // Special constants for a test instance
817 if (isTestInstance() === true && process.env.PRODUCTION_CONSTANTS !== 'true') {
818 PRIVATE_RSA_KEY_SIZE = 1024
820 ACTOR_FOLLOW_SCORE.BASE = 20
822 REMOTE_SCHEME.HTTP = 'http'
823 REMOTE_SCHEME.WS = 'ws'
825 STATIC_MAX_AGE.SERVER = '0'
827 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
828 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
829 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
830 ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
832 CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max = 100 * 1024 // 100KB
833 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max = 400 * 1024 // 400KB
835 SCHEDULER_INTERVALS_MS.ACTOR_FOLLOW_SCORES = 1000
836 SCHEDULER_INTERVALS_MS.REMOVE_OLD_JOBS = 10000
837 SCHEDULER_INTERVALS_MS.REMOVE_OLD_HISTORY = 5000
838 SCHEDULER_INTERVALS_MS.REMOVE_OLD_VIEWS = 5000
839 SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS = 5000
840 SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES = 5000
841 SCHEDULER_INTERVALS_MS.UPDATE_INBOX_STATS = 5000
842 SCHEDULER_INTERVALS_MS.CHECK_PEERTUBE_VERSION = 2000
844 REPEAT_JOBS['videos-views-stats'] = { every: 5000 }
846 REPEAT_JOBS['activitypub-cleaner'] = { every: 5000 }
847 AP_CLEANER.PERIOD = 5000
849 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
851 VIEW_LIFETIME.VIEWER_COUNTER = 1000 * 5 // 5 second
852 VIEW_LIFETIME.VIEWER_STATS = 1000 * 5 // 5 second
853 CONTACT_FORM_LIFETIME = 1000 // 1 second
855 JOB_ATTEMPTS['email'] = 1
857 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
858 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 3000
859 MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD = 3000
860 OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD = 2
862 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 5000
864 VIDEO_LIVE.CLEANUP_DELAY = 5000
865 VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY = 2
866 VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY = 1
867 VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION = 1
870 updateWebserverUrls()
871 updateWebserverConfig()
873 registerConfigChangedHandler(() => {
874 updateWebserverUrls()
875 updateWebserverConfig()
878 // ---------------------------------------------------------------------------
880 const FILES_CONTENT_HASH = {
881 MANIFEST: generateContentHash(),
882 FAVICON: generateContentHash(),
883 LOGO: generateContentHash()
886 // ---------------------------------------------------------------------------
888 const VIDEO_FILTERS = {
891 HORIZONTAL_MARGIN_RATIO: 1 / 20,
892 VERTICAL_MARGIN_RATIO: 1 / 20
896 // ---------------------------------------------------------------------------
905 RESUMABLE_UPLOAD_DIRECTORY,
906 RESUMABLE_UPLOAD_SESSION_LIFETIME,
907 HLS_REDUNDANCY_DIRECTORY,
908 P2P_MEDIA_LOADER_PEER_VERSION,
921 LAST_MIGRATION_VERSION,
923 CUSTOM_HTML_TAG_COMMENTS,
925 BROADCAST_CONCURRENCY,
932 DEFAULT_USER_THEME_NAME,
934 PLUGIN_GLOBAL_CSS_FILE_NAME,
935 PLUGIN_GLOBAL_CSS_PATH,
936 PRIVATE_RSA_KEY_SIZE,
938 ROUTE_CACHE_LIFETIME,
940 HLS_STREAMING_PLAYLIST_DIRECTORY,
946 VIDEO_IMPORT_TIMEOUT,
947 VIDEO_PLAYLIST_TYPES,
948 MAX_LOGS_OUTPUT_CHARACTERS,
950 ACTIVITY_PUB_ACTOR_TYPES,
961 VIDEO_TRANSCODING_FPS,
966 MAX_LOCAL_VIEWER_WATCH_SECTIONS,
967 USER_PASSWORD_RESET_LIFETIME,
968 USER_PASSWORD_CREATE_LIFETIME,
970 USER_EMAIL_VERIFY_LIFETIME,
972 SCHEDULER_INTERVALS_MS,
974 STATIC_DOWNLOAD_PATHS,
976 CRAWL_REQUEST_CONCURRENCY,
977 DEFAULT_AUDIO_RESOLUTION,
978 BINARY_CONTENT_TYPES,
979 JOB_COMPLETED_LIFETIME,
983 CONTACT_FORM_LIFETIME,
984 VIDEO_PLAYLIST_PRIVACIES,
985 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
993 // ---------------------------------------------------------------------------
995 function buildVideoMimetypeExt () {
997 // streamable formats that warrant cross-browser compatibility
998 'video/webm': '.webm',
999 // We'll add .ogg if additional extensions are enabled
1000 // We could add .ogg here but since it could be an audio file,
1001 // it would be confusing for users because PeerTube will refuse their file (based on the mimetype)
1002 'video/ogg': [ '.ogv' ],
1006 if (CONFIG.TRANSCODING.ENABLED) {
1007 if (CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
1008 data['video/ogg'].push('.ogg')
1010 Object.assign(data, {
1011 'video/x-matroska': '.mkv',
1013 // Developed by Apple
1014 'video/quicktime': [ '.mov', '.qt', '.mqv' ], // often used as output format by editing software
1015 'video/x-m4v': '.m4v',
1016 'video/m4v': '.m4v',
1018 // Developed by the Adobe Flash Platform
1019 'video/x-flv': '.flv',
1020 'video/x-f4v': '.f4v', // replacement for flv
1022 // Developed by Microsoft
1023 'video/x-ms-wmv': '.wmv',
1024 'video/x-msvideo': '.avi',
1025 'video/avi': '.avi',
1027 // Developed by 3GPP
1028 // common video formats for cell phones
1029 'video/3gpp': [ '.3gp', '.3gpp' ],
1030 'video/3gpp2': [ '.3g2', '.3gpp2' ],
1032 // Developed by FFmpeg/Mplayer
1033 'application/x-nut': '.nut',
1035 // The standard video format used by many Sony and Panasonic HD camcorders.
1036 // It is also used for storing high definition video on Blu-ray discs.
1037 'video/mp2t': '.mts',
1038 'video/vnd.dlna.mpeg-tts': '.mts',
1040 'video/m2ts': '.m2ts',
1042 // Old formats reliant on MPEG-1/MPEG-2
1043 'video/mpv': '.mpv',
1044 'video/mpeg2': '.m2v',
1045 'video/mpeg': [ '.m1v', '.mpg', '.mpe', '.mpeg', '.vob' ],
1046 'video/dvd': '.vob',
1048 // Could be anything
1049 'application/octet-stream': null,
1050 'application/mxf': '.mxf' // often used as exchange format by editing software
1054 if (CONFIG.TRANSCODING.ALLOW_AUDIO_FILES) {
1055 Object.assign(data, MIMETYPES.AUDIO.MIMETYPE_EXT)
1062 function updateWebserverUrls () {
1063 WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
1064 WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
1065 WEBSERVER.WS = CONFIG.WEBSERVER.WS
1067 WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME
1068 WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME
1069 WEBSERVER.PORT = CONFIG.WEBSERVER.PORT
1071 const rtmpHostname = CONFIG.LIVE.RTMP.PUBLIC_HOSTNAME || CONFIG.WEBSERVER.HOSTNAME
1072 const rtmpsHostname = CONFIG.LIVE.RTMPS.PUBLIC_HOSTNAME || CONFIG.WEBSERVER.HOSTNAME
1074 WEBSERVER.RTMP_URL = 'rtmp://' + rtmpHostname + ':' + CONFIG.LIVE.RTMP.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1075 WEBSERVER.RTMPS_URL = 'rtmps://' + rtmpsHostname + ':' + CONFIG.LIVE.RTMPS.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1078 function updateWebserverConfig () {
1079 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
1080 MIMETYPES.VIDEO.MIMETYPES_REGEX = buildMimetypesRegex(MIMETYPES.VIDEO.MIMETYPE_EXT)
1082 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
1084 MIMETYPES.VIDEO.EXT_MIMETYPE = buildVideoExtMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT)
1086 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = Object.keys(MIMETYPES.VIDEO.EXT_MIMETYPE)
1089 function buildVideoExtMimetype (obj: { [ id: string ]: string | string[] }) {
1090 const result: { [id: string]: string } = {}
1092 for (const mimetype of Object.keys(obj)) {
1093 const value = obj[mimetype]
1094 if (!value) continue
1096 const extensions = Array.isArray(value) ? value : [ value ]
1098 for (const extension of extensions) {
1099 result[extension] = mimetype
1106 function buildMimetypesRegex (obj: { [id: string]: string | string[] }) {
1107 return Object.keys(obj)
1112 function loadLanguages () {
1113 Object.assign(VIDEO_LANGUAGES, buildLanguages())
1116 function buildLanguages () {
1117 const iso639 = require('iso-639-3')
1119 const languages: { [id: string]: string } = {}
1121 const additionalLanguages = {
1122 sgn: true, // Sign languages (macro language)
1123 ase: true, // American sign language
1124 sdl: true, // Arabian sign language
1125 bfi: true, // British sign language
1126 bzs: true, // Brazilian sign language
1127 csl: true, // Chinese sign language
1128 cse: true, // Czech sign language
1129 dsl: true, // Danish sign language
1130 fsl: true, // French sign language
1131 gsg: true, // German sign language
1132 pks: true, // Pakistan sign language
1133 jsl: true, // Japanese sign language
1134 sfs: true, // South African sign language
1135 swl: true, // Swedish sign language
1136 rsl: true, // Russian sign language
1138 kab: true, // Kabyle
1142 epo: true, // Esperanto
1143 tlh: true, // Klingon
1144 jbo: true, // Lojban
1145 avk: true, // Kotava
1147 zxx: true // No linguistic content (ISO-639-2)
1150 // Only add ISO639-1 languages and some sign languages (ISO639-3)
1153 return (l.iso6391 !== undefined && l.type === 'living') ||
1154 additionalLanguages[l.iso6393] === true
1156 .forEach(l => { languages[l.iso6391 || l.iso6393] = l.name })
1158 // Override Occitan label
1159 languages['oc'] = 'Occitan'
1160 languages['el'] = 'Greek'
1162 // Chinese languages
1163 languages['zh-Hans'] = 'Simplified Chinese'
1164 languages['zh-Hant'] = 'Traditional Chinese'
1169 function generateContentHash () {
1170 return randomBytes(20).toString('hex')