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 = 710
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 'video-studio-edition': 1,
156 'manage-video-torrent': 1,
157 'move-to-object-storage': 3
159 // Excluded keys are jobs that can be configured by admins
160 const JOB_CONCURRENCY: { [id in Exclude<JobType, 'video-transcoding' | 'video-import'>]: number } = {
161 'activitypub-http-broadcast': 1,
162 'activitypub-http-unicast': 10,
163 'activitypub-http-fetcher': 3,
164 'activitypub-cleaner': 1,
165 'activitypub-follow': 1,
166 'video-file-import': 1,
169 'videos-views-stats': 1,
170 'activitypub-refresher': 1,
171 'video-redundancy': 1,
172 'video-live-ending': 10,
173 'video-studio-edition': 1,
174 'manage-video-torrent': 1,
175 'move-to-object-storage': 1
177 const JOB_TTL: { [id in JobType]: number } = {
178 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
179 'activitypub-http-unicast': 60000 * 10, // 10 minutes
180 'activitypub-http-fetcher': 1000 * 3600 * 10, // 10 hours
181 'activitypub-follow': 60000 * 10, // 10 minutes
182 'activitypub-cleaner': 1000 * 3600, // 1 hour
183 'video-file-import': 1000 * 3600, // 1 hour
184 'video-transcoding': 1000 * 3600 * 48, // 2 days, transcoding could be long
185 'video-studio-edition': 1000 * 3600 * 10, // 10 hours
186 'video-import': CONFIG.IMPORT.VIDEOS.TIMEOUT,
187 'email': 60000 * 10, // 10 minutes
188 'actor-keys': 60000 * 20, // 20 minutes
189 'videos-views-stats': undefined, // Unlimited
190 'activitypub-refresher': 60000 * 10, // 10 minutes
191 'video-redundancy': 1000 * 3600 * 3, // 3 hours
192 'video-live-ending': 1000 * 60 * 10, // 10 minutes
193 'manage-video-torrent': 1000 * 3600 * 3, // 3 hours
194 'move-to-object-storage': 1000 * 60 * 60 * 3 // 3 hours
196 const REPEAT_JOBS: { [ id in JobType ]?: EveryRepeatOptions | CronRepeatOptions } = {
197 'videos-views-stats': {
198 cron: randomInt(1, 20) + ' * * * *' // Between 1-20 minutes past the hour
200 'activitypub-cleaner': {
201 cron: '30 5 * * ' + randomInt(0, 7) // 1 time per week (random day) at 5:30 AM
204 const JOB_PRIORITY = {
208 const BROADCAST_CONCURRENCY = 30 // How many requests in parallel we do in activitypub-http-broadcast job
209 const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
212 CONCURRENCY: 10, // How many requests in parallel we do in activitypub-cleaner job
213 UNAVAILABLE_TRESHOLD: 3, // How many attemps we do before removing an unavailable remote resource
214 PERIOD: parseDurationToMs('1 week') // /!\ Has to be sync with REPEAT_JOBS
217 const REQUEST_TIMEOUTS = {
218 DEFAULT: 7000, // 7 seconds
219 FILE: 30000, // 30 seconds
220 REDUNDANCY: JOB_TTL['video-redundancy']
223 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
224 const VIDEO_IMPORT_TIMEOUT = Math.floor(JOB_TTL['video-import'] * 0.9)
226 const SCHEDULER_INTERVALS_MS = {
227 ACTOR_FOLLOW_SCORES: 60000 * 60, // 1 hour
228 REMOVE_OLD_JOBS: 60000 * 60, // 1 hour
229 UPDATE_VIDEOS: 60000, // 1 minute
230 YOUTUBE_DL_UPDATE: 60000 * 60 * 24, // 1 day
231 GEO_IP_UPDATE: 60000 * 60 * 24, // 1 day
232 VIDEO_VIEWS_BUFFER_UPDATE: CONFIG.VIEWS.VIDEOS.LOCAL_BUFFER_UPDATE_INTERVAL,
233 CHECK_PLUGINS: CONFIG.PLUGINS.INDEX.CHECK_LATEST_VERSIONS_INTERVAL,
234 CHECK_PEERTUBE_VERSION: 60000 * 60 * 24, // 1 day
235 AUTO_FOLLOW_INDEX_INSTANCES: 60000 * 60 * 24, // 1 day
236 REMOVE_OLD_VIEWS: 60000 * 60 * 24, // 1 day
237 REMOVE_OLD_HISTORY: 60000 * 60 * 24, // 1 day
238 UPDATE_INBOX_STATS: 1000 * 60, // 1 minute
239 REMOVE_DANGLING_RESUMABLE_UPLOADS: 60000 * 60 // 1 hour
242 // ---------------------------------------------------------------------------
244 const CONSTRAINTS_FIELDS = {
246 NAME: { min: 1, max: 120 }, // Length
247 DESCRIPTION: { min: 3, max: 1000 }, // Length
248 USERNAME: { min: 1, max: 50 }, // Length
249 PASSWORD: { min: 6, max: 255 }, // Length
250 VIDEO_QUOTA: { min: -1 },
251 VIDEO_QUOTA_DAILY: { min: -1 },
252 VIDEO_LANGUAGES: { max: 500 }, // Array length
253 BLOCKED_REASON: { min: 3, max: 250 } // Length
256 REASON: { min: 2, max: 3000 }, // Length
257 MODERATION_COMMENT: { min: 2, max: 3000 } // Length
260 MESSAGE: { min: 2, max: 3000 } // Length
263 REASON: { min: 2, max: 300 } // Length
266 NAME: { min: 1, max: 120 }, // Length
267 DESCRIPTION: { min: 3, max: 1000 }, // Length
268 SUPPORT: { min: 3, max: 1000 }, // Length
269 URL: { min: 3, max: 2000 } // Length
273 EXTNAME: [ '.vtt', '.srt' ],
275 max: 20 * 1024 * 1024 // 20MB
280 URL: { min: 3, max: 2000 }, // Length
281 TORRENT_NAME: { min: 3, max: 255 }, // Length
283 EXTNAME: [ '.torrent' ],
285 max: 1024 * 200 // 200 KB
290 URL: { min: 3, max: 2000 } // Length
293 URL: { min: 3, max: 2000 } // Length
296 NAME: { min: 3, max: 120 }, // Length
297 LANGUAGE: { min: 1, max: 10 }, // Length
298 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
299 DESCRIPTION: { min: 3, max: 10000 }, // Length
300 SUPPORT: { min: 3, max: 1000 }, // Length
302 EXTNAME: [ '.png', '.jpg', '.jpeg', '.webp' ],
304 max: 4 * 1024 * 1024 // 4MB
307 EXTNAME: [] as string[],
308 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
309 DURATION: { min: 0 }, // Number
310 TAGS: { min: 0, max: 5 }, // Number of total tags
311 TAG: { min: 2, max: 30 }, // Length
314 DISLIKES: { min: 0 },
315 FILE_SIZE: { min: -1 },
316 PARTIAL_UPLOAD_SIZE: { max: 50 * 1024 * 1024 * 1024 }, // 50GB
317 URL: { min: 3, max: 2000 } // Length
320 NAME: { min: 1, max: 120 }, // Length
321 DESCRIPTION: { min: 3, max: 1000 }, // Length
322 URL: { min: 3, max: 2000 }, // Length
324 EXTNAME: [ '.jpg', '.jpeg' ],
326 max: 4 * 1024 * 1024 // 4MB
331 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
332 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
333 URL: { min: 3, max: 2000 }, // Length
335 EXTNAME: [ '.png', '.jpeg', '.jpg', '.gif', '.webp' ],
337 max: 4 * 1024 * 1024 // 4MB
345 TEXT: { min: 1, max: 10000 }, // Length
346 URL: { min: 3, max: 2000 } // Length
349 URL: { min: 3, max: 2000 } // Length
352 FROM_NAME: { min: 1, max: 120 }, // Length
353 BODY: { min: 3, max: 5000 } // Length
356 NAME: { min: 1, max: 214 }, // Length
357 DESCRIPTION: { min: 1, max: 20000 } // Length
360 URL: { min: 5, max: 2000 } // Length
363 TASKS: { min: 1, max: 10 }, // Number of tasks
364 CUT_TIME: { min: 0 } // Value
368 const VIEW_LIFETIME = {
369 VIEW: CONFIG.VIEWS.VIDEOS.IP_VIEW_EXPIRATION,
370 VIEWER_COUNTER: 60000 * 1, // 1 minute
371 VIEWER_STATS: 60000 * 60 // 1 hour
374 const MAX_LOCAL_VIEWER_WATCH_SECTIONS = 10
376 let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour
378 const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
380 STANDARD: [ 24, 25, 30 ],
381 HD_STANDARD: [ 50, 60 ],
385 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
388 const DEFAULT_AUDIO_RESOLUTION = VideoResolution.H_480P
390 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
395 const FFMPEG_NICE: { [ id: string ]: number } = {
396 // parent process defaults to niceness = 0
397 // reminder: lower = higher priority, max value is 19, lowest is -20
398 LIVE: 5, // prioritize over VOD and THUMBNAIL
403 const VIDEO_CATEGORIES = {
414 11: 'News & Politics',
418 15: 'Science & Technology',
424 // See https://creativecommons.org/licenses/?lang=en
425 const VIDEO_LICENCES = {
427 2: 'Attribution - Share Alike',
428 3: 'Attribution - No Derivatives',
429 4: 'Attribution - Non Commercial',
430 5: 'Attribution - Non Commercial - Share Alike',
431 6: 'Attribution - Non Commercial - No Derivatives',
432 7: 'Public Domain Dedication'
435 const VIDEO_LANGUAGES: { [id: string]: string } = {}
437 const VIDEO_PRIVACIES: { [ id in VideoPrivacy ]: string } = {
438 [VideoPrivacy.PUBLIC]: 'Public',
439 [VideoPrivacy.UNLISTED]: 'Unlisted',
440 [VideoPrivacy.PRIVATE]: 'Private',
441 [VideoPrivacy.INTERNAL]: 'Internal'
444 const VIDEO_STATES: { [ id in VideoState ]: string } = {
445 [VideoState.PUBLISHED]: 'Published',
446 [VideoState.TO_TRANSCODE]: 'To transcode',
447 [VideoState.TO_IMPORT]: 'To import',
448 [VideoState.WAITING_FOR_LIVE]: 'Waiting for livestream',
449 [VideoState.LIVE_ENDED]: 'Livestream ended',
450 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE]: 'To move to an external storage',
451 [VideoState.TRANSCODING_FAILED]: 'Transcoding failed',
452 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED]: 'External storage move failed',
453 [VideoState.TO_EDIT]: 'To edit*'
456 const VIDEO_IMPORT_STATES: { [ id in VideoImportState ]: string } = {
457 [VideoImportState.FAILED]: 'Failed',
458 [VideoImportState.PENDING]: 'Pending',
459 [VideoImportState.SUCCESS]: 'Success',
460 [VideoImportState.REJECTED]: 'Rejected',
461 [VideoImportState.CANCELLED]: 'Cancelled',
462 [VideoImportState.PROCESSING]: 'Processing'
465 const ABUSE_STATES: { [ id in AbuseState ]: string } = {
466 [AbuseState.PENDING]: 'Pending',
467 [AbuseState.REJECTED]: 'Rejected',
468 [AbuseState.ACCEPTED]: 'Accepted'
471 const VIDEO_PLAYLIST_PRIVACIES: { [ id in VideoPlaylistPrivacy ]: string } = {
472 [VideoPlaylistPrivacy.PUBLIC]: 'Public',
473 [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted',
474 [VideoPlaylistPrivacy.PRIVATE]: 'Private'
477 const VIDEO_PLAYLIST_TYPES: { [ id in VideoPlaylistType ]: string } = {
478 [VideoPlaylistType.REGULAR]: 'Regular',
479 [VideoPlaylistType.WATCH_LATER]: 'Watch later'
485 'audio/mpeg': '.mp3',
488 'application/ogg': '.ogg',
491 'audio/x-ms-wma': '.wma',
493 'audio/x-wav': '.wav',
495 'audio/x-flac': '.flac',
496 'audio/flac': '.flac',
498 'audio/vnd.dlna.adts': '.aac',
503 'audio/x-m4a': '.m4a',
505 'audio/vnd.dolby.dd-raw': '.ac3',
508 EXT_MIMETYPE: null as { [ id: string ]: string }
511 MIMETYPE_EXT: null as { [ id: string ]: string | string[] },
512 MIMETYPES_REGEX: null as string,
513 EXT_MIMETYPE: null as { [ id: string ]: string }
519 'image/webp': '.webp',
523 EXT_MIMETYPE: null as { [ id: string ]: string }
528 'application/x-subrip': '.srt',
534 'application/x-bittorrent': '.torrent'
538 MIMETYPES.AUDIO.EXT_MIMETYPE = invert(MIMETYPES.AUDIO.MIMETYPE_EXT)
539 MIMETYPES.IMAGE.EXT_MIMETYPE = invert(MIMETYPES.IMAGE.MIMETYPE_EXT)
541 const BINARY_CONTENT_TYPES = new Set([
542 'binary/octet-stream',
543 'application/octet-stream',
544 'application/x-binary'
547 // ---------------------------------------------------------------------------
556 // ---------------------------------------------------------------------------
558 const SERVER_ACTOR_NAME = 'peertube'
560 const ACTIVITY_PUB = {
561 POTENTIAL_ACCEPT_HEADERS: [
562 'application/activity+json',
563 'application/ld+json',
564 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
566 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
567 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
568 COLLECTION_ITEMS_PER_PAGE: 10,
569 FETCH_PAGE_LIMIT: 2000,
571 VIDEO: [] as string[],
572 TORRENT: [ 'application/x-bittorrent' ],
573 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
575 MAX_RECURSION_COMMENTS: 100,
576 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
577 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
578 VIDEO_PLAYLIST_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2 // 2 days
581 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
584 APPLICATION: 'Application',
585 ORGANIZATION: 'Organization',
589 const HTTP_SIGNATURE = {
590 HEADER_NAME: 'signature',
591 ALGORITHM: 'rsa-sha256',
592 HEADERS_TO_SIGN: [ '(request-target)', 'host', 'date', 'digest' ],
593 CLOCK_SKEW_SECONDS: 1800
596 // ---------------------------------------------------------------------------
598 let PRIVATE_RSA_KEY_SIZE = 2048
600 // Password encryption
601 const BCRYPT_SALT_SIZE = 10
603 const USER_PASSWORD_RESET_LIFETIME = 60000 * 60 // 60 minutes
604 const USER_PASSWORD_CREATE_LIFETIME = 60000 * 60 * 24 * 7 // 7 days
606 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
608 const NSFW_POLICY_TYPES: { [ id: string ]: NSFWPolicyType } = {
609 DO_NOT_LIST: 'do_not_list',
614 // ---------------------------------------------------------------------------
616 // Express static paths (router)
617 const STATIC_PATHS = {
618 THUMBNAILS: '/static/thumbnails/',
619 WEBSEED: '/static/webseed/',
620 REDUNDANCY: '/static/redundancy/',
621 STREAMING_PLAYLISTS: {
622 HLS: '/static/streaming-playlists/hls'
625 const STATIC_DOWNLOAD_PATHS = {
626 TORRENTS: '/download/torrents/',
627 VIDEOS: '/download/videos/',
628 HLS_VIDEOS: '/download/streaming-playlists/hls/videos/'
630 const LAZY_STATIC_PATHS = {
631 BANNERS: '/lazy-static/banners/',
632 AVATARS: '/lazy-static/avatars/',
633 PREVIEWS: '/lazy-static/previews/',
634 VIDEO_CAPTIONS: '/lazy-static/video-captions/',
635 TORRENTS: '/lazy-static/torrents/'
639 const STATIC_MAX_AGE = {
645 // Videos thumbnail size
646 const THUMBNAILS_SIZE = {
651 const PREVIEWS_SIZE = {
656 const ACTOR_IMAGES_SIZE: { [key in ActorImageType]: { width: number, height: number }[]} = {
657 [ActorImageType.AVATAR]: [
667 [ActorImageType.BANNER]: [
670 height: 317 // 6/1 ratio
680 // Sub folders of cache directory
681 const FILES_CACHE = {
683 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
684 MAX_AGE: 1000 * 3600 * 3 // 3 hours
687 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
688 MAX_AGE: 1000 * 3600 * 3 // 3 hours
691 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'torrents'),
692 MAX_AGE: 1000 * 3600 * 3 // 3 hours
700 ACTOR_IMAGE_STATIC: {
705 const RESUMABLE_UPLOAD_DIRECTORY = join(CONFIG.STORAGE.TMP_DIR, 'resumable-uploads')
706 const HLS_STREAMING_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.STREAMING_PLAYLISTS_DIR, 'hls')
707 const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls')
709 const RESUMABLE_UPLOAD_SESSION_LIFETIME = SCHEDULER_INTERVALS_MS.REMOVE_DANGLING_RESUMABLE_UPLOADS
713 CLEANUP_DELAY: 1000 * 60 * 5, // 5 minutes
714 SEGMENT_TIME_SECONDS: {
715 DEFAULT_LATENCY: 4, // 4 seconds
716 SMALL_LATENCY: 2 // 2 seconds
718 SEGMENTS_LIST_SIZE: 15, // 15 maximum segments in live playlist
719 REPLAY_DIRECTORY: 'replay',
720 EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION: 4,
721 MAX_SOCKET_WAITING_DATA: 1024 * 1000 * 100, // 100MB
731 const MEMOIZE_TTL = {
732 OVERVIEWS_SAMPLE: 1000 * 3600 * 4, // 4 hours
733 INFO_HASH_EXISTS: 1000 * 3600 * 12, // 12 hours
734 LIVE_ABLE_TO_UPLOAD: 1000 * 60, // 1 minute
735 LIVE_CHECK_SOCKET_HEALTH: 1000 * 60 // 1 minute
738 const MEMOIZE_LENGTH = {
739 INFO_HASH_EXISTS: 200
742 const QUEUE_CONCURRENCY = {
743 ACTOR_PROCESS_IMAGE: 3
752 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
754 const ASSETS_PATH = {
755 DEFAULT_AUDIO_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-audio-background.jpg'),
756 DEFAULT_LIVE_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-live-background.jpg')
759 // ---------------------------------------------------------------------------
761 const CUSTOM_HTML_TAG_COMMENTS = {
762 TITLE: '<!-- title tag -->',
763 DESCRIPTION: '<!-- description tag -->',
764 CUSTOM_CSS: '<!-- custom css tag -->',
765 META_TAGS: '<!-- meta tags -->',
766 SERVER_CONFIG: '<!-- server config -->'
769 // ---------------------------------------------------------------------------
775 const MAX_LOGS_OUTPUT_CHARACTERS = 10 * 1000 * 1000
776 const LOG_FILENAME = 'peertube.log'
777 const AUDIT_LOG_FILENAME = 'peertube-audit.log'
779 // ---------------------------------------------------------------------------
781 const TRACKER_RATE_LIMITS = {
782 INTERVAL: 60000 * 5, // 5 minutes
783 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
784 ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
785 BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
788 const P2P_MEDIA_LOADER_PEER_VERSION = 2
790 // ---------------------------------------------------------------------------
792 const PLUGIN_GLOBAL_CSS_FILE_NAME = 'plugins-global.css'
793 const PLUGIN_GLOBAL_CSS_PATH = join(CONFIG.STORAGE.TMP_DIR, PLUGIN_GLOBAL_CSS_FILE_NAME)
795 let PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 1000 * 60 * 5 // 5 minutes
797 const DEFAULT_THEME_NAME = 'default'
798 const DEFAULT_USER_THEME_NAME = 'instance-default'
800 // ---------------------------------------------------------------------------
802 const SEARCH_INDEX = {
804 VIDEOS: '/api/v1/search/videos',
805 VIDEO_CHANNELS: '/api/v1/search/video-channels'
809 // ---------------------------------------------------------------------------
811 const STATS_TIMESERIE = {
812 MAX_DAYS: 365 * 10 // Around 10 years
815 // ---------------------------------------------------------------------------
817 // Special constants for a test instance
818 if (isTestInstance() === true) {
819 PRIVATE_RSA_KEY_SIZE = 1024
821 ACTOR_FOLLOW_SCORE.BASE = 20
823 REMOTE_SCHEME.HTTP = 'http'
824 REMOTE_SCHEME.WS = 'ws'
826 STATIC_MAX_AGE.SERVER = '0'
828 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
829 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
830 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
831 ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
833 CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max = 100 * 1024 // 100KB
834 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max = 400 * 1024 // 400KB
836 SCHEDULER_INTERVALS_MS.ACTOR_FOLLOW_SCORES = 1000
837 SCHEDULER_INTERVALS_MS.REMOVE_OLD_JOBS = 10000
838 SCHEDULER_INTERVALS_MS.REMOVE_OLD_HISTORY = 5000
839 SCHEDULER_INTERVALS_MS.REMOVE_OLD_VIEWS = 5000
840 SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS = 5000
841 SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES = 5000
842 SCHEDULER_INTERVALS_MS.UPDATE_INBOX_STATS = 5000
843 SCHEDULER_INTERVALS_MS.CHECK_PEERTUBE_VERSION = 2000
845 REPEAT_JOBS['videos-views-stats'] = { every: 5000 }
847 REPEAT_JOBS['activitypub-cleaner'] = { every: 5000 }
848 AP_CLEANER.PERIOD = 5000
850 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
852 VIEW_LIFETIME.VIEWER_COUNTER = 1000 * 5 // 5 second
853 VIEW_LIFETIME.VIEWER_STATS = 1000 * 5 // 5 second
854 CONTACT_FORM_LIFETIME = 1000 // 1 second
856 JOB_ATTEMPTS['email'] = 1
858 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
859 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 3000
860 MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD = 3000
861 OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD = 2
863 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 5000
865 VIDEO_LIVE.CLEANUP_DELAY = 5000
866 VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY = 2
867 VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY = 1
868 VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION = 1
871 updateWebserverUrls()
872 updateWebserverConfig()
874 registerConfigChangedHandler(() => {
875 updateWebserverUrls()
876 updateWebserverConfig()
879 // ---------------------------------------------------------------------------
881 const FILES_CONTENT_HASH = {
882 MANIFEST: generateContentHash(),
883 FAVICON: generateContentHash(),
884 LOGO: generateContentHash()
887 // ---------------------------------------------------------------------------
889 const VIDEO_FILTERS = {
892 HORIZONTAL_MARGIN_RATIO: 1 / 20,
893 VERTICAL_MARGIN_RATIO: 1 / 20
897 // ---------------------------------------------------------------------------
906 RESUMABLE_UPLOAD_DIRECTORY,
907 RESUMABLE_UPLOAD_SESSION_LIFETIME,
908 HLS_REDUNDANCY_DIRECTORY,
909 P2P_MEDIA_LOADER_PEER_VERSION,
922 LAST_MIGRATION_VERSION,
924 CUSTOM_HTML_TAG_COMMENTS,
926 BROADCAST_CONCURRENCY,
933 DEFAULT_USER_THEME_NAME,
935 PLUGIN_GLOBAL_CSS_FILE_NAME,
936 PLUGIN_GLOBAL_CSS_PATH,
937 PRIVATE_RSA_KEY_SIZE,
939 ROUTE_CACHE_LIFETIME,
941 HLS_STREAMING_PLAYLIST_DIRECTORY,
948 VIDEO_IMPORT_TIMEOUT,
949 VIDEO_PLAYLIST_TYPES,
950 MAX_LOGS_OUTPUT_CHARACTERS,
952 ACTIVITY_PUB_ACTOR_TYPES,
963 VIDEO_TRANSCODING_FPS,
968 MAX_LOCAL_VIEWER_WATCH_SECTIONS,
969 USER_PASSWORD_RESET_LIFETIME,
970 USER_PASSWORD_CREATE_LIFETIME,
972 USER_EMAIL_VERIFY_LIFETIME,
974 SCHEDULER_INTERVALS_MS,
976 STATIC_DOWNLOAD_PATHS,
978 CRAWL_REQUEST_CONCURRENCY,
979 DEFAULT_AUDIO_RESOLUTION,
980 BINARY_CONTENT_TYPES,
981 JOB_COMPLETED_LIFETIME,
985 CONTACT_FORM_LIFETIME,
986 VIDEO_PLAYLIST_PRIVACIES,
987 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
995 // ---------------------------------------------------------------------------
997 function buildVideoMimetypeExt () {
999 // streamable formats that warrant cross-browser compatibility
1000 'video/webm': '.webm',
1001 // We'll add .ogg if additional extensions are enabled
1002 // We could add .ogg here but since it could be an audio file,
1003 // it would be confusing for users because PeerTube will refuse their file (based on the mimetype)
1004 'video/ogg': [ '.ogv' ],
1008 if (CONFIG.TRANSCODING.ENABLED) {
1009 if (CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
1010 data['video/ogg'].push('.ogg')
1012 Object.assign(data, {
1013 'video/x-matroska': '.mkv',
1015 // Developed by Apple
1016 'video/quicktime': [ '.mov', '.qt', '.mqv' ], // often used as output format by editing software
1017 'video/x-m4v': '.m4v',
1018 'video/m4v': '.m4v',
1020 // Developed by the Adobe Flash Platform
1021 'video/x-flv': '.flv',
1022 'video/x-f4v': '.f4v', // replacement for flv
1024 // Developed by Microsoft
1025 'video/x-ms-wmv': '.wmv',
1026 'video/x-msvideo': '.avi',
1027 'video/avi': '.avi',
1029 // Developed by 3GPP
1030 // common video formats for cell phones
1031 'video/3gpp': [ '.3gp', '.3gpp' ],
1032 'video/3gpp2': [ '.3g2', '.3gpp2' ],
1034 // Developed by FFmpeg/Mplayer
1035 'application/x-nut': '.nut',
1037 // The standard video format used by many Sony and Panasonic HD camcorders.
1038 // It is also used for storing high definition video on Blu-ray discs.
1039 'video/mp2t': '.mts',
1040 'video/vnd.dlna.mpeg-tts': '.mts',
1042 'video/m2ts': '.m2ts',
1044 // Old formats reliant on MPEG-1/MPEG-2
1045 'video/mpv': '.mpv',
1046 'video/mpeg2': '.m2v',
1047 'video/mpeg': [ '.m1v', '.mpg', '.mpe', '.mpeg', '.vob' ],
1048 'video/dvd': '.vob',
1050 // Could be anything
1051 'application/octet-stream': null,
1052 'application/mxf': '.mxf' // often used as exchange format by editing software
1056 if (CONFIG.TRANSCODING.ALLOW_AUDIO_FILES) {
1057 Object.assign(data, MIMETYPES.AUDIO.MIMETYPE_EXT)
1064 function updateWebserverUrls () {
1065 WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
1066 WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
1067 WEBSERVER.WS = CONFIG.WEBSERVER.WS
1069 WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME
1070 WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME
1071 WEBSERVER.PORT = CONFIG.WEBSERVER.PORT
1073 const rtmpHostname = CONFIG.LIVE.RTMP.PUBLIC_HOSTNAME || CONFIG.WEBSERVER.HOSTNAME
1074 const rtmpsHostname = CONFIG.LIVE.RTMPS.PUBLIC_HOSTNAME || CONFIG.WEBSERVER.HOSTNAME
1076 WEBSERVER.RTMP_URL = 'rtmp://' + rtmpHostname + ':' + CONFIG.LIVE.RTMP.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1077 WEBSERVER.RTMPS_URL = 'rtmps://' + rtmpsHostname + ':' + CONFIG.LIVE.RTMPS.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1080 function updateWebserverConfig () {
1081 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
1082 MIMETYPES.VIDEO.MIMETYPES_REGEX = buildMimetypesRegex(MIMETYPES.VIDEO.MIMETYPE_EXT)
1084 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
1086 MIMETYPES.VIDEO.EXT_MIMETYPE = buildVideoExtMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT)
1088 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = Object.keys(MIMETYPES.VIDEO.EXT_MIMETYPE)
1091 function buildVideoExtMimetype (obj: { [ id: string ]: string | string[] }) {
1092 const result: { [id: string]: string } = {}
1094 for (const mimetype of Object.keys(obj)) {
1095 const value = obj[mimetype]
1096 if (!value) continue
1098 const extensions = Array.isArray(value) ? value : [ value ]
1100 for (const extension of extensions) {
1101 result[extension] = mimetype
1108 function buildMimetypesRegex (obj: { [id: string]: string | string[] }) {
1109 return Object.keys(obj)
1114 function loadLanguages () {
1115 Object.assign(VIDEO_LANGUAGES, buildLanguages())
1118 function buildLanguages () {
1119 const iso639 = require('iso-639-3')
1121 const languages: { [id: string]: string } = {}
1123 const additionalLanguages = {
1124 sgn: true, // Sign languages (macro language)
1125 ase: true, // American sign language
1126 sdl: true, // Arabian sign language
1127 bfi: true, // British sign language
1128 bzs: true, // Brazilian sign language
1129 csl: true, // Chinese sign language
1130 cse: true, // Czech sign language
1131 dsl: true, // Danish sign language
1132 fsl: true, // French sign language
1133 gsg: true, // German sign language
1134 pks: true, // Pakistan sign language
1135 jsl: true, // Japanese sign language
1136 sfs: true, // South African sign language
1137 swl: true, // Swedish sign language
1138 rsl: true, // Russian sign language
1140 kab: true, // Kabyle
1144 epo: true, // Esperanto
1145 tlh: true, // Klingon
1146 jbo: true, // Lojban
1147 avk: true, // Kotava
1149 zxx: true // No linguistic content (ISO-639-2)
1152 // Only add ISO639-1 languages and some sign languages (ISO639-3)
1155 return (l.iso6391 !== undefined && l.type === 'living') ||
1156 additionalLanguages[l.iso6393] === true
1158 .forEach(l => { languages[l.iso6391 || l.iso6393] = l.name })
1160 // Override Occitan label
1161 languages['oc'] = 'Occitan'
1162 languages['el'] = 'Greek'
1164 // Chinese languages
1165 languages['zh-Hans'] = 'Simplified Chinese'
1166 languages['zh-Hant'] = 'Traditional Chinese'
1171 function generateContentHash () {
1172 return randomBytes(20).toString('hex')