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 import { root } from '../../shared/core-utils'
22 // Do not use barrels, remain constants as independent as possible
23 import { isTestInstance, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
24 import { CONFIG, registerConfigChangedHandler } from './config'
26 // ---------------------------------------------------------------------------
28 const LAST_MIGRATION_VERSION = 670
30 // ---------------------------------------------------------------------------
32 const API_VERSION = 'v1'
33 const PEERTUBE_VERSION: string = require(join(root(), 'package.json')).version
60 // Sortable columns per schema
61 const SORTABLE_COLUMNS = {
62 USERS: [ 'id', 'username', 'videoQuotaUsed', 'createdAt', 'lastLoginDate', 'role' ],
63 USER_SUBSCRIPTIONS: [ 'id', 'createdAt' ],
64 ACCOUNTS: [ 'createdAt' ],
65 JOBS: [ 'createdAt' ],
66 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
67 VIDEO_IMPORTS: [ 'createdAt' ],
69 VIDEO_COMMENT_THREADS: [ 'createdAt', 'totalReplies' ],
70 VIDEO_COMMENTS: [ 'createdAt' ],
72 VIDEO_RATES: [ 'createdAt' ],
73 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
75 INSTANCE_FOLLOWERS: [ 'createdAt', 'state', 'score' ],
76 INSTANCE_FOLLOWING: [ 'createdAt', 'redundancyAllowed', 'state' ],
77 ACCOUNT_FOLLOWERS: [ 'createdAt' ],
78 CHANNEL_FOLLOWERS: [ 'createdAt' ],
80 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending', 'hot', 'best' ],
82 // Don't forget to update peertube-search-index with the same values
83 VIDEOS_SEARCH: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'match' ],
84 VIDEO_CHANNELS_SEARCH: [ 'match', 'displayName', 'createdAt' ],
85 VIDEO_PLAYLISTS_SEARCH: [ 'match', 'displayName', 'createdAt' ],
87 ABUSES: [ 'id', 'createdAt', 'state' ],
89 ACCOUNTS_BLOCKLIST: [ 'createdAt' ],
90 SERVERS_BLOCKLIST: [ 'createdAt' ],
92 USER_NOTIFICATIONS: [ 'createdAt', 'read' ],
94 VIDEO_PLAYLISTS: [ 'name', 'displayName', 'createdAt', 'updatedAt' ],
96 PLUGINS: [ 'name', 'createdAt', 'updatedAt' ],
98 AVAILABLE_PLUGINS: [ 'npmName', 'popularity' ],
100 VIDEO_REDUNDANCIES: [ 'name' ]
103 const OAUTH_LIFETIME = {
104 ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
105 REFRESH_TOKEN: 1209600 // 2 weeks
108 const ROUTE_CACHE_LIFETIME = {
112 SECURITYTXT: '2 hours',
113 NODEINFO: '10 minutes',
114 DNT_POLICY: '1 week',
116 VIDEOS: '1 second' // 1 second, cache concurrent requests after a broadcast for example
121 // ---------------------------------------------------------------------------
123 // Number of points we add/remove after a successful/bad request
124 const ACTOR_FOLLOW_SCORE = {
131 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
136 const REMOTE_SCHEME = {
141 const JOB_ATTEMPTS: { [id in JobType]: number } = {
142 'activitypub-http-broadcast': 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 '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,
172 'move-to-object-storage': 1
174 const JOB_TTL: { [id in JobType]: number } = {
175 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
176 'activitypub-http-unicast': 60000 * 10, // 10 minutes
177 'activitypub-http-fetcher': 1000 * 3600 * 10, // 10 hours
178 'activitypub-follow': 60000 * 10, // 10 minutes
179 'activitypub-cleaner': 1000 * 3600, // 1 hour
180 'video-file-import': 1000 * 3600, // 1 hour
181 'video-transcoding': 1000 * 3600 * 48, // 2 days, transcoding could be long
182 'video-import': 1000 * 3600 * 2, // 2 hours
183 'email': 60000 * 10, // 10 minutes
184 'actor-keys': 60000 * 20, // 20 minutes
185 'videos-views-stats': undefined, // Unlimited
186 'activitypub-refresher': 60000 * 10, // 10 minutes
187 'video-redundancy': 1000 * 3600 * 3, // 3 hours
188 'video-live-ending': 1000 * 60 * 10, // 10 minutes
189 'move-to-object-storage': 1000 * 60 * 60 * 3 // 3 hours
191 const REPEAT_JOBS: { [ id in JobType ]?: EveryRepeatOptions | CronRepeatOptions } = {
192 'videos-views-stats': {
193 cron: randomInt(1, 20) + ' * * * *' // Between 1-20 minutes past the hour
195 'activitypub-cleaner': {
196 cron: '30 5 * * ' + randomInt(0, 7) // 1 time per week (random day) at 5:30 AM
199 const JOB_PRIORITY = {
203 const BROADCAST_CONCURRENCY = 30 // How many requests in parallel we do in activitypub-http-broadcast job
204 const AP_CLEANER_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-cleaner job
205 const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
206 const REQUEST_TIMEOUTS = {
207 DEFAULT: 7000, // 7 seconds
208 FILE: 30000, // 30 seconds
209 REDUNDANCY: JOB_TTL['video-redundancy']
212 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
213 const VIDEO_IMPORT_TIMEOUT = 1000 * 3600 // 1 hour
215 const SCHEDULER_INTERVALS_MS = {
216 ACTOR_FOLLOW_SCORES: 60000 * 60, // 1 hour
217 REMOVE_OLD_JOBS: 60000 * 60, // 1 hour
218 UPDATE_VIDEOS: 60000, // 1 minute
219 YOUTUBE_DL_UPDATE: 60000 * 60 * 24, // 1 day
220 VIDEO_VIEWS_BUFFER_UPDATE: CONFIG.VIEWS.VIDEOS.LOCAL_BUFFER_UPDATE_INTERVAL,
221 CHECK_PLUGINS: CONFIG.PLUGINS.INDEX.CHECK_LATEST_VERSIONS_INTERVAL,
222 CHECK_PEERTUBE_VERSION: 60000 * 60 * 24, // 1 day
223 AUTO_FOLLOW_INDEX_INSTANCES: 60000 * 60 * 24, // 1 day
224 REMOVE_OLD_VIEWS: 60000 * 60 * 24, // 1 day
225 REMOVE_OLD_HISTORY: 60000 * 60 * 24, // 1 day
226 UPDATE_INBOX_STATS: 1000 * 60, // 1 minute
227 REMOVE_DANGLING_RESUMABLE_UPLOADS: 60000 * 60 // 1 hour
230 // ---------------------------------------------------------------------------
232 const CONSTRAINTS_FIELDS = {
234 NAME: { min: 1, max: 120 }, // Length
235 DESCRIPTION: { min: 3, max: 1000 }, // Length
236 USERNAME: { min: 1, max: 50 }, // Length
237 PASSWORD: { min: 6, max: 255 }, // Length
238 VIDEO_QUOTA: { min: -1 },
239 VIDEO_QUOTA_DAILY: { min: -1 },
240 VIDEO_LANGUAGES: { max: 500 }, // Array length
241 BLOCKED_REASON: { min: 3, max: 250 } // Length
244 REASON: { min: 2, max: 3000 }, // Length
245 MODERATION_COMMENT: { min: 2, max: 3000 } // Length
248 MESSAGE: { min: 2, max: 3000 } // Length
251 REASON: { min: 2, max: 300 } // Length
254 NAME: { min: 1, max: 120 }, // Length
255 DESCRIPTION: { min: 3, max: 1000 }, // Length
256 SUPPORT: { min: 3, max: 1000 }, // Length
257 URL: { min: 3, max: 2000 } // Length
261 EXTNAME: [ '.vtt', '.srt' ],
263 max: 4 * 1024 * 1024 // 4MB
268 URL: { min: 3, max: 2000 }, // Length
269 TORRENT_NAME: { min: 3, max: 255 }, // Length
271 EXTNAME: [ '.torrent' ],
273 max: 1024 * 200 // 200 KB
278 URL: { min: 3, max: 2000 } // Length
281 URL: { min: 3, max: 2000 } // Length
284 NAME: { min: 3, max: 120 }, // Length
285 LANGUAGE: { min: 1, max: 10 }, // Length
286 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
287 DESCRIPTION: { min: 3, max: 10000 }, // Length
288 SUPPORT: { min: 3, max: 1000 }, // Length
290 EXTNAME: [ '.png', '.jpg', '.jpeg', '.webp' ],
292 max: 4 * 1024 * 1024 // 4MB
295 EXTNAME: [] as string[],
296 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
297 DURATION: { min: 0 }, // Number
298 TAGS: { min: 0, max: 5 }, // Number of total tags
299 TAG: { min: 2, max: 30 }, // Length
302 DISLIKES: { min: 0 },
303 FILE_SIZE: { min: -1 },
304 PARTIAL_UPLOAD_SIZE: { max: 50 * 1024 * 1024 * 1024 }, // 50GB
305 URL: { min: 3, max: 2000 } // Length
308 NAME: { min: 1, max: 120 }, // Length
309 DESCRIPTION: { min: 3, max: 1000 }, // Length
310 URL: { min: 3, max: 2000 }, // Length
312 EXTNAME: [ '.jpg', '.jpeg' ],
314 max: 4 * 1024 * 1024 // 4MB
319 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
320 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
321 URL: { min: 3, max: 2000 }, // Length
323 EXTNAME: [ '.png', '.jpeg', '.jpg', '.gif', '.webp' ],
325 max: 4 * 1024 * 1024 // 4MB
333 TEXT: { min: 1, max: 10000 }, // Length
334 URL: { min: 3, max: 2000 } // Length
337 URL: { min: 3, max: 2000 } // Length
340 FROM_NAME: { min: 1, max: 120 }, // Length
341 BODY: { min: 3, max: 5000 } // Length
344 NAME: { min: 1, max: 214 }, // Length
345 DESCRIPTION: { min: 1, max: 20000 } // Length
348 URL: { min: 5, max: 2000 } // Length
352 const VIEW_LIFETIME = {
353 VIEW: CONFIG.VIEWS.VIDEOS.IP_VIEW_EXPIRATION,
354 VIEWER: 60000 * 5 // 5 minutes
357 let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour
359 const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
361 STANDARD: [ 24, 25, 30 ],
362 HD_STANDARD: [ 50, 60 ],
365 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
368 const DEFAULT_AUDIO_RESOLUTION = VideoResolution.H_480P
370 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
375 const FFMPEG_NICE: { [ id: string ]: number } = {
376 // parent process defaults to niceness = 0
377 // reminder: lower = higher priority, max value is 19, lowest is -20
378 LIVE: 5, // prioritize over VOD and THUMBNAIL
383 const VIDEO_CATEGORIES = {
394 11: 'News & Politics',
398 15: 'Science & Technology',
404 // See https://creativecommons.org/licenses/?lang=en
405 const VIDEO_LICENCES = {
407 2: 'Attribution - Share Alike',
408 3: 'Attribution - No Derivatives',
409 4: 'Attribution - Non Commercial',
410 5: 'Attribution - Non Commercial - Share Alike',
411 6: 'Attribution - Non Commercial - No Derivatives',
412 7: 'Public Domain Dedication'
415 const VIDEO_LANGUAGES: { [id: string]: string } = {}
417 const VIDEO_PRIVACIES: { [ id in VideoPrivacy ]: string } = {
418 [VideoPrivacy.PUBLIC]: 'Public',
419 [VideoPrivacy.UNLISTED]: 'Unlisted',
420 [VideoPrivacy.PRIVATE]: 'Private',
421 [VideoPrivacy.INTERNAL]: 'Internal'
424 const VIDEO_STATES: { [ id in VideoState ]: string } = {
425 [VideoState.PUBLISHED]: 'Published',
426 [VideoState.TO_TRANSCODE]: 'To transcode',
427 [VideoState.TO_IMPORT]: 'To import',
428 [VideoState.WAITING_FOR_LIVE]: 'Waiting for livestream',
429 [VideoState.LIVE_ENDED]: 'Livestream ended',
430 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE]: 'To move to an external storage',
431 [VideoState.TRANSCODING_FAILED]: 'Transcoding failed'
434 const VIDEO_IMPORT_STATES: { [ id in VideoImportState ]: string } = {
435 [VideoImportState.FAILED]: 'Failed',
436 [VideoImportState.PENDING]: 'Pending',
437 [VideoImportState.SUCCESS]: 'Success',
438 [VideoImportState.REJECTED]: 'Rejected'
441 const ABUSE_STATES: { [ id in AbuseState ]: string } = {
442 [AbuseState.PENDING]: 'Pending',
443 [AbuseState.REJECTED]: 'Rejected',
444 [AbuseState.ACCEPTED]: 'Accepted'
447 const VIDEO_PLAYLIST_PRIVACIES: { [ id in VideoPlaylistPrivacy ]: string } = {
448 [VideoPlaylistPrivacy.PUBLIC]: 'Public',
449 [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted',
450 [VideoPlaylistPrivacy.PRIVATE]: 'Private'
453 const VIDEO_PLAYLIST_TYPES: { [ id in VideoPlaylistType ]: string } = {
454 [VideoPlaylistType.REGULAR]: 'Regular',
455 [VideoPlaylistType.WATCH_LATER]: 'Watch later'
461 'audio/mpeg': '.mp3',
463 'application/ogg': '.ogg',
465 'audio/x-ms-wma': '.wma',
467 'audio/x-wav': '.wav',
468 'audio/x-flac': '.flac',
469 'audio/flac': '.flac',
473 'audio/x-m4a': '.m4a',
476 EXT_MIMETYPE: null as { [ id: string ]: string }
479 MIMETYPE_EXT: null as { [ id: string ]: string | string[] },
480 MIMETYPES_REGEX: null as string,
481 EXT_MIMETYPE: null as { [ id: string ]: string }
487 'image/webp': '.webp',
491 EXT_MIMETYPE: null as { [ id: string ]: string }
496 'application/x-subrip': '.srt',
502 'application/x-bittorrent': '.torrent'
506 MIMETYPES.AUDIO.EXT_MIMETYPE = invert(MIMETYPES.AUDIO.MIMETYPE_EXT)
507 MIMETYPES.IMAGE.EXT_MIMETYPE = invert(MIMETYPES.IMAGE.MIMETYPE_EXT)
509 const BINARY_CONTENT_TYPES = new Set([
510 'binary/octet-stream',
511 'application/octet-stream',
512 'application/x-binary'
515 // ---------------------------------------------------------------------------
524 // ---------------------------------------------------------------------------
526 const SERVER_ACTOR_NAME = 'peertube'
528 const ACTIVITY_PUB = {
529 POTENTIAL_ACCEPT_HEADERS: [
530 'application/activity+json',
531 'application/ld+json',
532 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
534 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
535 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
536 COLLECTION_ITEMS_PER_PAGE: 10,
537 FETCH_PAGE_LIMIT: 2000,
539 VIDEO: [] as string[],
540 TORRENT: [ 'application/x-bittorrent' ],
541 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
543 MAX_RECURSION_COMMENTS: 100,
544 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
545 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
546 VIDEO_PLAYLIST_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2 // 2 days
549 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
552 APPLICATION: 'Application',
553 ORGANIZATION: 'Organization',
557 const HTTP_SIGNATURE = {
558 HEADER_NAME: 'signature',
559 ALGORITHM: 'rsa-sha256',
560 HEADERS_TO_SIGN: [ '(request-target)', 'host', 'date', 'digest' ],
562 ALL: [ '(request-target)', 'host', 'date' ],
563 POST: [ '(request-target)', 'host', 'date', 'digest' ]
565 CLOCK_SKEW_SECONDS: 1800
568 // ---------------------------------------------------------------------------
570 let PRIVATE_RSA_KEY_SIZE = 2048
572 // Password encryption
573 const BCRYPT_SALT_SIZE = 10
575 const USER_PASSWORD_RESET_LIFETIME = 60000 * 60 // 60 minutes
576 const USER_PASSWORD_CREATE_LIFETIME = 60000 * 60 * 24 * 7 // 7 days
578 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
580 const NSFW_POLICY_TYPES: { [ id: string ]: NSFWPolicyType } = {
581 DO_NOT_LIST: 'do_not_list',
586 // ---------------------------------------------------------------------------
588 // Express static paths (router)
589 const STATIC_PATHS = {
590 THUMBNAILS: '/static/thumbnails/',
591 WEBSEED: '/static/webseed/',
592 REDUNDANCY: '/static/redundancy/',
593 STREAMING_PLAYLISTS: {
594 HLS: '/static/streaming-playlists/hls'
597 const STATIC_DOWNLOAD_PATHS = {
598 TORRENTS: '/download/torrents/',
599 VIDEOS: '/download/videos/',
600 HLS_VIDEOS: '/download/streaming-playlists/hls/videos/'
602 const LAZY_STATIC_PATHS = {
603 BANNERS: '/lazy-static/banners/',
604 AVATARS: '/lazy-static/avatars/',
605 PREVIEWS: '/lazy-static/previews/',
606 VIDEO_CAPTIONS: '/lazy-static/video-captions/',
607 TORRENTS: '/lazy-static/torrents/'
611 const STATIC_MAX_AGE = {
617 // Videos thumbnail size
618 const THUMBNAILS_SIZE = {
623 const PREVIEWS_SIZE = {
628 const ACTOR_IMAGES_SIZE = {
635 height: 317 // 6/1 ratio
644 // Sub folders of cache directory
645 const FILES_CACHE = {
647 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
648 MAX_AGE: 1000 * 3600 * 3 // 3 hours
651 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
652 MAX_AGE: 1000 * 3600 * 3 // 3 hours
655 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'torrents'),
656 MAX_AGE: 1000 * 3600 * 3 // 3 hours
664 ACTOR_IMAGE_STATIC: {
669 const RESUMABLE_UPLOAD_DIRECTORY = join(CONFIG.STORAGE.TMP_DIR, 'resumable-uploads')
670 const HLS_STREAMING_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.STREAMING_PLAYLISTS_DIR, 'hls')
671 const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls')
673 const RESUMABLE_UPLOAD_SESSION_LIFETIME = SCHEDULER_INTERVALS_MS.REMOVE_DANGLING_RESUMABLE_UPLOADS
677 CLEANUP_DELAY: 1000 * 60 * 5, // 5 minutes
678 SEGMENT_TIME_SECONDS: 4, // 4 seconds
679 SEGMENTS_LIST_SIZE: 15, // 15 maximum segments in live playlist
680 REPLAY_DIRECTORY: 'replay',
681 EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION: 4,
682 MAX_SOCKET_WAITING_DATA: 1024 * 1000 * 100, // 100MB
692 const MEMOIZE_TTL = {
693 OVERVIEWS_SAMPLE: 1000 * 3600 * 4, // 4 hours
694 INFO_HASH_EXISTS: 1000 * 3600 * 12, // 12 hours
695 LIVE_ABLE_TO_UPLOAD: 1000 * 60, // 1 minute
696 LIVE_CHECK_SOCKET_HEALTH: 1000 * 60 // 1 minute
699 const MEMOIZE_LENGTH = {
700 INFO_HASH_EXISTS: 200
703 const QUEUE_CONCURRENCY = {
704 ACTOR_PROCESS_IMAGE: 3
713 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
715 const ASSETS_PATH = {
716 DEFAULT_AUDIO_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-audio-background.jpg'),
717 DEFAULT_LIVE_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-live-background.jpg')
720 // ---------------------------------------------------------------------------
722 const CUSTOM_HTML_TAG_COMMENTS = {
723 TITLE: '<!-- title tag -->',
724 DESCRIPTION: '<!-- description tag -->',
725 CUSTOM_CSS: '<!-- custom css tag -->',
726 META_TAGS: '<!-- meta tags -->',
727 SERVER_CONFIG: '<!-- server config -->'
730 // ---------------------------------------------------------------------------
736 const MAX_LOGS_OUTPUT_CHARACTERS = 10 * 1000 * 1000
737 const LOG_FILENAME = 'peertube.log'
738 const AUDIT_LOG_FILENAME = 'peertube-audit.log'
740 // ---------------------------------------------------------------------------
742 const TRACKER_RATE_LIMITS = {
743 INTERVAL: 60000 * 5, // 5 minutes
744 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
745 ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
746 BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
749 const P2P_MEDIA_LOADER_PEER_VERSION = 2
751 // ---------------------------------------------------------------------------
753 const PLUGIN_GLOBAL_CSS_FILE_NAME = 'plugins-global.css'
754 const PLUGIN_GLOBAL_CSS_PATH = join(CONFIG.STORAGE.TMP_DIR, PLUGIN_GLOBAL_CSS_FILE_NAME)
756 let PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 1000 * 60 * 5 // 5 minutes
758 const DEFAULT_THEME_NAME = 'default'
759 const DEFAULT_USER_THEME_NAME = 'instance-default'
761 // ---------------------------------------------------------------------------
763 const SEARCH_INDEX = {
765 VIDEOS: '/api/v1/search/videos',
766 VIDEO_CHANNELS: '/api/v1/search/video-channels'
770 // ---------------------------------------------------------------------------
772 // Special constants for a test instance
773 if (isTestInstance() === true) {
774 PRIVATE_RSA_KEY_SIZE = 1024
776 ACTOR_FOLLOW_SCORE.BASE = 20
778 REMOTE_SCHEME.HTTP = 'http'
779 REMOTE_SCHEME.WS = 'ws'
781 STATIC_MAX_AGE.SERVER = '0'
783 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
784 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
785 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
786 ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
788 CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max = 100 * 1024 // 100KB
789 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max = 400 * 1024 // 400KB
791 SCHEDULER_INTERVALS_MS.ACTOR_FOLLOW_SCORES = 1000
792 SCHEDULER_INTERVALS_MS.REMOVE_OLD_JOBS = 10000
793 SCHEDULER_INTERVALS_MS.REMOVE_OLD_HISTORY = 5000
794 SCHEDULER_INTERVALS_MS.REMOVE_OLD_VIEWS = 5000
795 SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS = 5000
796 SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES = 5000
797 SCHEDULER_INTERVALS_MS.UPDATE_INBOX_STATS = 5000
798 SCHEDULER_INTERVALS_MS.CHECK_PEERTUBE_VERSION = 2000
799 REPEAT_JOBS['videos-views-stats'] = { every: 5000 }
800 REPEAT_JOBS['activitypub-cleaner'] = { every: 5000 }
802 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
804 VIEW_LIFETIME.VIEWER = 1000 * 5 // 5 second
805 CONTACT_FORM_LIFETIME = 1000 // 1 second
807 JOB_ATTEMPTS['email'] = 1
809 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
810 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 3000
811 MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD = 3000
812 OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD = 2
814 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 5000
816 VIDEO_LIVE.CLEANUP_DELAY = 5000
817 VIDEO_LIVE.SEGMENT_TIME_SECONDS = 2
818 VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION = 1
821 updateWebserverUrls()
822 updateWebserverConfig()
824 registerConfigChangedHandler(() => {
825 updateWebserverUrls()
826 updateWebserverConfig()
829 // ---------------------------------------------------------------------------
831 const FILES_CONTENT_HASH = {
832 MANIFEST: generateContentHash(),
833 FAVICON: generateContentHash(),
834 LOGO: generateContentHash()
837 // ---------------------------------------------------------------------------
846 RESUMABLE_UPLOAD_DIRECTORY,
847 RESUMABLE_UPLOAD_SESSION_LIFETIME,
848 HLS_REDUNDANCY_DIRECTORY,
849 P2P_MEDIA_LOADER_PEER_VERSION,
861 AP_CLEANER_CONCURRENCY,
862 LAST_MIGRATION_VERSION,
864 CUSTOM_HTML_TAG_COMMENTS,
865 BROADCAST_CONCURRENCY,
872 DEFAULT_USER_THEME_NAME,
874 PLUGIN_GLOBAL_CSS_FILE_NAME,
875 PLUGIN_GLOBAL_CSS_PATH,
876 PRIVATE_RSA_KEY_SIZE,
877 ROUTE_CACHE_LIFETIME,
879 HLS_STREAMING_PLAYLIST_DIRECTORY,
886 VIDEO_IMPORT_TIMEOUT,
887 VIDEO_PLAYLIST_TYPES,
888 MAX_LOGS_OUTPUT_CHARACTERS,
890 ACTIVITY_PUB_ACTOR_TYPES,
901 VIDEO_TRANSCODING_FPS,
906 USER_PASSWORD_RESET_LIFETIME,
907 USER_PASSWORD_CREATE_LIFETIME,
909 USER_EMAIL_VERIFY_LIFETIME,
911 SCHEDULER_INTERVALS_MS,
913 STATIC_DOWNLOAD_PATHS,
915 CRAWL_REQUEST_CONCURRENCY,
916 DEFAULT_AUDIO_RESOLUTION,
917 BINARY_CONTENT_TYPES,
918 JOB_COMPLETED_LIFETIME,
922 CONTACT_FORM_LIFETIME,
923 VIDEO_PLAYLIST_PRIVACIES,
924 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
932 // ---------------------------------------------------------------------------
934 function buildVideoMimetypeExt () {
936 // streamable formats that warrant cross-browser compatibility
937 'video/webm': '.webm',
938 // We'll add .ogg if additional extensions are enabled
939 // We could add .ogg here but since it could be an audio file,
940 // it would be confusing for users because PeerTube will refuse their file (based on the mimetype)
941 'video/ogg': [ '.ogv' ],
945 if (CONFIG.TRANSCODING.ENABLED) {
946 if (CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
947 data['video/ogg'].push('.ogg')
949 Object.assign(data, {
950 'video/x-matroska': '.mkv',
952 // Developed by Apple
953 'video/quicktime': [ '.mov', '.qt', '.mqv' ], // often used as output format by editing software
954 'video/x-m4v': '.m4v',
957 // Developed by the Adobe Flash Platform
958 'video/x-flv': '.flv',
959 'video/x-f4v': '.f4v', // replacement for flv
961 // Developed by Microsoft
962 'video/x-ms-wmv': '.wmv',
963 'video/x-msvideo': '.avi',
967 // common video formats for cell phones
968 'video/3gpp': [ '.3gp', '.3gpp' ],
969 'video/3gpp2': [ '.3g2', '.3gpp2' ],
971 // Developed by FFmpeg/Mplayer
972 'application/x-nut': '.nut',
974 // The standard video format used by many Sony and Panasonic HD camcorders.
975 // It is also used for storing high definition video on Blu-ray discs.
976 'video/mp2t': '.mts',
977 'video/m2ts': '.m2ts',
979 // Old formats reliant on MPEG-1/MPEG-2
981 'video/mpeg2': '.m2v',
982 'video/mpeg': [ '.m1v', '.mpg', '.mpe', '.mpeg', '.vob' ],
986 'application/octet-stream': null,
987 'application/mxf': '.mxf' // often used as exchange format by editing software
991 if (CONFIG.TRANSCODING.ALLOW_AUDIO_FILES) {
992 Object.assign(data, MIMETYPES.AUDIO.MIMETYPE_EXT)
999 function updateWebserverUrls () {
1000 WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
1001 WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
1002 WEBSERVER.WS = CONFIG.WEBSERVER.WS
1004 WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME
1005 WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME
1006 WEBSERVER.PORT = CONFIG.WEBSERVER.PORT
1008 WEBSERVER.RTMP_URL = 'rtmp://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.LIVE.RTMP.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1009 WEBSERVER.RTMPS_URL = 'rtmps://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.LIVE.RTMPS.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1012 function updateWebserverConfig () {
1013 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
1014 MIMETYPES.VIDEO.MIMETYPES_REGEX = buildMimetypesRegex(MIMETYPES.VIDEO.MIMETYPE_EXT)
1016 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
1018 MIMETYPES.VIDEO.EXT_MIMETYPE = buildVideoExtMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT)
1020 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = Object.keys(MIMETYPES.VIDEO.EXT_MIMETYPE)
1023 function buildVideoExtMimetype (obj: { [ id: string ]: string | string[] }) {
1024 const result: { [id: string]: string } = {}
1026 for (const mimetype of Object.keys(obj)) {
1027 const value = obj[mimetype]
1028 if (!value) continue
1030 const extensions = Array.isArray(value) ? value : [ value ]
1032 for (const extension of extensions) {
1033 result[extension] = mimetype
1040 function buildMimetypesRegex (obj: { [id: string]: string | string[] }) {
1041 return Object.keys(obj)
1046 function loadLanguages () {
1047 Object.assign(VIDEO_LANGUAGES, buildLanguages())
1050 function buildLanguages () {
1051 const iso639 = require('iso-639-3')
1053 const languages: { [id: string]: string } = {}
1055 const additionalLanguages = {
1056 sgn: true, // Sign languages (macro language)
1057 ase: true, // American sign language
1058 sdl: true, // Arabian sign language
1059 bfi: true, // British sign language
1060 bzs: true, // Brazilian sign language
1061 csl: true, // Chinese sign language
1062 cse: true, // Czech sign language
1063 dsl: true, // Danish sign language
1064 fsl: true, // French sign language
1065 gsg: true, // German sign language
1066 pks: true, // Pakistan sign language
1067 jsl: true, // Japanese sign language
1068 sfs: true, // South African sign language
1069 swl: true, // Swedish sign language
1070 rsl: true, // Russian sign language
1072 kab: true, // Kabyle
1076 epo: true, // Esperanto
1077 tlh: true, // Klingon
1078 jbo: true, // Lojban
1079 avk: true, // Kotava
1081 zxx: true // No linguistic content (ISO-639-2)
1084 // Only add ISO639-1 languages and some sign languages (ISO639-3)
1087 return (l.iso6391 !== undefined && l.type === 'living') ||
1088 additionalLanguages[l.iso6393] === true
1090 .forEach(l => { languages[l.iso6391 || l.iso6393] = l.name })
1092 // Override Occitan label
1093 languages['oc'] = 'Occitan'
1094 languages['el'] = 'Greek'
1099 function generateContentHash () {
1100 return randomBytes(20).toString('hex')