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-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': 1000 * 3600 * 2, // 2 hours
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 attemps 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 LIVE_ABLE_TO_UPLOAD: 1000 * 60, // 1 minute
738 LIVE_CHECK_SOCKET_HEALTH: 1000 * 60 // 1 minute
741 const MEMOIZE_LENGTH = {
742 INFO_HASH_EXISTS: 200
745 const QUEUE_CONCURRENCY = {
746 ACTOR_PROCESS_IMAGE: 3
755 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
757 const ASSETS_PATH = {
758 DEFAULT_AUDIO_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-audio-background.jpg'),
759 DEFAULT_LIVE_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-live-background.jpg')
762 // ---------------------------------------------------------------------------
764 const CUSTOM_HTML_TAG_COMMENTS = {
765 TITLE: '<!-- title tag -->',
766 DESCRIPTION: '<!-- description tag -->',
767 CUSTOM_CSS: '<!-- custom css tag -->',
768 META_TAGS: '<!-- meta tags -->',
769 SERVER_CONFIG: '<!-- server config -->'
772 // ---------------------------------------------------------------------------
778 const MAX_LOGS_OUTPUT_CHARACTERS = 10 * 1000 * 1000
779 const LOG_FILENAME = 'peertube.log'
780 const AUDIT_LOG_FILENAME = 'peertube-audit.log'
782 // ---------------------------------------------------------------------------
784 const TRACKER_RATE_LIMITS = {
785 INTERVAL: 60000 * 5, // 5 minutes
786 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
787 ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
788 BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
791 const P2P_MEDIA_LOADER_PEER_VERSION = 2
793 // ---------------------------------------------------------------------------
795 const PLUGIN_GLOBAL_CSS_FILE_NAME = 'plugins-global.css'
796 const PLUGIN_GLOBAL_CSS_PATH = join(CONFIG.STORAGE.TMP_DIR, PLUGIN_GLOBAL_CSS_FILE_NAME)
798 let PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 1000 * 60 * 5 // 5 minutes
800 const DEFAULT_THEME_NAME = 'default'
801 const DEFAULT_USER_THEME_NAME = 'instance-default'
803 // ---------------------------------------------------------------------------
805 const SEARCH_INDEX = {
807 VIDEOS: '/api/v1/search/videos',
808 VIDEO_CHANNELS: '/api/v1/search/video-channels'
812 // ---------------------------------------------------------------------------
814 const STATS_TIMESERIE = {
815 MAX_DAYS: 365 * 10 // Around 10 years
818 // ---------------------------------------------------------------------------
820 // Special constants for a test instance
821 if (isTestInstance() === true) {
822 PRIVATE_RSA_KEY_SIZE = 1024
824 ACTOR_FOLLOW_SCORE.BASE = 20
826 REMOTE_SCHEME.HTTP = 'http'
827 REMOTE_SCHEME.WS = 'ws'
829 STATIC_MAX_AGE.SERVER = '0'
831 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
832 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
833 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
834 ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
836 CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max = 100 * 1024 // 100KB
837 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max = 400 * 1024 // 400KB
839 SCHEDULER_INTERVALS_MS.ACTOR_FOLLOW_SCORES = 1000
840 SCHEDULER_INTERVALS_MS.REMOVE_OLD_JOBS = 10000
841 SCHEDULER_INTERVALS_MS.REMOVE_OLD_HISTORY = 5000
842 SCHEDULER_INTERVALS_MS.REMOVE_OLD_VIEWS = 5000
843 SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS = 5000
844 SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES = 5000
845 SCHEDULER_INTERVALS_MS.UPDATE_INBOX_STATS = 5000
846 SCHEDULER_INTERVALS_MS.CHECK_PEERTUBE_VERSION = 2000
848 REPEAT_JOBS['videos-views-stats'] = { every: 5000 }
850 REPEAT_JOBS['activitypub-cleaner'] = { every: 5000 }
851 AP_CLEANER.PERIOD = 5000
853 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
855 VIEW_LIFETIME.VIEWER_COUNTER = 1000 * 5 // 5 second
856 VIEW_LIFETIME.VIEWER_STATS = 1000 * 5 // 5 second
857 CONTACT_FORM_LIFETIME = 1000 // 1 second
859 JOB_ATTEMPTS['email'] = 1
861 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
862 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 3000
863 MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD = 3000
864 OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD = 2
866 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 5000
868 VIDEO_LIVE.CLEANUP_DELAY = 5000
869 VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY = 2
870 VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY = 1
871 VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION = 1
874 updateWebserverUrls()
875 updateWebserverConfig()
877 registerConfigChangedHandler(() => {
878 updateWebserverUrls()
879 updateWebserverConfig()
882 // ---------------------------------------------------------------------------
884 const FILES_CONTENT_HASH = {
885 MANIFEST: generateContentHash(),
886 FAVICON: generateContentHash(),
887 LOGO: generateContentHash()
890 // ---------------------------------------------------------------------------
892 const VIDEO_FILTERS = {
895 HORIZONTAL_MARGIN_RATIO: 1 / 20,
896 VERTICAL_MARGIN_RATIO: 1 / 20
900 // ---------------------------------------------------------------------------
909 RESUMABLE_UPLOAD_DIRECTORY,
910 RESUMABLE_UPLOAD_SESSION_LIFETIME,
911 HLS_REDUNDANCY_DIRECTORY,
912 P2P_MEDIA_LOADER_PEER_VERSION,
925 LAST_MIGRATION_VERSION,
927 CUSTOM_HTML_TAG_COMMENTS,
929 BROADCAST_CONCURRENCY,
936 DEFAULT_USER_THEME_NAME,
938 PLUGIN_GLOBAL_CSS_FILE_NAME,
939 PLUGIN_GLOBAL_CSS_PATH,
940 PRIVATE_RSA_KEY_SIZE,
942 ROUTE_CACHE_LIFETIME,
944 HLS_STREAMING_PLAYLIST_DIRECTORY,
951 VIDEO_IMPORT_TIMEOUT,
952 VIDEO_PLAYLIST_TYPES,
953 MAX_LOGS_OUTPUT_CHARACTERS,
955 ACTIVITY_PUB_ACTOR_TYPES,
966 VIDEO_TRANSCODING_FPS,
971 MAX_LOCAL_VIEWER_WATCH_SECTIONS,
972 USER_PASSWORD_RESET_LIFETIME,
973 USER_PASSWORD_CREATE_LIFETIME,
975 USER_EMAIL_VERIFY_LIFETIME,
977 SCHEDULER_INTERVALS_MS,
979 STATIC_DOWNLOAD_PATHS,
981 CRAWL_REQUEST_CONCURRENCY,
982 DEFAULT_AUDIO_RESOLUTION,
983 BINARY_CONTENT_TYPES,
984 JOB_COMPLETED_LIFETIME,
988 CONTACT_FORM_LIFETIME,
989 VIDEO_PLAYLIST_PRIVACIES,
990 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
998 // ---------------------------------------------------------------------------
1000 function buildVideoMimetypeExt () {
1002 // streamable formats that warrant cross-browser compatibility
1003 'video/webm': '.webm',
1004 // We'll add .ogg if additional extensions are enabled
1005 // We could add .ogg here but since it could be an audio file,
1006 // it would be confusing for users because PeerTube will refuse their file (based on the mimetype)
1007 'video/ogg': [ '.ogv' ],
1011 if (CONFIG.TRANSCODING.ENABLED) {
1012 if (CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
1013 data['video/ogg'].push('.ogg')
1015 Object.assign(data, {
1016 'video/x-matroska': '.mkv',
1018 // Developed by Apple
1019 'video/quicktime': [ '.mov', '.qt', '.mqv' ], // often used as output format by editing software
1020 'video/x-m4v': '.m4v',
1021 'video/m4v': '.m4v',
1023 // Developed by the Adobe Flash Platform
1024 'video/x-flv': '.flv',
1025 'video/x-f4v': '.f4v', // replacement for flv
1027 // Developed by Microsoft
1028 'video/x-ms-wmv': '.wmv',
1029 'video/x-msvideo': '.avi',
1030 'video/avi': '.avi',
1032 // Developed by 3GPP
1033 // common video formats for cell phones
1034 'video/3gpp': [ '.3gp', '.3gpp' ],
1035 'video/3gpp2': [ '.3g2', '.3gpp2' ],
1037 // Developed by FFmpeg/Mplayer
1038 'application/x-nut': '.nut',
1040 // The standard video format used by many Sony and Panasonic HD camcorders.
1041 // It is also used for storing high definition video on Blu-ray discs.
1042 'video/mp2t': '.mts',
1043 'video/vnd.dlna.mpeg-tts': '.mts',
1045 'video/m2ts': '.m2ts',
1047 // Old formats reliant on MPEG-1/MPEG-2
1048 'video/mpv': '.mpv',
1049 'video/mpeg2': '.m2v',
1050 'video/mpeg': [ '.m1v', '.mpg', '.mpe', '.mpeg', '.vob' ],
1051 'video/dvd': '.vob',
1053 // Could be anything
1054 'application/octet-stream': null,
1055 'application/mxf': '.mxf' // often used as exchange format by editing software
1059 if (CONFIG.TRANSCODING.ALLOW_AUDIO_FILES) {
1060 Object.assign(data, MIMETYPES.AUDIO.MIMETYPE_EXT)
1067 function updateWebserverUrls () {
1068 WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
1069 WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
1070 WEBSERVER.WS = CONFIG.WEBSERVER.WS
1072 WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME
1073 WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME
1074 WEBSERVER.PORT = CONFIG.WEBSERVER.PORT
1076 const rtmpHostname = CONFIG.LIVE.RTMP.PUBLIC_HOSTNAME || CONFIG.WEBSERVER.HOSTNAME
1077 const rtmpsHostname = CONFIG.LIVE.RTMPS.PUBLIC_HOSTNAME || CONFIG.WEBSERVER.HOSTNAME
1079 WEBSERVER.RTMP_URL = 'rtmp://' + rtmpHostname + ':' + CONFIG.LIVE.RTMP.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1080 WEBSERVER.RTMPS_URL = 'rtmps://' + rtmpsHostname + ':' + CONFIG.LIVE.RTMPS.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1083 function updateWebserverConfig () {
1084 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
1085 MIMETYPES.VIDEO.MIMETYPES_REGEX = buildMimetypesRegex(MIMETYPES.VIDEO.MIMETYPE_EXT)
1087 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
1089 MIMETYPES.VIDEO.EXT_MIMETYPE = buildVideoExtMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT)
1091 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = Object.keys(MIMETYPES.VIDEO.EXT_MIMETYPE)
1094 function buildVideoExtMimetype (obj: { [ id: string ]: string | string[] }) {
1095 const result: { [id: string]: string } = {}
1097 for (const mimetype of Object.keys(obj)) {
1098 const value = obj[mimetype]
1099 if (!value) continue
1101 const extensions = Array.isArray(value) ? value : [ value ]
1103 for (const extension of extensions) {
1104 result[extension] = mimetype
1111 function buildMimetypesRegex (obj: { [id: string]: string | string[] }) {
1112 return Object.keys(obj)
1117 function loadLanguages () {
1118 Object.assign(VIDEO_LANGUAGES, buildLanguages())
1121 function buildLanguages () {
1122 const iso639 = require('iso-639-3')
1124 const languages: { [id: string]: string } = {}
1126 const additionalLanguages = {
1127 sgn: true, // Sign languages (macro language)
1128 ase: true, // American sign language
1129 sdl: true, // Arabian sign language
1130 bfi: true, // British sign language
1131 bzs: true, // Brazilian sign language
1132 csl: true, // Chinese sign language
1133 cse: true, // Czech sign language
1134 dsl: true, // Danish sign language
1135 fsl: true, // French sign language
1136 gsg: true, // German sign language
1137 pks: true, // Pakistan sign language
1138 jsl: true, // Japanese sign language
1139 sfs: true, // South African sign language
1140 swl: true, // Swedish sign language
1141 rsl: true, // Russian sign language
1143 kab: true, // Kabyle
1147 epo: true, // Esperanto
1148 tlh: true, // Klingon
1149 jbo: true, // Lojban
1150 avk: true, // Kotava
1152 zxx: true // No linguistic content (ISO-639-2)
1155 // Only add ISO639-1 languages and some sign languages (ISO639-3)
1158 return (l.iso6391 !== undefined && l.type === 'living') ||
1159 additionalLanguages[l.iso6393] === true
1161 .forEach(l => { languages[l.iso6391 || l.iso6393] = l.name })
1163 // Override Occitan label
1164 languages['oc'] = 'Occitan'
1165 languages['el'] = 'Greek'
1167 // Chinese languages
1168 languages['zh-Hans'] = 'Simplified Chinese'
1169 languages['zh-Hant'] = 'Traditional Chinese'
1174 function generateContentHash () {
1175 return randomBytes(20).toString('hex')