1 import { CronRepeatOptions, EveryRepeatOptions } from 'bull'
2 import { randomBytes } from 'crypto'
3 import { invert } from 'lodash'
4 import { join } from 'path'
5 import { randomInt } from '../../shared/core-utils/common/miscs'
15 } from '../../shared/models'
16 import { ActivityPubActorType } from '../../shared/models/activitypub'
17 import { FollowState } from '../../shared/models/actors'
18 import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
19 import { VideoPlaylistPrivacy } from '../../shared/models/videos/playlist/video-playlist-privacy.model'
20 import { VideoPlaylistType } from '../../shared/models/videos/playlist/video-playlist-type.model'
21 // Do not use barrels, remain constants as independent as possible
22 import { isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
23 import { CONFIG, registerConfigChangedHandler } from './config'
25 // ---------------------------------------------------------------------------
27 const LAST_MIGRATION_VERSION = 670
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,
155 'move-to-object-storage': 3
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,
167 'videos-views-stats': 1,
168 'activitypub-refresher': 1,
169 'video-redundancy': 1,
170 'video-live-ending': 10,
171 'move-to-object-storage': 1
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
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
194 'activitypub-cleaner': {
195 cron: '30 5 * * ' + randomInt(0, 7) // 1 time per week (random day) at 5:30 AM
198 const JOB_PRIORITY = {
202 const BROADCAST_CONCURRENCY = 30 // How many requests in parallel we do in activitypub-http-broadcast job
203 const AP_CLEANER_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-cleaner job
204 const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
205 const REQUEST_TIMEOUTS = {
206 DEFAULT: 7000, // 7 seconds
207 FILE: 30000, // 30 seconds
208 REDUNDANCY: JOB_TTL['video-redundancy']
211 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
212 const VIDEO_IMPORT_TIMEOUT = 1000 * 3600 // 1 hour
214 const SCHEDULER_INTERVALS_MS = {
215 ACTOR_FOLLOW_SCORES: 60000 * 60, // 1 hour
216 REMOVE_OLD_JOBS: 60000 * 60, // 1 hour
217 UPDATE_VIDEOS: 60000, // 1 minute
218 YOUTUBE_DL_UPDATE: 60000 * 60 * 24, // 1 day
219 VIDEO_VIEWS_BUFFER_UPDATE: CONFIG.VIEWS.VIDEOS.LOCAL_BUFFER_UPDATE_INTERVAL,
220 CHECK_PLUGINS: CONFIG.PLUGINS.INDEX.CHECK_LATEST_VERSIONS_INTERVAL,
221 CHECK_PEERTUBE_VERSION: 60000 * 60 * 24, // 1 day
222 AUTO_FOLLOW_INDEX_INSTANCES: 60000 * 60 * 24, // 1 day
223 REMOVE_OLD_VIEWS: 60000 * 60 * 24, // 1 day
224 REMOVE_OLD_HISTORY: 60000 * 60 * 24, // 1 day
225 UPDATE_INBOX_STATS: 1000 * 60, // 1 minute
226 REMOVE_DANGLING_RESUMABLE_UPLOADS: 60000 * 60 * 16 // 16 hours
229 // ---------------------------------------------------------------------------
231 const CONSTRAINTS_FIELDS = {
233 NAME: { min: 1, max: 120 }, // Length
234 DESCRIPTION: { min: 3, max: 1000 }, // Length
235 USERNAME: { min: 1, max: 50 }, // Length
236 PASSWORD: { min: 6, max: 255 }, // Length
237 VIDEO_QUOTA: { min: -1 },
238 VIDEO_QUOTA_DAILY: { min: -1 },
239 VIDEO_LANGUAGES: { max: 500 }, // Array length
240 BLOCKED_REASON: { min: 3, max: 250 } // Length
243 REASON: { min: 2, max: 3000 }, // Length
244 MODERATION_COMMENT: { min: 2, max: 3000 } // Length
247 MESSAGE: { min: 2, max: 3000 } // Length
250 REASON: { min: 2, max: 300 } // Length
253 NAME: { min: 1, max: 120 }, // Length
254 DESCRIPTION: { min: 3, max: 1000 }, // Length
255 SUPPORT: { min: 3, max: 1000 }, // Length
256 URL: { min: 3, max: 2000 } // Length
260 EXTNAME: [ '.vtt', '.srt' ],
262 max: 4 * 1024 * 1024 // 4MB
267 URL: { min: 3, max: 2000 }, // Length
268 TORRENT_NAME: { min: 3, max: 255 }, // Length
270 EXTNAME: [ '.torrent' ],
272 max: 1024 * 200 // 200 KB
277 URL: { min: 3, max: 2000 } // Length
280 URL: { min: 3, max: 2000 } // Length
283 NAME: { min: 3, max: 120 }, // Length
284 LANGUAGE: { min: 1, max: 10 }, // Length
285 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
286 DESCRIPTION: { min: 3, max: 10000 }, // Length
287 SUPPORT: { min: 3, max: 1000 }, // Length
289 EXTNAME: [ '.png', '.jpg', '.jpeg', '.webp' ],
291 max: 4 * 1024 * 1024 // 4MB
294 EXTNAME: [] as string[],
295 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
296 DURATION: { min: 0 }, // Number
297 TAGS: { min: 0, max: 5 }, // Number of total tags
298 TAG: { min: 2, max: 30 }, // Length
301 DISLIKES: { min: 0 },
302 FILE_SIZE: { min: -1 },
303 PARTIAL_UPLOAD_SIZE: { max: 50 * 1024 * 1024 * 1024 }, // 50GB
304 URL: { min: 3, max: 2000 } // Length
307 NAME: { min: 1, max: 120 }, // Length
308 DESCRIPTION: { min: 3, max: 1000 }, // Length
309 URL: { min: 3, max: 2000 }, // Length
311 EXTNAME: [ '.jpg', '.jpeg' ],
313 max: 4 * 1024 * 1024 // 4MB
318 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
319 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
320 URL: { min: 3, max: 2000 }, // Length
322 EXTNAME: [ '.png', '.jpeg', '.jpg', '.gif', '.webp' ],
324 max: 4 * 1024 * 1024 // 4MB
332 TEXT: { min: 1, max: 10000 }, // Length
333 URL: { min: 3, max: 2000 } // Length
336 URL: { min: 3, max: 2000 } // Length
339 FROM_NAME: { min: 1, max: 120 }, // Length
340 BODY: { min: 3, max: 5000 } // Length
343 NAME: { min: 1, max: 214 }, // Length
344 DESCRIPTION: { min: 1, max: 20000 } // Length
347 URL: { min: 5, max: 2000 } // Length
351 const VIEW_LIFETIME = {
352 VIEW: CONFIG.VIEWS.VIDEOS.IP_VIEW_EXPIRATION,
353 VIEWER: 60000 * 5 // 5 minutes
356 let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour
358 const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
360 STANDARD: [ 24, 25, 30 ],
361 HD_STANDARD: [ 50, 60 ],
364 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
367 const DEFAULT_AUDIO_RESOLUTION = VideoResolution.H_480P
369 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
374 const FFMPEG_NICE: { [ id: string ]: number } = {
375 // parent process defaults to niceness = 0
376 // reminder: lower = higher priority, max value is 19, lowest is -20
377 LIVE: 5, // prioritize over VOD and THUMBNAIL
382 const VIDEO_CATEGORIES = {
393 11: 'News & Politics',
397 15: 'Science & Technology',
403 // See https://creativecommons.org/licenses/?lang=en
404 const VIDEO_LICENCES = {
406 2: 'Attribution - Share Alike',
407 3: 'Attribution - No Derivatives',
408 4: 'Attribution - Non Commercial',
409 5: 'Attribution - Non Commercial - Share Alike',
410 6: 'Attribution - Non Commercial - No Derivatives',
411 7: 'Public Domain Dedication'
414 const VIDEO_LANGUAGES: { [id: string]: string } = {}
416 const VIDEO_PRIVACIES: { [ id in VideoPrivacy ]: string } = {
417 [VideoPrivacy.PUBLIC]: 'Public',
418 [VideoPrivacy.UNLISTED]: 'Unlisted',
419 [VideoPrivacy.PRIVATE]: 'Private',
420 [VideoPrivacy.INTERNAL]: 'Internal'
423 const VIDEO_STATES: { [ id in VideoState ]: string } = {
424 [VideoState.PUBLISHED]: 'Published',
425 [VideoState.TO_TRANSCODE]: 'To transcode',
426 [VideoState.TO_IMPORT]: 'To import',
427 [VideoState.WAITING_FOR_LIVE]: 'Waiting for livestream',
428 [VideoState.LIVE_ENDED]: 'Livestream ended',
429 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE]: 'To move to an external storage',
430 [VideoState.TRANSCODING_FAILED]: 'Transcoding failed'
433 const VIDEO_IMPORT_STATES: { [ id in VideoImportState ]: string } = {
434 [VideoImportState.FAILED]: 'Failed',
435 [VideoImportState.PENDING]: 'Pending',
436 [VideoImportState.SUCCESS]: 'Success',
437 [VideoImportState.REJECTED]: 'Rejected'
440 const ABUSE_STATES: { [ id in AbuseState ]: string } = {
441 [AbuseState.PENDING]: 'Pending',
442 [AbuseState.REJECTED]: 'Rejected',
443 [AbuseState.ACCEPTED]: 'Accepted'
446 const VIDEO_PLAYLIST_PRIVACIES: { [ id in VideoPlaylistPrivacy ]: string } = {
447 [VideoPlaylistPrivacy.PUBLIC]: 'Public',
448 [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted',
449 [VideoPlaylistPrivacy.PRIVATE]: 'Private'
452 const VIDEO_PLAYLIST_TYPES: { [ id in VideoPlaylistType ]: string } = {
453 [VideoPlaylistType.REGULAR]: 'Regular',
454 [VideoPlaylistType.WATCH_LATER]: 'Watch later'
460 'audio/mpeg': '.mp3',
462 'application/ogg': '.ogg',
464 'audio/x-ms-wma': '.wma',
466 'audio/x-wav': '.wav',
467 'audio/x-flac': '.flac',
468 'audio/flac': '.flac',
472 'audio/x-m4a': '.m4a',
475 EXT_MIMETYPE: null as { [ id: string ]: string }
478 MIMETYPE_EXT: null as { [ id: string ]: string | string[] },
479 MIMETYPES_REGEX: null as string,
480 EXT_MIMETYPE: null as { [ id: string ]: string }
486 'image/webp': '.webp',
490 EXT_MIMETYPE: null as { [ id: string ]: string }
495 'application/x-subrip': '.srt',
501 'application/x-bittorrent': '.torrent'
505 MIMETYPES.AUDIO.EXT_MIMETYPE = invert(MIMETYPES.AUDIO.MIMETYPE_EXT)
506 MIMETYPES.IMAGE.EXT_MIMETYPE = invert(MIMETYPES.IMAGE.MIMETYPE_EXT)
508 const BINARY_CONTENT_TYPES = new Set([
509 'binary/octet-stream',
510 'application/octet-stream',
511 'application/x-binary'
514 // ---------------------------------------------------------------------------
523 // ---------------------------------------------------------------------------
525 const SERVER_ACTOR_NAME = 'peertube'
527 const ACTIVITY_PUB = {
528 POTENTIAL_ACCEPT_HEADERS: [
529 'application/activity+json',
530 'application/ld+json',
531 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
533 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
534 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
535 COLLECTION_ITEMS_PER_PAGE: 10,
536 FETCH_PAGE_LIMIT: 2000,
538 VIDEO: [] as string[],
539 TORRENT: [ 'application/x-bittorrent' ],
540 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
542 MAX_RECURSION_COMMENTS: 100,
543 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
544 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
545 VIDEO_PLAYLIST_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2 // 2 days
548 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
551 APPLICATION: 'Application',
552 ORGANIZATION: 'Organization',
556 const HTTP_SIGNATURE = {
557 HEADER_NAME: 'signature',
558 ALGORITHM: 'rsa-sha256',
559 HEADERS_TO_SIGN: [ '(request-target)', 'host', 'date', 'digest' ],
561 ALL: [ '(request-target)', 'host', 'date' ],
562 POST: [ '(request-target)', 'host', 'date', 'digest' ]
564 CLOCK_SKEW_SECONDS: 1800
567 // ---------------------------------------------------------------------------
569 let PRIVATE_RSA_KEY_SIZE = 2048
571 // Password encryption
572 const BCRYPT_SALT_SIZE = 10
574 const USER_PASSWORD_RESET_LIFETIME = 60000 * 60 // 60 minutes
575 const USER_PASSWORD_CREATE_LIFETIME = 60000 * 60 * 24 * 7 // 7 days
577 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
579 const NSFW_POLICY_TYPES: { [ id: string ]: NSFWPolicyType } = {
580 DO_NOT_LIST: 'do_not_list',
585 // ---------------------------------------------------------------------------
587 // Express static paths (router)
588 const STATIC_PATHS = {
589 THUMBNAILS: '/static/thumbnails/',
590 WEBSEED: '/static/webseed/',
591 REDUNDANCY: '/static/redundancy/',
592 STREAMING_PLAYLISTS: {
593 HLS: '/static/streaming-playlists/hls'
596 const STATIC_DOWNLOAD_PATHS = {
597 TORRENTS: '/download/torrents/',
598 VIDEOS: '/download/videos/',
599 HLS_VIDEOS: '/download/streaming-playlists/hls/videos/'
601 const LAZY_STATIC_PATHS = {
602 BANNERS: '/lazy-static/banners/',
603 AVATARS: '/lazy-static/avatars/',
604 PREVIEWS: '/lazy-static/previews/',
605 VIDEO_CAPTIONS: '/lazy-static/video-captions/',
606 TORRENTS: '/lazy-static/torrents/'
610 const STATIC_MAX_AGE = {
616 // Videos thumbnail size
617 const THUMBNAILS_SIZE = {
622 const PREVIEWS_SIZE = {
627 const ACTOR_IMAGES_SIZE = {
634 height: 317 // 6/1 ratio
643 // Sub folders of cache directory
644 const FILES_CACHE = {
646 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
647 MAX_AGE: 1000 * 3600 * 3 // 3 hours
650 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
651 MAX_AGE: 1000 * 3600 * 3 // 3 hours
654 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'torrents'),
655 MAX_AGE: 1000 * 3600 * 3 // 3 hours
663 ACTOR_IMAGE_STATIC: {
668 const RESUMABLE_UPLOAD_DIRECTORY = join(CONFIG.STORAGE.TMP_DIR, 'resumable-uploads')
669 const HLS_STREAMING_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.STREAMING_PLAYLISTS_DIR, 'hls')
670 const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls')
672 const RESUMABLE_UPLOAD_SESSION_LIFETIME = SCHEDULER_INTERVALS_MS.REMOVE_DANGLING_RESUMABLE_UPLOADS
676 CLEANUP_DELAY: 1000 * 60 * 5, // 5 minutes
677 SEGMENT_TIME_SECONDS: 4, // 4 seconds
678 SEGMENTS_LIST_SIZE: 15, // 15 maximum segments in live playlist
679 REPLAY_DIRECTORY: 'replay',
680 EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION: 4,
681 MAX_SOCKET_WAITING_DATA: 1024 * 1000 * 100, // 100MB
691 const MEMOIZE_TTL = {
692 OVERVIEWS_SAMPLE: 1000 * 3600 * 4, // 4 hours
693 INFO_HASH_EXISTS: 1000 * 3600 * 12, // 12 hours
694 LIVE_ABLE_TO_UPLOAD: 1000 * 60, // 1 minute
695 LIVE_CHECK_SOCKET_HEALTH: 1000 * 60 // 1 minute
698 const MEMOIZE_LENGTH = {
699 INFO_HASH_EXISTS: 200
702 const QUEUE_CONCURRENCY = {
703 ACTOR_PROCESS_IMAGE: 3
712 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
714 const ASSETS_PATH = {
715 DEFAULT_AUDIO_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-audio-background.jpg'),
716 DEFAULT_LIVE_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-live-background.jpg')
719 // ---------------------------------------------------------------------------
721 const CUSTOM_HTML_TAG_COMMENTS = {
722 TITLE: '<!-- title tag -->',
723 DESCRIPTION: '<!-- description tag -->',
724 CUSTOM_CSS: '<!-- custom css tag -->',
725 META_TAGS: '<!-- meta tags -->',
726 SERVER_CONFIG: '<!-- server config -->'
729 // ---------------------------------------------------------------------------
735 const MAX_LOGS_OUTPUT_CHARACTERS = 10 * 1000 * 1000
736 const LOG_FILENAME = 'peertube.log'
737 const AUDIT_LOG_FILENAME = 'peertube-audit.log'
739 // ---------------------------------------------------------------------------
741 const TRACKER_RATE_LIMITS = {
742 INTERVAL: 60000 * 5, // 5 minutes
743 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
744 ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
745 BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
748 const P2P_MEDIA_LOADER_PEER_VERSION = 2
750 // ---------------------------------------------------------------------------
752 const PLUGIN_GLOBAL_CSS_FILE_NAME = 'plugins-global.css'
753 const PLUGIN_GLOBAL_CSS_PATH = join(CONFIG.STORAGE.TMP_DIR, PLUGIN_GLOBAL_CSS_FILE_NAME)
755 let PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 1000 * 60 * 5 // 5 minutes
757 const DEFAULT_THEME_NAME = 'default'
758 const DEFAULT_USER_THEME_NAME = 'instance-default'
760 // ---------------------------------------------------------------------------
762 const SEARCH_INDEX = {
764 VIDEOS: '/api/v1/search/videos',
765 VIDEO_CHANNELS: '/api/v1/search/video-channels'
769 // ---------------------------------------------------------------------------
771 // Special constants for a test instance
772 if (isTestInstance() === true) {
773 PRIVATE_RSA_KEY_SIZE = 1024
775 ACTOR_FOLLOW_SCORE.BASE = 20
777 REMOTE_SCHEME.HTTP = 'http'
778 REMOTE_SCHEME.WS = 'ws'
780 STATIC_MAX_AGE.SERVER = '0'
782 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
783 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
784 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
785 ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
787 CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max = 100 * 1024 // 100KB
788 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max = 400 * 1024 // 400KB
790 SCHEDULER_INTERVALS_MS.ACTOR_FOLLOW_SCORES = 1000
791 SCHEDULER_INTERVALS_MS.REMOVE_OLD_JOBS = 10000
792 SCHEDULER_INTERVALS_MS.REMOVE_OLD_HISTORY = 5000
793 SCHEDULER_INTERVALS_MS.REMOVE_OLD_VIEWS = 5000
794 SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS = 5000
795 SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES = 5000
796 SCHEDULER_INTERVALS_MS.UPDATE_INBOX_STATS = 5000
797 SCHEDULER_INTERVALS_MS.CHECK_PEERTUBE_VERSION = 2000
798 REPEAT_JOBS['videos-views-stats'] = { every: 5000 }
799 REPEAT_JOBS['activitypub-cleaner'] = { every: 5000 }
801 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
803 VIEW_LIFETIME.VIEWER = 1000 * 5 // 5 second
804 CONTACT_FORM_LIFETIME = 1000 // 1 second
806 JOB_ATTEMPTS['email'] = 1
808 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
809 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 3000
810 MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD = 3000
811 OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD = 2
813 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 5000
815 VIDEO_LIVE.CLEANUP_DELAY = 5000
816 VIDEO_LIVE.SEGMENT_TIME_SECONDS = 2
817 VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION = 1
820 updateWebserverUrls()
821 updateWebserverConfig()
823 registerConfigChangedHandler(() => {
824 updateWebserverUrls()
825 updateWebserverConfig()
828 // ---------------------------------------------------------------------------
830 const FILES_CONTENT_HASH = {
831 MANIFEST: generateContentHash(),
832 FAVICON: generateContentHash(),
833 LOGO: generateContentHash()
836 // ---------------------------------------------------------------------------
845 RESUMABLE_UPLOAD_DIRECTORY,
846 RESUMABLE_UPLOAD_SESSION_LIFETIME,
847 HLS_REDUNDANCY_DIRECTORY,
848 P2P_MEDIA_LOADER_PEER_VERSION,
860 AP_CLEANER_CONCURRENCY,
861 LAST_MIGRATION_VERSION,
863 CUSTOM_HTML_TAG_COMMENTS,
864 BROADCAST_CONCURRENCY,
871 DEFAULT_USER_THEME_NAME,
873 PLUGIN_GLOBAL_CSS_FILE_NAME,
874 PLUGIN_GLOBAL_CSS_PATH,
875 PRIVATE_RSA_KEY_SIZE,
876 ROUTE_CACHE_LIFETIME,
878 HLS_STREAMING_PLAYLIST_DIRECTORY,
885 VIDEO_IMPORT_TIMEOUT,
886 VIDEO_PLAYLIST_TYPES,
887 MAX_LOGS_OUTPUT_CHARACTERS,
889 ACTIVITY_PUB_ACTOR_TYPES,
900 VIDEO_TRANSCODING_FPS,
905 USER_PASSWORD_RESET_LIFETIME,
906 USER_PASSWORD_CREATE_LIFETIME,
908 USER_EMAIL_VERIFY_LIFETIME,
910 SCHEDULER_INTERVALS_MS,
912 STATIC_DOWNLOAD_PATHS,
914 CRAWL_REQUEST_CONCURRENCY,
915 DEFAULT_AUDIO_RESOLUTION,
916 BINARY_CONTENT_TYPES,
917 JOB_COMPLETED_LIFETIME,
921 CONTACT_FORM_LIFETIME,
922 VIDEO_PLAYLIST_PRIVACIES,
923 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
931 // ---------------------------------------------------------------------------
933 function buildVideoMimetypeExt () {
935 // streamable formats that warrant cross-browser compatibility
936 'video/webm': '.webm',
937 // We'll add .ogg if additional extensions are enabled
938 // We could add .ogg here but since it could be an audio file,
939 // it would be confusing for users because PeerTube will refuse their file (based on the mimetype)
940 'video/ogg': [ '.ogv' ],
944 if (CONFIG.TRANSCODING.ENABLED) {
945 if (CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
946 data['video/ogg'].push('.ogg')
948 Object.assign(data, {
949 'video/x-matroska': '.mkv',
951 // Developed by Apple
952 'video/quicktime': [ '.mov', '.qt', '.mqv' ], // often used as output format by editing software
953 'video/x-m4v': '.m4v',
956 // Developed by the Adobe Flash Platform
957 'video/x-flv': '.flv',
958 'video/x-f4v': '.f4v', // replacement for flv
960 // Developed by Microsoft
961 'video/x-ms-wmv': '.wmv',
962 'video/x-msvideo': '.avi',
966 // common video formats for cell phones
967 'video/3gpp': [ '.3gp', '.3gpp' ],
968 'video/3gpp2': [ '.3g2', '.3gpp2' ],
970 // Developed by FFmpeg/Mplayer
971 'application/x-nut': '.nut',
973 // The standard video format used by many Sony and Panasonic HD camcorders.
974 // It is also used for storing high definition video on Blu-ray discs.
975 'video/mp2t': '.mts',
976 'video/m2ts': '.m2ts',
978 // Old formats reliant on MPEG-1/MPEG-2
980 'video/mpeg2': '.m2v',
981 'video/mpeg': [ '.m1v', '.mpg', '.mpe', '.mpeg', '.vob' ],
985 'application/octet-stream': null,
986 'application/mxf': '.mxf' // often used as exchange format by editing software
990 if (CONFIG.TRANSCODING.ALLOW_AUDIO_FILES) {
991 Object.assign(data, MIMETYPES.AUDIO.MIMETYPE_EXT)
998 function updateWebserverUrls () {
999 WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
1000 WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
1001 WEBSERVER.WS = CONFIG.WEBSERVER.WS
1003 WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME
1004 WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME
1005 WEBSERVER.PORT = CONFIG.WEBSERVER.PORT
1007 WEBSERVER.RTMP_URL = 'rtmp://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.LIVE.RTMP.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1008 WEBSERVER.RTMPS_URL = 'rtmps://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.LIVE.RTMPS.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1011 function updateWebserverConfig () {
1012 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
1013 MIMETYPES.VIDEO.MIMETYPES_REGEX = buildMimetypesRegex(MIMETYPES.VIDEO.MIMETYPE_EXT)
1015 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
1017 MIMETYPES.VIDEO.EXT_MIMETYPE = buildVideoExtMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT)
1019 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = Object.keys(MIMETYPES.VIDEO.EXT_MIMETYPE)
1022 function buildVideoExtMimetype (obj: { [ id: string ]: string | string[] }) {
1023 const result: { [id: string]: string } = {}
1025 for (const mimetype of Object.keys(obj)) {
1026 const value = obj[mimetype]
1027 if (!value) continue
1029 const extensions = Array.isArray(value) ? value : [ value ]
1031 for (const extension of extensions) {
1032 result[extension] = mimetype
1039 function buildMimetypesRegex (obj: { [id: string]: string | string[] }) {
1040 return Object.keys(obj)
1045 function loadLanguages () {
1046 Object.assign(VIDEO_LANGUAGES, buildLanguages())
1049 function buildLanguages () {
1050 const iso639 = require('iso-639-3')
1052 const languages: { [id: string]: string } = {}
1054 const additionalLanguages = {
1055 sgn: true, // Sign languages (macro language)
1056 ase: true, // American sign language
1057 sdl: true, // Arabian sign language
1058 bfi: true, // British sign language
1059 bzs: true, // Brazilian sign language
1060 csl: true, // Chinese sign language
1061 cse: true, // Czech sign language
1062 dsl: true, // Danish sign language
1063 fsl: true, // French sign language
1064 gsg: true, // German sign language
1065 pks: true, // Pakistan sign language
1066 jsl: true, // Japanese sign language
1067 sfs: true, // South African sign language
1068 swl: true, // Swedish sign language
1069 rsl: true, // Russian sign language
1071 kab: true, // Kabyle
1075 epo: true, // Esperanto
1076 tlh: true, // Klingon
1077 jbo: true, // Lojban
1081 // Only add ISO639-1 languages and some sign languages (ISO639-3)
1084 return (l.iso6391 !== undefined && l.type === 'living') ||
1085 additionalLanguages[l.iso6393] === true
1087 .forEach(l => { languages[l.iso6391 || l.iso6393] = l.name })
1089 // Override Occitan label
1090 languages['oc'] = 'Occitan'
1091 languages['el'] = 'Greek'
1096 function generateContentHash () {
1097 return randomBytes(20).toString('hex')