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 = 690
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 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-unicast': 1,
143 'activitypub-http-fetcher': 2,
144 'activitypub-follow': 5,
145 'activitypub-cleaner': 1,
146 'video-file-import': 1,
147 'video-transcoding': 1,
151 'videos-views-stats': 1,
152 'activitypub-refresher': 1,
153 'video-redundancy': 1,
154 'video-live-ending': 1,
156 'move-to-object-storage': 3
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,
168 'videos-views-stats': 1,
169 'activitypub-refresher': 1,
170 'video-redundancy': 1,
171 'video-live-ending': 10,
173 'move-to-object-storage': 1
175 const JOB_TTL: { [id in JobType]: number } = {
176 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
177 'activitypub-http-unicast': 60000 * 10, // 10 minutes
178 'activitypub-http-fetcher': 1000 * 3600 * 10, // 10 hours
179 'activitypub-follow': 60000 * 10, // 10 minutes
180 'activitypub-cleaner': 1000 * 3600, // 1 hour
181 'video-file-import': 1000 * 3600, // 1 hour
182 'video-transcoding': 1000 * 3600 * 48, // 2 days, transcoding could be long
183 'video-edition': 1000 * 3600 * 10, // 10 hours
184 'video-import': 1000 * 3600 * 2, // 2 hours
185 'email': 60000 * 10, // 10 minutes
186 'actor-keys': 60000 * 20, // 20 minutes
187 'videos-views-stats': undefined, // Unlimited
188 'activitypub-refresher': 60000 * 10, // 10 minutes
189 'video-redundancy': 1000 * 3600 * 3, // 3 hours
190 'video-live-ending': 1000 * 60 * 10, // 10 minutes
191 'move-to-object-storage': 1000 * 60 * 60 * 3 // 3 hours
193 const REPEAT_JOBS: { [ id in JobType ]?: EveryRepeatOptions | CronRepeatOptions } = {
194 'videos-views-stats': {
195 cron: randomInt(1, 20) + ' * * * *' // Between 1-20 minutes past the hour
197 'activitypub-cleaner': {
198 cron: '30 5 * * ' + randomInt(0, 7) // 1 time per week (random day) at 5:30 AM
201 const JOB_PRIORITY = {
205 const BROADCAST_CONCURRENCY = 30 // How many requests in parallel we do in activitypub-http-broadcast job
206 const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
209 CONCURRENCY: 10, // How many requests in parallel we do in activitypub-cleaner job
210 UNAVAILABLE_TRESHOLD: 3, // How many attemps we do before removing an unavailable remote resource
211 PERIOD: parseDurationToMs('1 week') // /!\ Has to be sync with REPEAT_JOBS
214 const REQUEST_TIMEOUTS = {
215 DEFAULT: 7000, // 7 seconds
216 FILE: 30000, // 30 seconds
217 REDUNDANCY: JOB_TTL['video-redundancy']
220 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
221 const VIDEO_IMPORT_TIMEOUT = Math.floor(JOB_TTL['video-import'] * 0.9)
223 const SCHEDULER_INTERVALS_MS = {
224 ACTOR_FOLLOW_SCORES: 60000 * 60, // 1 hour
225 REMOVE_OLD_JOBS: 60000 * 60, // 1 hour
226 UPDATE_VIDEOS: 60000, // 1 minute
227 YOUTUBE_DL_UPDATE: 60000 * 60 * 24, // 1 day
228 VIDEO_VIEWS_BUFFER_UPDATE: CONFIG.VIEWS.VIDEOS.LOCAL_BUFFER_UPDATE_INTERVAL,
229 CHECK_PLUGINS: CONFIG.PLUGINS.INDEX.CHECK_LATEST_VERSIONS_INTERVAL,
230 CHECK_PEERTUBE_VERSION: 60000 * 60 * 24, // 1 day
231 AUTO_FOLLOW_INDEX_INSTANCES: 60000 * 60 * 24, // 1 day
232 REMOVE_OLD_VIEWS: 60000 * 60 * 24, // 1 day
233 REMOVE_OLD_HISTORY: 60000 * 60 * 24, // 1 day
234 UPDATE_INBOX_STATS: 1000 * 60, // 1 minute
235 REMOVE_DANGLING_RESUMABLE_UPLOADS: 60000 * 60 // 1 hour
238 // ---------------------------------------------------------------------------
240 const CONSTRAINTS_FIELDS = {
242 NAME: { min: 1, max: 120 }, // Length
243 DESCRIPTION: { min: 3, max: 1000 }, // Length
244 USERNAME: { min: 1, max: 50 }, // Length
245 PASSWORD: { min: 6, max: 255 }, // Length
246 VIDEO_QUOTA: { min: -1 },
247 VIDEO_QUOTA_DAILY: { min: -1 },
248 VIDEO_LANGUAGES: { max: 500 }, // Array length
249 BLOCKED_REASON: { min: 3, max: 250 } // Length
252 REASON: { min: 2, max: 3000 }, // Length
253 MODERATION_COMMENT: { min: 2, max: 3000 } // Length
256 MESSAGE: { min: 2, max: 3000 } // Length
259 REASON: { min: 2, max: 300 } // Length
262 NAME: { min: 1, max: 120 }, // Length
263 DESCRIPTION: { min: 3, max: 1000 }, // Length
264 SUPPORT: { min: 3, max: 1000 }, // Length
265 URL: { min: 3, max: 2000 } // Length
269 EXTNAME: [ '.vtt', '.srt' ],
271 max: 20 * 1024 * 1024 // 20MB
276 URL: { min: 3, max: 2000 }, // Length
277 TORRENT_NAME: { min: 3, max: 255 }, // Length
279 EXTNAME: [ '.torrent' ],
281 max: 1024 * 200 // 200 KB
286 URL: { min: 3, max: 2000 } // Length
289 URL: { min: 3, max: 2000 } // Length
292 NAME: { min: 3, max: 120 }, // Length
293 LANGUAGE: { min: 1, max: 10 }, // Length
294 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
295 DESCRIPTION: { min: 3, max: 10000 }, // Length
296 SUPPORT: { min: 3, max: 1000 }, // Length
298 EXTNAME: [ '.png', '.jpg', '.jpeg', '.webp' ],
300 max: 4 * 1024 * 1024 // 4MB
303 EXTNAME: [] as string[],
304 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
305 DURATION: { min: 0 }, // Number
306 TAGS: { min: 0, max: 5 }, // Number of total tags
307 TAG: { min: 2, max: 30 }, // Length
310 DISLIKES: { min: 0 },
311 FILE_SIZE: { min: -1 },
312 PARTIAL_UPLOAD_SIZE: { max: 50 * 1024 * 1024 * 1024 }, // 50GB
313 URL: { min: 3, max: 2000 } // Length
316 NAME: { min: 1, max: 120 }, // Length
317 DESCRIPTION: { min: 3, max: 1000 }, // Length
318 URL: { min: 3, max: 2000 }, // Length
320 EXTNAME: [ '.jpg', '.jpeg' ],
322 max: 4 * 1024 * 1024 // 4MB
327 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
328 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
329 URL: { min: 3, max: 2000 }, // Length
331 EXTNAME: [ '.png', '.jpeg', '.jpg', '.gif', '.webp' ],
333 max: 4 * 1024 * 1024 // 4MB
341 TEXT: { min: 1, max: 10000 }, // Length
342 URL: { min: 3, max: 2000 } // Length
345 URL: { min: 3, max: 2000 } // Length
348 FROM_NAME: { min: 1, max: 120 }, // Length
349 BODY: { min: 3, max: 5000 } // Length
352 NAME: { min: 1, max: 214 }, // Length
353 DESCRIPTION: { min: 1, max: 20000 } // Length
356 URL: { min: 5, max: 2000 } // Length
359 TASKS: { min: 1, max: 10 }, // Number of tasks
360 CUT_TIME: { min: 0 } // Value
364 const VIEW_LIFETIME = {
365 VIEW: CONFIG.VIEWS.VIDEOS.IP_VIEW_EXPIRATION,
366 VIEWER: 60000 * 5 // 5 minutes
369 let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour
371 const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
373 STANDARD: [ 24, 25, 30 ],
374 HD_STANDARD: [ 50, 60 ],
378 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
381 const DEFAULT_AUDIO_RESOLUTION = VideoResolution.H_480P
383 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
388 const FFMPEG_NICE: { [ id: string ]: number } = {
389 // parent process defaults to niceness = 0
390 // reminder: lower = higher priority, max value is 19, lowest is -20
391 LIVE: 5, // prioritize over VOD and THUMBNAIL
396 const VIDEO_CATEGORIES = {
407 11: 'News & Politics',
411 15: 'Science & Technology',
417 // See https://creativecommons.org/licenses/?lang=en
418 const VIDEO_LICENCES = {
420 2: 'Attribution - Share Alike',
421 3: 'Attribution - No Derivatives',
422 4: 'Attribution - Non Commercial',
423 5: 'Attribution - Non Commercial - Share Alike',
424 6: 'Attribution - Non Commercial - No Derivatives',
425 7: 'Public Domain Dedication'
428 const VIDEO_LANGUAGES: { [id: string]: string } = {}
430 const VIDEO_PRIVACIES: { [ id in VideoPrivacy ]: string } = {
431 [VideoPrivacy.PUBLIC]: 'Public',
432 [VideoPrivacy.UNLISTED]: 'Unlisted',
433 [VideoPrivacy.PRIVATE]: 'Private',
434 [VideoPrivacy.INTERNAL]: 'Internal'
437 const VIDEO_STATES: { [ id in VideoState ]: string } = {
438 [VideoState.PUBLISHED]: 'Published',
439 [VideoState.TO_TRANSCODE]: 'To transcode',
440 [VideoState.TO_IMPORT]: 'To import',
441 [VideoState.WAITING_FOR_LIVE]: 'Waiting for livestream',
442 [VideoState.LIVE_ENDED]: 'Livestream ended',
443 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE]: 'To move to an external storage',
444 [VideoState.TRANSCODING_FAILED]: 'Transcoding failed',
445 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED]: 'External storage move failed',
446 [VideoState.TO_EDIT]: 'To edit*'
449 const VIDEO_IMPORT_STATES: { [ id in VideoImportState ]: string } = {
450 [VideoImportState.FAILED]: 'Failed',
451 [VideoImportState.PENDING]: 'Pending',
452 [VideoImportState.SUCCESS]: 'Success',
453 [VideoImportState.REJECTED]: 'Rejected',
454 [VideoImportState.CANCELLED]: 'Cancelled',
455 [VideoImportState.PROCESSING]: 'Processing'
458 const ABUSE_STATES: { [ id in AbuseState ]: string } = {
459 [AbuseState.PENDING]: 'Pending',
460 [AbuseState.REJECTED]: 'Rejected',
461 [AbuseState.ACCEPTED]: 'Accepted'
464 const VIDEO_PLAYLIST_PRIVACIES: { [ id in VideoPlaylistPrivacy ]: string } = {
465 [VideoPlaylistPrivacy.PUBLIC]: 'Public',
466 [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted',
467 [VideoPlaylistPrivacy.PRIVATE]: 'Private'
470 const VIDEO_PLAYLIST_TYPES: { [ id in VideoPlaylistType ]: string } = {
471 [VideoPlaylistType.REGULAR]: 'Regular',
472 [VideoPlaylistType.WATCH_LATER]: 'Watch later'
478 'audio/mpeg': '.mp3',
480 'application/ogg': '.ogg',
482 'audio/x-ms-wma': '.wma',
484 'audio/x-wav': '.wav',
485 'audio/x-flac': '.flac',
486 'audio/flac': '.flac',
490 'audio/x-m4a': '.m4a',
493 EXT_MIMETYPE: null as { [ id: string ]: string }
496 MIMETYPE_EXT: null as { [ id: string ]: string | string[] },
497 MIMETYPES_REGEX: null as string,
498 EXT_MIMETYPE: null as { [ id: string ]: string }
504 'image/webp': '.webp',
508 EXT_MIMETYPE: null as { [ id: string ]: string }
513 'application/x-subrip': '.srt',
519 'application/x-bittorrent': '.torrent'
523 MIMETYPES.AUDIO.EXT_MIMETYPE = invert(MIMETYPES.AUDIO.MIMETYPE_EXT)
524 MIMETYPES.IMAGE.EXT_MIMETYPE = invert(MIMETYPES.IMAGE.MIMETYPE_EXT)
526 const BINARY_CONTENT_TYPES = new Set([
527 'binary/octet-stream',
528 'application/octet-stream',
529 'application/x-binary'
532 // ---------------------------------------------------------------------------
541 // ---------------------------------------------------------------------------
543 const SERVER_ACTOR_NAME = 'peertube'
545 const ACTIVITY_PUB = {
546 POTENTIAL_ACCEPT_HEADERS: [
547 'application/activity+json',
548 'application/ld+json',
549 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
551 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
552 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
553 COLLECTION_ITEMS_PER_PAGE: 10,
554 FETCH_PAGE_LIMIT: 2000,
556 VIDEO: [] as string[],
557 TORRENT: [ 'application/x-bittorrent' ],
558 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
560 MAX_RECURSION_COMMENTS: 100,
561 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
562 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
563 VIDEO_PLAYLIST_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2 // 2 days
566 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
569 APPLICATION: 'Application',
570 ORGANIZATION: 'Organization',
574 const HTTP_SIGNATURE = {
575 HEADER_NAME: 'signature',
576 ALGORITHM: 'rsa-sha256',
577 HEADERS_TO_SIGN: [ '(request-target)', 'host', 'date', 'digest' ],
579 ALL: [ '(request-target)', 'host', 'date' ],
580 POST: [ '(request-target)', 'host', 'date', 'digest' ]
582 CLOCK_SKEW_SECONDS: 1800
585 // ---------------------------------------------------------------------------
587 let PRIVATE_RSA_KEY_SIZE = 2048
589 // Password encryption
590 const BCRYPT_SALT_SIZE = 10
592 const USER_PASSWORD_RESET_LIFETIME = 60000 * 60 // 60 minutes
593 const USER_PASSWORD_CREATE_LIFETIME = 60000 * 60 * 24 * 7 // 7 days
595 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
597 const NSFW_POLICY_TYPES: { [ id: string ]: NSFWPolicyType } = {
598 DO_NOT_LIST: 'do_not_list',
603 // ---------------------------------------------------------------------------
605 // Express static paths (router)
606 const STATIC_PATHS = {
607 THUMBNAILS: '/static/thumbnails/',
608 WEBSEED: '/static/webseed/',
609 REDUNDANCY: '/static/redundancy/',
610 STREAMING_PLAYLISTS: {
611 HLS: '/static/streaming-playlists/hls'
614 const STATIC_DOWNLOAD_PATHS = {
615 TORRENTS: '/download/torrents/',
616 VIDEOS: '/download/videos/',
617 HLS_VIDEOS: '/download/streaming-playlists/hls/videos/'
619 const LAZY_STATIC_PATHS = {
620 BANNERS: '/lazy-static/banners/',
621 AVATARS: '/lazy-static/avatars/',
622 PREVIEWS: '/lazy-static/previews/',
623 VIDEO_CAPTIONS: '/lazy-static/video-captions/',
624 TORRENTS: '/lazy-static/torrents/'
628 const STATIC_MAX_AGE = {
634 // Videos thumbnail size
635 const THUMBNAILS_SIZE = {
640 const PREVIEWS_SIZE = {
645 const ACTOR_IMAGES_SIZE: { [key in ActorImageType]: { width: number, height: number }[]} = {
646 [ActorImageType.AVATAR]: [
656 [ActorImageType.BANNER]: [
659 height: 317 // 6/1 ratio
669 // Sub folders of cache directory
670 const FILES_CACHE = {
672 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
673 MAX_AGE: 1000 * 3600 * 3 // 3 hours
676 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
677 MAX_AGE: 1000 * 3600 * 3 // 3 hours
680 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'torrents'),
681 MAX_AGE: 1000 * 3600 * 3 // 3 hours
689 ACTOR_IMAGE_STATIC: {
694 const RESUMABLE_UPLOAD_DIRECTORY = join(CONFIG.STORAGE.TMP_DIR, 'resumable-uploads')
695 const HLS_STREAMING_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.STREAMING_PLAYLISTS_DIR, 'hls')
696 const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls')
698 const RESUMABLE_UPLOAD_SESSION_LIFETIME = SCHEDULER_INTERVALS_MS.REMOVE_DANGLING_RESUMABLE_UPLOADS
702 CLEANUP_DELAY: 1000 * 60 * 5, // 5 minutes
703 SEGMENT_TIME_SECONDS: {
704 DEFAULT_LATENCY: 4, // 4 seconds
705 SMALL_LATENCY: 2 // 2 seconds
707 SEGMENTS_LIST_SIZE: 15, // 15 maximum segments in live playlist
708 REPLAY_DIRECTORY: 'replay',
709 EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION: 4,
710 MAX_SOCKET_WAITING_DATA: 1024 * 1000 * 100, // 100MB
720 const MEMOIZE_TTL = {
721 OVERVIEWS_SAMPLE: 1000 * 3600 * 4, // 4 hours
722 INFO_HASH_EXISTS: 1000 * 3600 * 12, // 12 hours
723 LIVE_ABLE_TO_UPLOAD: 1000 * 60, // 1 minute
724 LIVE_CHECK_SOCKET_HEALTH: 1000 * 60 // 1 minute
727 const MEMOIZE_LENGTH = {
728 INFO_HASH_EXISTS: 200
731 const QUEUE_CONCURRENCY = {
732 ACTOR_PROCESS_IMAGE: 3
741 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
743 const ASSETS_PATH = {
744 DEFAULT_AUDIO_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-audio-background.jpg'),
745 DEFAULT_LIVE_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-live-background.jpg')
748 // ---------------------------------------------------------------------------
750 const CUSTOM_HTML_TAG_COMMENTS = {
751 TITLE: '<!-- title tag -->',
752 DESCRIPTION: '<!-- description tag -->',
753 CUSTOM_CSS: '<!-- custom css tag -->',
754 META_TAGS: '<!-- meta tags -->',
755 SERVER_CONFIG: '<!-- server config -->'
758 // ---------------------------------------------------------------------------
764 const MAX_LOGS_OUTPUT_CHARACTERS = 10 * 1000 * 1000
765 const LOG_FILENAME = 'peertube.log'
766 const AUDIT_LOG_FILENAME = 'peertube-audit.log'
768 // ---------------------------------------------------------------------------
770 const TRACKER_RATE_LIMITS = {
771 INTERVAL: 60000 * 5, // 5 minutes
772 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
773 ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
774 BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
777 const P2P_MEDIA_LOADER_PEER_VERSION = 2
779 // ---------------------------------------------------------------------------
781 const PLUGIN_GLOBAL_CSS_FILE_NAME = 'plugins-global.css'
782 const PLUGIN_GLOBAL_CSS_PATH = join(CONFIG.STORAGE.TMP_DIR, PLUGIN_GLOBAL_CSS_FILE_NAME)
784 let PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 1000 * 60 * 5 // 5 minutes
786 const DEFAULT_THEME_NAME = 'default'
787 const DEFAULT_USER_THEME_NAME = 'instance-default'
789 // ---------------------------------------------------------------------------
791 const SEARCH_INDEX = {
793 VIDEOS: '/api/v1/search/videos',
794 VIDEO_CHANNELS: '/api/v1/search/video-channels'
798 // ---------------------------------------------------------------------------
800 // Special constants for a test instance
801 if (isTestInstance() === true) {
802 PRIVATE_RSA_KEY_SIZE = 1024
804 ACTOR_FOLLOW_SCORE.BASE = 20
806 REMOTE_SCHEME.HTTP = 'http'
807 REMOTE_SCHEME.WS = 'ws'
809 STATIC_MAX_AGE.SERVER = '0'
811 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
812 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
813 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
814 ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
816 CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max = 100 * 1024 // 100KB
817 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max = 400 * 1024 // 400KB
819 SCHEDULER_INTERVALS_MS.ACTOR_FOLLOW_SCORES = 1000
820 SCHEDULER_INTERVALS_MS.REMOVE_OLD_JOBS = 10000
821 SCHEDULER_INTERVALS_MS.REMOVE_OLD_HISTORY = 5000
822 SCHEDULER_INTERVALS_MS.REMOVE_OLD_VIEWS = 5000
823 SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS = 5000
824 SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES = 5000
825 SCHEDULER_INTERVALS_MS.UPDATE_INBOX_STATS = 5000
826 SCHEDULER_INTERVALS_MS.CHECK_PEERTUBE_VERSION = 2000
828 REPEAT_JOBS['videos-views-stats'] = { every: 5000 }
830 REPEAT_JOBS['activitypub-cleaner'] = { every: 5000 }
831 AP_CLEANER.PERIOD = 5000
833 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
835 VIEW_LIFETIME.VIEWER = 1000 * 5 // 5 second
836 CONTACT_FORM_LIFETIME = 1000 // 1 second
838 JOB_ATTEMPTS['email'] = 1
840 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
841 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 3000
842 MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD = 3000
843 OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD = 2
845 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 5000
847 VIDEO_LIVE.CLEANUP_DELAY = 5000
848 VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY = 2
849 VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY = 1
850 VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION = 1
853 updateWebserverUrls()
854 updateWebserverConfig()
856 registerConfigChangedHandler(() => {
857 updateWebserverUrls()
858 updateWebserverConfig()
861 // ---------------------------------------------------------------------------
863 const FILES_CONTENT_HASH = {
864 MANIFEST: generateContentHash(),
865 FAVICON: generateContentHash(),
866 LOGO: generateContentHash()
869 // ---------------------------------------------------------------------------
871 const VIDEO_FILTERS = {
874 HORIZONTAL_MARGIN_RATIO: 1 / 20,
875 VERTICAL_MARGIN_RATIO: 1 / 20
879 // ---------------------------------------------------------------------------
888 RESUMABLE_UPLOAD_DIRECTORY,
889 RESUMABLE_UPLOAD_SESSION_LIFETIME,
890 HLS_REDUNDANCY_DIRECTORY,
891 P2P_MEDIA_LOADER_PEER_VERSION,
904 LAST_MIGRATION_VERSION,
906 CUSTOM_HTML_TAG_COMMENTS,
907 BROADCAST_CONCURRENCY,
914 DEFAULT_USER_THEME_NAME,
916 PLUGIN_GLOBAL_CSS_FILE_NAME,
917 PLUGIN_GLOBAL_CSS_PATH,
918 PRIVATE_RSA_KEY_SIZE,
920 ROUTE_CACHE_LIFETIME,
922 HLS_STREAMING_PLAYLIST_DIRECTORY,
929 VIDEO_IMPORT_TIMEOUT,
930 VIDEO_PLAYLIST_TYPES,
931 MAX_LOGS_OUTPUT_CHARACTERS,
933 ACTIVITY_PUB_ACTOR_TYPES,
944 VIDEO_TRANSCODING_FPS,
949 USER_PASSWORD_RESET_LIFETIME,
950 USER_PASSWORD_CREATE_LIFETIME,
952 USER_EMAIL_VERIFY_LIFETIME,
954 SCHEDULER_INTERVALS_MS,
956 STATIC_DOWNLOAD_PATHS,
958 CRAWL_REQUEST_CONCURRENCY,
959 DEFAULT_AUDIO_RESOLUTION,
960 BINARY_CONTENT_TYPES,
961 JOB_COMPLETED_LIFETIME,
965 CONTACT_FORM_LIFETIME,
966 VIDEO_PLAYLIST_PRIVACIES,
967 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
975 // ---------------------------------------------------------------------------
977 function buildVideoMimetypeExt () {
979 // streamable formats that warrant cross-browser compatibility
980 'video/webm': '.webm',
981 // We'll add .ogg if additional extensions are enabled
982 // We could add .ogg here but since it could be an audio file,
983 // it would be confusing for users because PeerTube will refuse their file (based on the mimetype)
984 'video/ogg': [ '.ogv' ],
988 if (CONFIG.TRANSCODING.ENABLED) {
989 if (CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
990 data['video/ogg'].push('.ogg')
992 Object.assign(data, {
993 'video/x-matroska': '.mkv',
995 // Developed by Apple
996 'video/quicktime': [ '.mov', '.qt', '.mqv' ], // often used as output format by editing software
997 'video/x-m4v': '.m4v',
1000 // Developed by the Adobe Flash Platform
1001 'video/x-flv': '.flv',
1002 'video/x-f4v': '.f4v', // replacement for flv
1004 // Developed by Microsoft
1005 'video/x-ms-wmv': '.wmv',
1006 'video/x-msvideo': '.avi',
1007 'video/avi': '.avi',
1009 // Developed by 3GPP
1010 // common video formats for cell phones
1011 'video/3gpp': [ '.3gp', '.3gpp' ],
1012 'video/3gpp2': [ '.3g2', '.3gpp2' ],
1014 // Developed by FFmpeg/Mplayer
1015 'application/x-nut': '.nut',
1017 // The standard video format used by many Sony and Panasonic HD camcorders.
1018 // It is also used for storing high definition video on Blu-ray discs.
1019 'video/mp2t': '.mts',
1020 'video/m2ts': '.m2ts',
1022 // Old formats reliant on MPEG-1/MPEG-2
1023 'video/mpv': '.mpv',
1024 'video/mpeg2': '.m2v',
1025 'video/mpeg': [ '.m1v', '.mpg', '.mpe', '.mpeg', '.vob' ],
1026 'video/dvd': '.vob',
1028 // Could be anything
1029 'application/octet-stream': null,
1030 'application/mxf': '.mxf' // often used as exchange format by editing software
1034 if (CONFIG.TRANSCODING.ALLOW_AUDIO_FILES) {
1035 Object.assign(data, MIMETYPES.AUDIO.MIMETYPE_EXT)
1042 function updateWebserverUrls () {
1043 WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
1044 WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
1045 WEBSERVER.WS = CONFIG.WEBSERVER.WS
1047 WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME
1048 WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME
1049 WEBSERVER.PORT = CONFIG.WEBSERVER.PORT
1051 WEBSERVER.RTMP_URL = 'rtmp://' + CONFIG.LIVE.RTMP.HOSTNAME + ':' + CONFIG.LIVE.RTMP.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1052 WEBSERVER.RTMPS_URL = 'rtmps://' + CONFIG.LIVE.RTMPS.HOSTNAME + ':' + CONFIG.LIVE.RTMPS.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1055 function updateWebserverConfig () {
1056 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
1057 MIMETYPES.VIDEO.MIMETYPES_REGEX = buildMimetypesRegex(MIMETYPES.VIDEO.MIMETYPE_EXT)
1059 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
1061 MIMETYPES.VIDEO.EXT_MIMETYPE = buildVideoExtMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT)
1063 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = Object.keys(MIMETYPES.VIDEO.EXT_MIMETYPE)
1066 function buildVideoExtMimetype (obj: { [ id: string ]: string | string[] }) {
1067 const result: { [id: string]: string } = {}
1069 for (const mimetype of Object.keys(obj)) {
1070 const value = obj[mimetype]
1071 if (!value) continue
1073 const extensions = Array.isArray(value) ? value : [ value ]
1075 for (const extension of extensions) {
1076 result[extension] = mimetype
1083 function buildMimetypesRegex (obj: { [id: string]: string | string[] }) {
1084 return Object.keys(obj)
1089 function loadLanguages () {
1090 Object.assign(VIDEO_LANGUAGES, buildLanguages())
1093 function buildLanguages () {
1094 const iso639 = require('iso-639-3')
1096 const languages: { [id: string]: string } = {}
1098 const additionalLanguages = {
1099 sgn: true, // Sign languages (macro language)
1100 ase: true, // American sign language
1101 sdl: true, // Arabian sign language
1102 bfi: true, // British sign language
1103 bzs: true, // Brazilian sign language
1104 csl: true, // Chinese sign language
1105 cse: true, // Czech sign language
1106 dsl: true, // Danish sign language
1107 fsl: true, // French sign language
1108 gsg: true, // German sign language
1109 pks: true, // Pakistan sign language
1110 jsl: true, // Japanese sign language
1111 sfs: true, // South African sign language
1112 swl: true, // Swedish sign language
1113 rsl: true, // Russian sign language
1115 kab: true, // Kabyle
1119 epo: true, // Esperanto
1120 tlh: true, // Klingon
1121 jbo: true, // Lojban
1122 avk: true, // Kotava
1124 zxx: true // No linguistic content (ISO-639-2)
1127 // Only add ISO639-1 languages and some sign languages (ISO639-3)
1130 return (l.iso6391 !== undefined && l.type === 'living') ||
1131 additionalLanguages[l.iso6393] === true
1133 .forEach(l => { languages[l.iso6391 || l.iso6393] = l.name })
1135 // Override Occitan label
1136 languages['oc'] = 'Occitan'
1137 languages['el'] = 'Greek'
1139 // Chinese languages
1140 languages['zh-Hans'] = 'Simplified Chinese'
1141 languages['zh-Hant'] = 'Traditional Chinese'
1146 function generateContentHash () {
1147 return randomBytes(20).toString('hex')