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 { 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 = 675
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 CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
206 CONCURRENCY: 10, // How many requests in parallel we do in activitypub-cleaner job
207 UNAVAILABLE_TRESHOLD: 3, // How many attemps we do before removing an unavailable remote resource
208 PERIOD: parseDurationToMs('1 week') // /!\ Has to be sync with REPEAT_JOBS
211 const REQUEST_TIMEOUTS = {
212 DEFAULT: 7000, // 7 seconds
213 FILE: 30000, // 30 seconds
214 REDUNDANCY: JOB_TTL['video-redundancy']
217 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
218 const VIDEO_IMPORT_TIMEOUT = 1000 * 3600 // 1 hour
220 const SCHEDULER_INTERVALS_MS = {
221 ACTOR_FOLLOW_SCORES: 60000 * 60, // 1 hour
222 REMOVE_OLD_JOBS: 60000 * 60, // 1 hour
223 UPDATE_VIDEOS: 60000, // 1 minute
224 YOUTUBE_DL_UPDATE: 60000 * 60 * 24, // 1 day
225 VIDEO_VIEWS_BUFFER_UPDATE: CONFIG.VIEWS.VIDEOS.LOCAL_BUFFER_UPDATE_INTERVAL,
226 CHECK_PLUGINS: CONFIG.PLUGINS.INDEX.CHECK_LATEST_VERSIONS_INTERVAL,
227 CHECK_PEERTUBE_VERSION: 60000 * 60 * 24, // 1 day
228 AUTO_FOLLOW_INDEX_INSTANCES: 60000 * 60 * 24, // 1 day
229 REMOVE_OLD_VIEWS: 60000 * 60 * 24, // 1 day
230 REMOVE_OLD_HISTORY: 60000 * 60 * 24, // 1 day
231 UPDATE_INBOX_STATS: 1000 * 60, // 1 minute
232 REMOVE_DANGLING_RESUMABLE_UPLOADS: 60000 * 60 // 1 hour
235 // ---------------------------------------------------------------------------
237 const CONSTRAINTS_FIELDS = {
239 NAME: { min: 1, max: 120 }, // Length
240 DESCRIPTION: { min: 3, max: 1000 }, // Length
241 USERNAME: { min: 1, max: 50 }, // Length
242 PASSWORD: { min: 6, max: 255 }, // Length
243 VIDEO_QUOTA: { min: -1 },
244 VIDEO_QUOTA_DAILY: { min: -1 },
245 VIDEO_LANGUAGES: { max: 500 }, // Array length
246 BLOCKED_REASON: { min: 3, max: 250 } // Length
249 REASON: { min: 2, max: 3000 }, // Length
250 MODERATION_COMMENT: { min: 2, max: 3000 } // Length
253 MESSAGE: { min: 2, max: 3000 } // Length
256 REASON: { min: 2, max: 300 } // Length
259 NAME: { min: 1, max: 120 }, // Length
260 DESCRIPTION: { min: 3, max: 1000 }, // Length
261 SUPPORT: { min: 3, max: 1000 }, // Length
262 URL: { min: 3, max: 2000 } // Length
266 EXTNAME: [ '.vtt', '.srt' ],
268 max: 4 * 1024 * 1024 // 4MB
273 URL: { min: 3, max: 2000 }, // Length
274 TORRENT_NAME: { min: 3, max: 255 }, // Length
276 EXTNAME: [ '.torrent' ],
278 max: 1024 * 200 // 200 KB
283 URL: { min: 3, max: 2000 } // Length
286 URL: { min: 3, max: 2000 } // Length
289 NAME: { min: 3, max: 120 }, // Length
290 LANGUAGE: { min: 1, max: 10 }, // Length
291 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
292 DESCRIPTION: { min: 3, max: 10000 }, // Length
293 SUPPORT: { min: 3, max: 1000 }, // Length
295 EXTNAME: [ '.png', '.jpg', '.jpeg', '.webp' ],
297 max: 4 * 1024 * 1024 // 4MB
300 EXTNAME: [] as string[],
301 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
302 DURATION: { min: 0 }, // Number
303 TAGS: { min: 0, max: 5 }, // Number of total tags
304 TAG: { min: 2, max: 30 }, // Length
307 DISLIKES: { min: 0 },
308 FILE_SIZE: { min: -1 },
309 PARTIAL_UPLOAD_SIZE: { max: 50 * 1024 * 1024 * 1024 }, // 50GB
310 URL: { min: 3, max: 2000 } // Length
313 NAME: { min: 1, max: 120 }, // Length
314 DESCRIPTION: { min: 3, max: 1000 }, // Length
315 URL: { min: 3, max: 2000 }, // Length
317 EXTNAME: [ '.jpg', '.jpeg' ],
319 max: 4 * 1024 * 1024 // 4MB
324 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
325 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
326 URL: { min: 3, max: 2000 }, // Length
328 EXTNAME: [ '.png', '.jpeg', '.jpg', '.gif', '.webp' ],
330 max: 4 * 1024 * 1024 // 4MB
338 TEXT: { min: 1, max: 10000 }, // Length
339 URL: { min: 3, max: 2000 } // Length
342 URL: { min: 3, max: 2000 } // Length
345 FROM_NAME: { min: 1, max: 120 }, // Length
346 BODY: { min: 3, max: 5000 } // Length
349 NAME: { min: 1, max: 214 }, // Length
350 DESCRIPTION: { min: 1, max: 20000 } // Length
353 URL: { min: 5, max: 2000 } // Length
357 const VIEW_LIFETIME = {
358 VIEW: CONFIG.VIEWS.VIDEOS.IP_VIEW_EXPIRATION,
359 VIEWER: 60000 * 5 // 5 minutes
362 let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour
364 const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
366 STANDARD: [ 24, 25, 30 ],
367 HD_STANDARD: [ 50, 60 ],
370 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
373 const DEFAULT_AUDIO_RESOLUTION = VideoResolution.H_480P
375 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
380 const FFMPEG_NICE: { [ id: string ]: number } = {
381 // parent process defaults to niceness = 0
382 // reminder: lower = higher priority, max value is 19, lowest is -20
383 LIVE: 5, // prioritize over VOD and THUMBNAIL
388 const VIDEO_CATEGORIES = {
399 11: 'News & Politics',
403 15: 'Science & Technology',
409 // See https://creativecommons.org/licenses/?lang=en
410 const VIDEO_LICENCES = {
412 2: 'Attribution - Share Alike',
413 3: 'Attribution - No Derivatives',
414 4: 'Attribution - Non Commercial',
415 5: 'Attribution - Non Commercial - Share Alike',
416 6: 'Attribution - Non Commercial - No Derivatives',
417 7: 'Public Domain Dedication'
420 const VIDEO_LANGUAGES: { [id: string]: string } = {}
422 const VIDEO_PRIVACIES: { [ id in VideoPrivacy ]: string } = {
423 [VideoPrivacy.PUBLIC]: 'Public',
424 [VideoPrivacy.UNLISTED]: 'Unlisted',
425 [VideoPrivacy.PRIVATE]: 'Private',
426 [VideoPrivacy.INTERNAL]: 'Internal'
429 const VIDEO_STATES: { [ id in VideoState ]: string } = {
430 [VideoState.PUBLISHED]: 'Published',
431 [VideoState.TO_TRANSCODE]: 'To transcode',
432 [VideoState.TO_IMPORT]: 'To import',
433 [VideoState.WAITING_FOR_LIVE]: 'Waiting for livestream',
434 [VideoState.LIVE_ENDED]: 'Livestream ended',
435 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE]: 'To move to an external storage',
436 [VideoState.TRANSCODING_FAILED]: 'Transcoding failed',
437 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE_FAILED]: 'External storage move failed'
440 const VIDEO_IMPORT_STATES: { [ id in VideoImportState ]: string } = {
441 [VideoImportState.FAILED]: 'Failed',
442 [VideoImportState.PENDING]: 'Pending',
443 [VideoImportState.SUCCESS]: 'Success',
444 [VideoImportState.REJECTED]: 'Rejected'
447 const ABUSE_STATES: { [ id in AbuseState ]: string } = {
448 [AbuseState.PENDING]: 'Pending',
449 [AbuseState.REJECTED]: 'Rejected',
450 [AbuseState.ACCEPTED]: 'Accepted'
453 const VIDEO_PLAYLIST_PRIVACIES: { [ id in VideoPlaylistPrivacy ]: string } = {
454 [VideoPlaylistPrivacy.PUBLIC]: 'Public',
455 [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted',
456 [VideoPlaylistPrivacy.PRIVATE]: 'Private'
459 const VIDEO_PLAYLIST_TYPES: { [ id in VideoPlaylistType ]: string } = {
460 [VideoPlaylistType.REGULAR]: 'Regular',
461 [VideoPlaylistType.WATCH_LATER]: 'Watch later'
467 'audio/mpeg': '.mp3',
469 'application/ogg': '.ogg',
471 'audio/x-ms-wma': '.wma',
473 'audio/x-wav': '.wav',
474 'audio/x-flac': '.flac',
475 'audio/flac': '.flac',
479 'audio/x-m4a': '.m4a',
482 EXT_MIMETYPE: null as { [ id: string ]: string }
485 MIMETYPE_EXT: null as { [ id: string ]: string | string[] },
486 MIMETYPES_REGEX: null as string,
487 EXT_MIMETYPE: null as { [ id: string ]: string }
493 'image/webp': '.webp',
497 EXT_MIMETYPE: null as { [ id: string ]: string }
502 'application/x-subrip': '.srt',
508 'application/x-bittorrent': '.torrent'
512 MIMETYPES.AUDIO.EXT_MIMETYPE = invert(MIMETYPES.AUDIO.MIMETYPE_EXT)
513 MIMETYPES.IMAGE.EXT_MIMETYPE = invert(MIMETYPES.IMAGE.MIMETYPE_EXT)
515 const BINARY_CONTENT_TYPES = new Set([
516 'binary/octet-stream',
517 'application/octet-stream',
518 'application/x-binary'
521 // ---------------------------------------------------------------------------
530 // ---------------------------------------------------------------------------
532 const SERVER_ACTOR_NAME = 'peertube'
534 const ACTIVITY_PUB = {
535 POTENTIAL_ACCEPT_HEADERS: [
536 'application/activity+json',
537 'application/ld+json',
538 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
540 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
541 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
542 COLLECTION_ITEMS_PER_PAGE: 10,
543 FETCH_PAGE_LIMIT: 2000,
545 VIDEO: [] as string[],
546 TORRENT: [ 'application/x-bittorrent' ],
547 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
549 MAX_RECURSION_COMMENTS: 100,
550 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
551 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
552 VIDEO_PLAYLIST_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2 // 2 days
555 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
558 APPLICATION: 'Application',
559 ORGANIZATION: 'Organization',
563 const HTTP_SIGNATURE = {
564 HEADER_NAME: 'signature',
565 ALGORITHM: 'rsa-sha256',
566 HEADERS_TO_SIGN: [ '(request-target)', 'host', 'date', 'digest' ],
568 ALL: [ '(request-target)', 'host', 'date' ],
569 POST: [ '(request-target)', 'host', 'date', 'digest' ]
571 CLOCK_SKEW_SECONDS: 1800
574 // ---------------------------------------------------------------------------
576 let PRIVATE_RSA_KEY_SIZE = 2048
578 // Password encryption
579 const BCRYPT_SALT_SIZE = 10
581 const USER_PASSWORD_RESET_LIFETIME = 60000 * 60 // 60 minutes
582 const USER_PASSWORD_CREATE_LIFETIME = 60000 * 60 * 24 * 7 // 7 days
584 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
586 const NSFW_POLICY_TYPES: { [ id: string ]: NSFWPolicyType } = {
587 DO_NOT_LIST: 'do_not_list',
592 // ---------------------------------------------------------------------------
594 // Express static paths (router)
595 const STATIC_PATHS = {
596 THUMBNAILS: '/static/thumbnails/',
597 WEBSEED: '/static/webseed/',
598 REDUNDANCY: '/static/redundancy/',
599 STREAMING_PLAYLISTS: {
600 HLS: '/static/streaming-playlists/hls'
603 const STATIC_DOWNLOAD_PATHS = {
604 TORRENTS: '/download/torrents/',
605 VIDEOS: '/download/videos/',
606 HLS_VIDEOS: '/download/streaming-playlists/hls/videos/'
608 const LAZY_STATIC_PATHS = {
609 BANNERS: '/lazy-static/banners/',
610 AVATARS: '/lazy-static/avatars/',
611 PREVIEWS: '/lazy-static/previews/',
612 VIDEO_CAPTIONS: '/lazy-static/video-captions/',
613 TORRENTS: '/lazy-static/torrents/'
617 const STATIC_MAX_AGE = {
623 // Videos thumbnail size
624 const THUMBNAILS_SIZE = {
629 const PREVIEWS_SIZE = {
634 const ACTOR_IMAGES_SIZE = {
641 height: 317 // 6/1 ratio
650 // Sub folders of cache directory
651 const FILES_CACHE = {
653 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
654 MAX_AGE: 1000 * 3600 * 3 // 3 hours
657 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
658 MAX_AGE: 1000 * 3600 * 3 // 3 hours
661 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'torrents'),
662 MAX_AGE: 1000 * 3600 * 3 // 3 hours
670 ACTOR_IMAGE_STATIC: {
675 const RESUMABLE_UPLOAD_DIRECTORY = join(CONFIG.STORAGE.TMP_DIR, 'resumable-uploads')
676 const HLS_STREAMING_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.STREAMING_PLAYLISTS_DIR, 'hls')
677 const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls')
679 const RESUMABLE_UPLOAD_SESSION_LIFETIME = SCHEDULER_INTERVALS_MS.REMOVE_DANGLING_RESUMABLE_UPLOADS
683 CLEANUP_DELAY: 1000 * 60 * 5, // 5 minutes
684 SEGMENT_TIME_SECONDS: 4, // 4 seconds
685 SEGMENTS_LIST_SIZE: 15, // 15 maximum segments in live playlist
686 REPLAY_DIRECTORY: 'replay',
687 EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION: 4,
688 MAX_SOCKET_WAITING_DATA: 1024 * 1000 * 100, // 100MB
698 const MEMOIZE_TTL = {
699 OVERVIEWS_SAMPLE: 1000 * 3600 * 4, // 4 hours
700 INFO_HASH_EXISTS: 1000 * 3600 * 12, // 12 hours
701 LIVE_ABLE_TO_UPLOAD: 1000 * 60, // 1 minute
702 LIVE_CHECK_SOCKET_HEALTH: 1000 * 60 // 1 minute
705 const MEMOIZE_LENGTH = {
706 INFO_HASH_EXISTS: 200
709 const QUEUE_CONCURRENCY = {
710 ACTOR_PROCESS_IMAGE: 3
719 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
721 const ASSETS_PATH = {
722 DEFAULT_AUDIO_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-audio-background.jpg'),
723 DEFAULT_LIVE_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-live-background.jpg')
726 // ---------------------------------------------------------------------------
728 const CUSTOM_HTML_TAG_COMMENTS = {
729 TITLE: '<!-- title tag -->',
730 DESCRIPTION: '<!-- description tag -->',
731 CUSTOM_CSS: '<!-- custom css tag -->',
732 META_TAGS: '<!-- meta tags -->',
733 SERVER_CONFIG: '<!-- server config -->'
736 // ---------------------------------------------------------------------------
742 const MAX_LOGS_OUTPUT_CHARACTERS = 10 * 1000 * 1000
743 const LOG_FILENAME = 'peertube.log'
744 const AUDIT_LOG_FILENAME = 'peertube-audit.log'
746 // ---------------------------------------------------------------------------
748 const TRACKER_RATE_LIMITS = {
749 INTERVAL: 60000 * 5, // 5 minutes
750 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
751 ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
752 BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
755 const P2P_MEDIA_LOADER_PEER_VERSION = 2
757 // ---------------------------------------------------------------------------
759 const PLUGIN_GLOBAL_CSS_FILE_NAME = 'plugins-global.css'
760 const PLUGIN_GLOBAL_CSS_PATH = join(CONFIG.STORAGE.TMP_DIR, PLUGIN_GLOBAL_CSS_FILE_NAME)
762 let PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 1000 * 60 * 5 // 5 minutes
764 const DEFAULT_THEME_NAME = 'default'
765 const DEFAULT_USER_THEME_NAME = 'instance-default'
767 // ---------------------------------------------------------------------------
769 const SEARCH_INDEX = {
771 VIDEOS: '/api/v1/search/videos',
772 VIDEO_CHANNELS: '/api/v1/search/video-channels'
776 // ---------------------------------------------------------------------------
778 // Special constants for a test instance
779 if (isTestInstance() === true) {
780 PRIVATE_RSA_KEY_SIZE = 1024
782 ACTOR_FOLLOW_SCORE.BASE = 20
784 REMOTE_SCHEME.HTTP = 'http'
785 REMOTE_SCHEME.WS = 'ws'
787 STATIC_MAX_AGE.SERVER = '0'
789 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
790 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
791 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
792 ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
794 CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max = 100 * 1024 // 100KB
795 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max = 400 * 1024 // 400KB
797 SCHEDULER_INTERVALS_MS.ACTOR_FOLLOW_SCORES = 1000
798 SCHEDULER_INTERVALS_MS.REMOVE_OLD_JOBS = 10000
799 SCHEDULER_INTERVALS_MS.REMOVE_OLD_HISTORY = 5000
800 SCHEDULER_INTERVALS_MS.REMOVE_OLD_VIEWS = 5000
801 SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS = 5000
802 SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES = 5000
803 SCHEDULER_INTERVALS_MS.UPDATE_INBOX_STATS = 5000
804 SCHEDULER_INTERVALS_MS.CHECK_PEERTUBE_VERSION = 2000
806 REPEAT_JOBS['videos-views-stats'] = { every: 5000 }
808 REPEAT_JOBS['activitypub-cleaner'] = { every: 5000 }
809 AP_CLEANER.PERIOD = 5000
811 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
813 VIEW_LIFETIME.VIEWER = 1000 * 5 // 5 second
814 CONTACT_FORM_LIFETIME = 1000 // 1 second
816 JOB_ATTEMPTS['email'] = 1
818 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
819 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 3000
820 MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD = 3000
821 OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD = 2
823 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 5000
825 VIDEO_LIVE.CLEANUP_DELAY = 5000
826 VIDEO_LIVE.SEGMENT_TIME_SECONDS = 2
827 VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION = 1
830 updateWebserverUrls()
831 updateWebserverConfig()
833 registerConfigChangedHandler(() => {
834 updateWebserverUrls()
835 updateWebserverConfig()
838 // ---------------------------------------------------------------------------
840 const FILES_CONTENT_HASH = {
841 MANIFEST: generateContentHash(),
842 FAVICON: generateContentHash(),
843 LOGO: generateContentHash()
846 // ---------------------------------------------------------------------------
855 RESUMABLE_UPLOAD_DIRECTORY,
856 RESUMABLE_UPLOAD_SESSION_LIFETIME,
857 HLS_REDUNDANCY_DIRECTORY,
858 P2P_MEDIA_LOADER_PEER_VERSION,
871 LAST_MIGRATION_VERSION,
873 CUSTOM_HTML_TAG_COMMENTS,
874 BROADCAST_CONCURRENCY,
881 DEFAULT_USER_THEME_NAME,
883 PLUGIN_GLOBAL_CSS_FILE_NAME,
884 PLUGIN_GLOBAL_CSS_PATH,
885 PRIVATE_RSA_KEY_SIZE,
886 ROUTE_CACHE_LIFETIME,
888 HLS_STREAMING_PLAYLIST_DIRECTORY,
895 VIDEO_IMPORT_TIMEOUT,
896 VIDEO_PLAYLIST_TYPES,
897 MAX_LOGS_OUTPUT_CHARACTERS,
899 ACTIVITY_PUB_ACTOR_TYPES,
910 VIDEO_TRANSCODING_FPS,
915 USER_PASSWORD_RESET_LIFETIME,
916 USER_PASSWORD_CREATE_LIFETIME,
918 USER_EMAIL_VERIFY_LIFETIME,
920 SCHEDULER_INTERVALS_MS,
922 STATIC_DOWNLOAD_PATHS,
924 CRAWL_REQUEST_CONCURRENCY,
925 DEFAULT_AUDIO_RESOLUTION,
926 BINARY_CONTENT_TYPES,
927 JOB_COMPLETED_LIFETIME,
931 CONTACT_FORM_LIFETIME,
932 VIDEO_PLAYLIST_PRIVACIES,
933 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
941 // ---------------------------------------------------------------------------
943 function buildVideoMimetypeExt () {
945 // streamable formats that warrant cross-browser compatibility
946 'video/webm': '.webm',
947 // We'll add .ogg if additional extensions are enabled
948 // We could add .ogg here but since it could be an audio file,
949 // it would be confusing for users because PeerTube will refuse their file (based on the mimetype)
950 'video/ogg': [ '.ogv' ],
954 if (CONFIG.TRANSCODING.ENABLED) {
955 if (CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
956 data['video/ogg'].push('.ogg')
958 Object.assign(data, {
959 'video/x-matroska': '.mkv',
961 // Developed by Apple
962 'video/quicktime': [ '.mov', '.qt', '.mqv' ], // often used as output format by editing software
963 'video/x-m4v': '.m4v',
966 // Developed by the Adobe Flash Platform
967 'video/x-flv': '.flv',
968 'video/x-f4v': '.f4v', // replacement for flv
970 // Developed by Microsoft
971 'video/x-ms-wmv': '.wmv',
972 'video/x-msvideo': '.avi',
976 // common video formats for cell phones
977 'video/3gpp': [ '.3gp', '.3gpp' ],
978 'video/3gpp2': [ '.3g2', '.3gpp2' ],
980 // Developed by FFmpeg/Mplayer
981 'application/x-nut': '.nut',
983 // The standard video format used by many Sony and Panasonic HD camcorders.
984 // It is also used for storing high definition video on Blu-ray discs.
985 'video/mp2t': '.mts',
986 'video/m2ts': '.m2ts',
988 // Old formats reliant on MPEG-1/MPEG-2
990 'video/mpeg2': '.m2v',
991 'video/mpeg': [ '.m1v', '.mpg', '.mpe', '.mpeg', '.vob' ],
995 'application/octet-stream': null,
996 'application/mxf': '.mxf' // often used as exchange format by editing software
1000 if (CONFIG.TRANSCODING.ALLOW_AUDIO_FILES) {
1001 Object.assign(data, MIMETYPES.AUDIO.MIMETYPE_EXT)
1008 function updateWebserverUrls () {
1009 WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
1010 WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
1011 WEBSERVER.WS = CONFIG.WEBSERVER.WS
1013 WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME
1014 WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME
1015 WEBSERVER.PORT = CONFIG.WEBSERVER.PORT
1017 WEBSERVER.RTMP_URL = 'rtmp://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.LIVE.RTMP.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1018 WEBSERVER.RTMPS_URL = 'rtmps://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.LIVE.RTMPS.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1021 function updateWebserverConfig () {
1022 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
1023 MIMETYPES.VIDEO.MIMETYPES_REGEX = buildMimetypesRegex(MIMETYPES.VIDEO.MIMETYPE_EXT)
1025 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
1027 MIMETYPES.VIDEO.EXT_MIMETYPE = buildVideoExtMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT)
1029 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = Object.keys(MIMETYPES.VIDEO.EXT_MIMETYPE)
1032 function buildVideoExtMimetype (obj: { [ id: string ]: string | string[] }) {
1033 const result: { [id: string]: string } = {}
1035 for (const mimetype of Object.keys(obj)) {
1036 const value = obj[mimetype]
1037 if (!value) continue
1039 const extensions = Array.isArray(value) ? value : [ value ]
1041 for (const extension of extensions) {
1042 result[extension] = mimetype
1049 function buildMimetypesRegex (obj: { [id: string]: string | string[] }) {
1050 return Object.keys(obj)
1055 function loadLanguages () {
1056 Object.assign(VIDEO_LANGUAGES, buildLanguages())
1059 function buildLanguages () {
1060 const iso639 = require('iso-639-3')
1062 const languages: { [id: string]: string } = {}
1064 const additionalLanguages = {
1065 sgn: true, // Sign languages (macro language)
1066 ase: true, // American sign language
1067 sdl: true, // Arabian sign language
1068 bfi: true, // British sign language
1069 bzs: true, // Brazilian sign language
1070 csl: true, // Chinese sign language
1071 cse: true, // Czech sign language
1072 dsl: true, // Danish sign language
1073 fsl: true, // French sign language
1074 gsg: true, // German sign language
1075 pks: true, // Pakistan sign language
1076 jsl: true, // Japanese sign language
1077 sfs: true, // South African sign language
1078 swl: true, // Swedish sign language
1079 rsl: true, // Russian sign language
1081 kab: true, // Kabyle
1085 epo: true, // Esperanto
1086 tlh: true, // Klingon
1087 jbo: true, // Lojban
1088 avk: true, // Kotava
1090 zxx: true // No linguistic content (ISO-639-2)
1093 // Only add ISO639-1 languages and some sign languages (ISO639-3)
1096 return (l.iso6391 !== undefined && l.type === 'living') ||
1097 additionalLanguages[l.iso6393] === true
1099 .forEach(l => { languages[l.iso6391 || l.iso6393] = l.name })
1101 // Override Occitan label
1102 languages['oc'] = 'Occitan'
1103 languages['el'] = 'Greek'
1108 function generateContentHash () {
1109 return randomBytes(20).toString('hex')