]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/constants.ts
prevent multiple post-process triggering of upload-resumable (#4175)
[github/Chocobozzz/PeerTube.git] / server / initializers / constants.ts
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'
6 import {
7 AbuseState,
8 JobType,
9 VideoImportState,
10 VideoPrivacy,
11 VideoRateType,
12 VideoResolution,
13 VideoState,
14 VideoTranscodingFPS
15 } from '../../shared/models'
16 import { ActivityPubActorType } from '../../shared/models/activitypub'
17 import { FollowState } from '../../shared/models/actors'
18 import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
19 import { VideoPlaylistPrivacy } from '../../shared/models/videos/playlist/video-playlist-privacy.model'
20 import { VideoPlaylistType } from '../../shared/models/videos/playlist/video-playlist-type.model'
21 // Do not use barrels, remain constants as independent as possible
22 import { isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
23 import { CONFIG, registerConfigChangedHandler } from './config'
24
25 // ---------------------------------------------------------------------------
26
27 const LAST_MIGRATION_VERSION = 670
28
29 // ---------------------------------------------------------------------------
30
31 const API_VERSION = 'v1'
32 const PEERTUBE_VERSION: string = require(join(root(), 'package.json')).version
33
34 const PAGINATION = {
35 GLOBAL: {
36 COUNT: {
37 DEFAULT: 15,
38 MAX: 100
39 }
40 },
41 OUTBOX: {
42 COUNT: {
43 MAX: 50
44 }
45 }
46 }
47
48 const WEBSERVER = {
49 URL: '',
50 HOST: '',
51 SCHEME: '',
52 WS: '',
53 HOSTNAME: '',
54 PORT: 0,
55 RTMP_URL: ''
56 }
57
58 // Sortable columns per schema
59 const SORTABLE_COLUMNS = {
60 USERS: [ 'id', 'username', 'videoQuotaUsed', 'createdAt', 'lastLoginDate', 'role' ],
61 USER_SUBSCRIPTIONS: [ 'id', 'createdAt' ],
62 ACCOUNTS: [ 'createdAt' ],
63 JOBS: [ 'createdAt' ],
64 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
65 VIDEO_IMPORTS: [ 'createdAt' ],
66
67 VIDEO_COMMENT_THREADS: [ 'createdAt', 'totalReplies' ],
68 VIDEO_COMMENTS: [ 'createdAt' ],
69
70 VIDEO_RATES: [ 'createdAt' ],
71 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
72
73 INSTANCE_FOLLOWERS: [ 'createdAt', 'state', 'score' ],
74 INSTANCE_FOLLOWING: [ 'createdAt', 'redundancyAllowed', 'state' ],
75 ACCOUNT_FOLLOWERS: [ 'createdAt' ],
76 CHANNEL_FOLLOWERS: [ 'createdAt' ],
77
78 VIDEOS: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'trending', 'hot', 'best' ],
79
80 // Don't forget to update peertube-search-index with the same values
81 VIDEOS_SEARCH: [ 'name', 'duration', 'createdAt', 'publishedAt', 'originallyPublishedAt', 'views', 'likes', 'match' ],
82 VIDEO_CHANNELS_SEARCH: [ 'match', 'displayName', 'createdAt' ],
83 VIDEO_PLAYLISTS_SEARCH: [ 'match', 'displayName', 'createdAt' ],
84
85 ABUSES: [ 'id', 'createdAt', 'state' ],
86
87 ACCOUNTS_BLOCKLIST: [ 'createdAt' ],
88 SERVERS_BLOCKLIST: [ 'createdAt' ],
89
90 USER_NOTIFICATIONS: [ 'createdAt', 'read' ],
91
92 VIDEO_PLAYLISTS: [ 'name', 'displayName', 'createdAt', 'updatedAt' ],
93
94 PLUGINS: [ 'name', 'createdAt', 'updatedAt' ],
95
96 AVAILABLE_PLUGINS: [ 'npmName', 'popularity' ],
97
98 VIDEO_REDUNDANCIES: [ 'name' ]
99 }
100
101 const OAUTH_LIFETIME = {
102 ACCESS_TOKEN: 3600 * 24, // 1 day, for upload
103 REFRESH_TOKEN: 1209600 // 2 weeks
104 }
105
106 const ROUTE_CACHE_LIFETIME = {
107 FEEDS: '15 minutes',
108 ROBOTS: '2 hours',
109 SITEMAP: '1 day',
110 SECURITYTXT: '2 hours',
111 NODEINFO: '10 minutes',
112 DNT_POLICY: '1 week',
113 ACTIVITY_PUB: {
114 VIDEOS: '1 second' // 1 second, cache concurrent requests after a broadcast for example
115 },
116 STATS: '4 hours'
117 }
118
119 // ---------------------------------------------------------------------------
120
121 // Number of points we add/remove after a successful/bad request
122 const ACTOR_FOLLOW_SCORE = {
123 PENALTY: -10,
124 BONUS: 10,
125 BASE: 1000,
126 MAX: 10000
127 }
128
129 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
130 PENDING: 'pending',
131 ACCEPTED: 'accepted'
132 }
133
134 const REMOTE_SCHEME = {
135 HTTP: 'https',
136 WS: 'wss'
137 }
138
139 const JOB_ATTEMPTS: { [id in JobType]: number } = {
140 'activitypub-http-broadcast': 1,
141 'activitypub-http-unicast': 1,
142 'activitypub-http-fetcher': 2,
143 'activitypub-follow': 5,
144 'activitypub-cleaner': 1,
145 'video-file-import': 1,
146 'video-transcoding': 1,
147 'video-import': 1,
148 'email': 5,
149 'actor-keys': 3,
150 'videos-views': 1,
151 'activitypub-refresher': 1,
152 'video-redundancy': 1,
153 'video-live-ending': 1,
154 'move-to-object-storage': 3
155 }
156 // Excluded keys are jobs that can be configured by admins
157 const JOB_CONCURRENCY: { [id in Exclude<JobType, 'video-transcoding' | 'video-import'>]: number } = {
158 'activitypub-http-broadcast': 1,
159 'activitypub-http-unicast': 10,
160 'activitypub-http-fetcher': 3,
161 'activitypub-cleaner': 1,
162 'activitypub-follow': 1,
163 'video-file-import': 1,
164 'email': 5,
165 'actor-keys': 1,
166 'videos-views': 1,
167 'activitypub-refresher': 1,
168 'video-redundancy': 1,
169 'video-live-ending': 10,
170 'move-to-object-storage': 1
171 }
172 const JOB_TTL: { [id in JobType]: number } = {
173 'activitypub-http-broadcast': 60000 * 10, // 10 minutes
174 'activitypub-http-unicast': 60000 * 10, // 10 minutes
175 'activitypub-http-fetcher': 1000 * 3600 * 10, // 10 hours
176 'activitypub-follow': 60000 * 10, // 10 minutes
177 'activitypub-cleaner': 1000 * 3600, // 1 hour
178 'video-file-import': 1000 * 3600, // 1 hour
179 'video-transcoding': 1000 * 3600 * 48, // 2 days, transcoding could be long
180 'video-import': 1000 * 3600 * 2, // 2 hours
181 'email': 60000 * 10, // 10 minutes
182 'actor-keys': 60000 * 20, // 20 minutes
183 'videos-views': undefined, // Unlimited
184 'activitypub-refresher': 60000 * 10, // 10 minutes
185 'video-redundancy': 1000 * 3600 * 3, // 3 hours
186 'video-live-ending': 1000 * 60 * 10, // 10 minutes
187 'move-to-object-storage': 1000 * 60 * 60 * 3 // 3 hours
188 }
189 const REPEAT_JOBS: { [ id: string ]: EveryRepeatOptions | CronRepeatOptions } = {
190 'videos-views': {
191 cron: randomInt(1, 20) + ' * * * *' // Between 1-20 minutes past the hour
192 },
193 'activitypub-cleaner': {
194 cron: '30 5 * * ' + randomInt(0, 7) // 1 time per week (random day) at 5:30 AM
195 }
196 }
197 const JOB_PRIORITY = {
198 TRANSCODING: 100
199 }
200
201 const BROADCAST_CONCURRENCY = 30 // How many requests in parallel we do in activitypub-http-broadcast job
202 const AP_CLEANER_CONCURRENCY = 10 // How many requests in parallel we do in activitypub-cleaner job
203 const CRAWL_REQUEST_CONCURRENCY = 1 // How many requests in parallel to fetch remote data (likes, shares...)
204 const REQUEST_TIMEOUT = 7000 // 7 seconds
205 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days
206 const VIDEO_IMPORT_TIMEOUT = 1000 * 3600 // 1 hour
207
208 const SCHEDULER_INTERVALS_MS = {
209 ACTOR_FOLLOW_SCORES: 60000 * 60, // 1 hour
210 REMOVE_OLD_JOBS: 60000 * 60, // 1 hour
211 UPDATE_VIDEOS: 60000, // 1 minute
212 YOUTUBE_DL_UPDATE: 60000 * 60 * 24, // 1 day
213 CHECK_PLUGINS: CONFIG.PLUGINS.INDEX.CHECK_LATEST_VERSIONS_INTERVAL,
214 CHECK_PEERTUBE_VERSION: 60000 * 60 * 24, // 1 day
215 AUTO_FOLLOW_INDEX_INSTANCES: 60000 * 60 * 24, // 1 day
216 REMOVE_OLD_VIEWS: 60000 * 60 * 24, // 1 day
217 REMOVE_OLD_HISTORY: 60000 * 60 * 24, // 1 day
218 UPDATE_INBOX_STATS: 1000 * 60, // 1 minute
219 REMOVE_DANGLING_RESUMABLE_UPLOADS: 60000 * 60 * 16 // 16 hours
220 }
221
222 // ---------------------------------------------------------------------------
223
224 const CONSTRAINTS_FIELDS = {
225 USERS: {
226 NAME: { min: 1, max: 120 }, // Length
227 DESCRIPTION: { min: 3, max: 1000 }, // Length
228 USERNAME: { min: 1, max: 50 }, // Length
229 PASSWORD: { min: 6, max: 255 }, // Length
230 VIDEO_QUOTA: { min: -1 },
231 VIDEO_QUOTA_DAILY: { min: -1 },
232 VIDEO_LANGUAGES: { max: 500 }, // Array length
233 BLOCKED_REASON: { min: 3, max: 250 } // Length
234 },
235 ABUSES: {
236 REASON: { min: 2, max: 3000 }, // Length
237 MODERATION_COMMENT: { min: 2, max: 3000 } // Length
238 },
239 ABUSE_MESSAGES: {
240 MESSAGE: { min: 2, max: 3000 } // Length
241 },
242 VIDEO_BLACKLIST: {
243 REASON: { min: 2, max: 300 } // Length
244 },
245 VIDEO_CHANNELS: {
246 NAME: { min: 1, max: 120 }, // Length
247 DESCRIPTION: { min: 3, max: 1000 }, // Length
248 SUPPORT: { min: 3, max: 1000 }, // Length
249 URL: { min: 3, max: 2000 } // Length
250 },
251 VIDEO_CAPTIONS: {
252 CAPTION_FILE: {
253 EXTNAME: [ '.vtt', '.srt' ],
254 FILE_SIZE: {
255 max: 4 * 1024 * 1024 // 4MB
256 }
257 }
258 },
259 VIDEO_IMPORTS: {
260 URL: { min: 3, max: 2000 }, // Length
261 TORRENT_NAME: { min: 3, max: 255 }, // Length
262 TORRENT_FILE: {
263 EXTNAME: [ '.torrent' ],
264 FILE_SIZE: {
265 max: 1024 * 200 // 200 KB
266 }
267 }
268 },
269 VIDEOS_REDUNDANCY: {
270 URL: { min: 3, max: 2000 } // Length
271 },
272 VIDEO_RATES: {
273 URL: { min: 3, max: 2000 } // Length
274 },
275 VIDEOS: {
276 NAME: { min: 3, max: 120 }, // Length
277 LANGUAGE: { min: 1, max: 10 }, // Length
278 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
279 DESCRIPTION: { min: 3, max: 10000 }, // Length
280 SUPPORT: { min: 3, max: 1000 }, // Length
281 IMAGE: {
282 EXTNAME: [ '.png', '.jpg', '.jpeg', '.webp' ],
283 FILE_SIZE: {
284 max: 4 * 1024 * 1024 // 4MB
285 }
286 },
287 EXTNAME: [] as string[],
288 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
289 DURATION: { min: 0 }, // Number
290 TAGS: { min: 0, max: 5 }, // Number of total tags
291 TAG: { min: 2, max: 30 }, // Length
292 VIEWS: { min: 0 },
293 LIKES: { min: 0 },
294 DISLIKES: { min: 0 },
295 FILE_SIZE: { min: -1 },
296 PARTIAL_UPLOAD_SIZE: { max: 50 * 1024 * 1024 * 1024 }, // 50GB
297 URL: { min: 3, max: 2000 } // Length
298 },
299 VIDEO_PLAYLISTS: {
300 NAME: { min: 1, max: 120 }, // Length
301 DESCRIPTION: { min: 3, max: 1000 }, // Length
302 URL: { min: 3, max: 2000 }, // Length
303 IMAGE: {
304 EXTNAME: [ '.jpg', '.jpeg' ],
305 FILE_SIZE: {
306 max: 4 * 1024 * 1024 // 4MB
307 }
308 }
309 },
310 ACTORS: {
311 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
312 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
313 URL: { min: 3, max: 2000 }, // Length
314 IMAGE: {
315 EXTNAME: [ '.png', '.jpeg', '.jpg', '.gif', '.webp' ],
316 FILE_SIZE: {
317 max: 4 * 1024 * 1024 // 4MB
318 }
319 }
320 },
321 VIDEO_EVENTS: {
322 COUNT: { min: 0 }
323 },
324 VIDEO_COMMENTS: {
325 TEXT: { min: 1, max: 10000 }, // Length
326 URL: { min: 3, max: 2000 } // Length
327 },
328 VIDEO_SHARE: {
329 URL: { min: 3, max: 2000 } // Length
330 },
331 CONTACT_FORM: {
332 FROM_NAME: { min: 1, max: 120 }, // Length
333 BODY: { min: 3, max: 5000 } // Length
334 },
335 PLUGINS: {
336 NAME: { min: 1, max: 214 }, // Length
337 DESCRIPTION: { min: 1, max: 20000 } // Length
338 },
339 COMMONS: {
340 URL: { min: 5, max: 2000 } // Length
341 }
342 }
343
344 const VIEW_LIFETIME = {
345 VIDEO: 60000 * 60, // 1 hour
346 LIVE: 60000 * 5 // 5 minutes
347 }
348
349 let CONTACT_FORM_LIFETIME = 60000 * 60 // 1 hour
350
351 const VIDEO_TRANSCODING_FPS: VideoTranscodingFPS = {
352 MIN: 1,
353 STANDARD: [ 24, 25, 30 ],
354 HD_STANDARD: [ 50, 60 ],
355 AVERAGE: 30,
356 MAX: 60,
357 KEEP_ORIGIN_FPS_RESOLUTION_MIN: 720 // We keep the original FPS on high resolutions (720 minimum)
358 }
359
360 const DEFAULT_AUDIO_RESOLUTION = VideoResolution.H_480P
361
362 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
363 LIKE: 'like',
364 DISLIKE: 'dislike'
365 }
366
367 const FFMPEG_NICE: { [ id: string ]: number } = {
368 // parent process defaults to niceness = 0
369 // reminder: lower = higher priority, max value is 19, lowest is -20
370 LIVE: 5, // prioritize over VOD and THUMBNAIL
371 THUMBNAIL: 10,
372 VOD: 15
373 }
374
375 const VIDEO_CATEGORIES = {
376 1: 'Music',
377 2: 'Films',
378 3: 'Vehicles',
379 4: 'Art',
380 5: 'Sports',
381 6: 'Travels',
382 7: 'Gaming',
383 8: 'People',
384 9: 'Comedy',
385 10: 'Entertainment',
386 11: 'News & Politics',
387 12: 'How To',
388 13: 'Education',
389 14: 'Activism',
390 15: 'Science & Technology',
391 16: 'Animals',
392 17: 'Kids',
393 18: 'Food'
394 }
395
396 // See https://creativecommons.org/licenses/?lang=en
397 const VIDEO_LICENCES = {
398 1: 'Attribution',
399 2: 'Attribution - Share Alike',
400 3: 'Attribution - No Derivatives',
401 4: 'Attribution - Non Commercial',
402 5: 'Attribution - Non Commercial - Share Alike',
403 6: 'Attribution - Non Commercial - No Derivatives',
404 7: 'Public Domain Dedication'
405 }
406
407 const VIDEO_LANGUAGES: { [id: string]: string } = {}
408
409 const VIDEO_PRIVACIES: { [ id in VideoPrivacy ]: string } = {
410 [VideoPrivacy.PUBLIC]: 'Public',
411 [VideoPrivacy.UNLISTED]: 'Unlisted',
412 [VideoPrivacy.PRIVATE]: 'Private',
413 [VideoPrivacy.INTERNAL]: 'Internal'
414 }
415
416 const VIDEO_STATES: { [ id in VideoState ]: string } = {
417 [VideoState.PUBLISHED]: 'Published',
418 [VideoState.TO_TRANSCODE]: 'To transcode',
419 [VideoState.TO_IMPORT]: 'To import',
420 [VideoState.WAITING_FOR_LIVE]: 'Waiting for livestream',
421 [VideoState.LIVE_ENDED]: 'Livestream ended',
422 [VideoState.TO_MOVE_TO_EXTERNAL_STORAGE]: 'To move to an external storage'
423 }
424
425 const VIDEO_IMPORT_STATES: { [ id in VideoImportState ]: string } = {
426 [VideoImportState.FAILED]: 'Failed',
427 [VideoImportState.PENDING]: 'Pending',
428 [VideoImportState.SUCCESS]: 'Success',
429 [VideoImportState.REJECTED]: 'Rejected'
430 }
431
432 const ABUSE_STATES: { [ id in AbuseState ]: string } = {
433 [AbuseState.PENDING]: 'Pending',
434 [AbuseState.REJECTED]: 'Rejected',
435 [AbuseState.ACCEPTED]: 'Accepted'
436 }
437
438 const VIDEO_PLAYLIST_PRIVACIES: { [ id in VideoPlaylistPrivacy ]: string } = {
439 [VideoPlaylistPrivacy.PUBLIC]: 'Public',
440 [VideoPlaylistPrivacy.UNLISTED]: 'Unlisted',
441 [VideoPlaylistPrivacy.PRIVATE]: 'Private'
442 }
443
444 const VIDEO_PLAYLIST_TYPES: { [ id in VideoPlaylistType ]: string } = {
445 [VideoPlaylistType.REGULAR]: 'Regular',
446 [VideoPlaylistType.WATCH_LATER]: 'Watch later'
447 }
448
449 const MIMETYPES = {
450 AUDIO: {
451 MIMETYPE_EXT: {
452 'audio/mpeg': '.mp3',
453 'audio/mp3': '.mp3',
454 'application/ogg': '.ogg',
455 'audio/ogg': '.ogg',
456 'audio/x-ms-wma': '.wma',
457 'audio/wav': '.wav',
458 'audio/x-wav': '.wav',
459 'audio/x-flac': '.flac',
460 'audio/flac': '.flac',
461 'audio/aac': '.aac',
462 'audio/m4a': '.m4a',
463 'audio/mp4': '.m4a',
464 'audio/x-m4a': '.m4a',
465 'audio/ac3': '.ac3'
466 },
467 EXT_MIMETYPE: null as { [ id: string ]: string }
468 },
469 VIDEO: {
470 MIMETYPE_EXT: null as { [ id: string ]: string | string[] },
471 MIMETYPES_REGEX: null as string,
472 EXT_MIMETYPE: null as { [ id: string ]: string }
473 },
474 IMAGE: {
475 MIMETYPE_EXT: {
476 'image/png': '.png',
477 'image/gif': '.gif',
478 'image/webp': '.webp',
479 'image/jpg': '.jpg',
480 'image/jpeg': '.jpg'
481 },
482 EXT_MIMETYPE: null as { [ id: string ]: string }
483 },
484 VIDEO_CAPTIONS: {
485 MIMETYPE_EXT: {
486 'text/vtt': '.vtt',
487 'application/x-subrip': '.srt',
488 'text/plain': '.srt'
489 }
490 },
491 TORRENT: {
492 MIMETYPE_EXT: {
493 'application/x-bittorrent': '.torrent'
494 }
495 }
496 }
497 MIMETYPES.AUDIO.EXT_MIMETYPE = invert(MIMETYPES.AUDIO.MIMETYPE_EXT)
498 MIMETYPES.IMAGE.EXT_MIMETYPE = invert(MIMETYPES.IMAGE.MIMETYPE_EXT)
499
500 const BINARY_CONTENT_TYPES = new Set([
501 'binary/octet-stream',
502 'application/octet-stream',
503 'application/x-binary'
504 ])
505
506 // ---------------------------------------------------------------------------
507
508 const OVERVIEWS = {
509 VIDEOS: {
510 SAMPLE_THRESHOLD: 6,
511 SAMPLES_COUNT: 20
512 }
513 }
514
515 const VIDEO_CHANNELS = {
516 MAX_PER_USER: 20
517 }
518
519 // ---------------------------------------------------------------------------
520
521 const SERVER_ACTOR_NAME = 'peertube'
522
523 const ACTIVITY_PUB = {
524 POTENTIAL_ACCEPT_HEADERS: [
525 'application/activity+json',
526 'application/ld+json',
527 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
528 ],
529 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
530 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
531 COLLECTION_ITEMS_PER_PAGE: 10,
532 FETCH_PAGE_LIMIT: 2000,
533 URL_MIME_TYPES: {
534 VIDEO: [] as string[],
535 TORRENT: [ 'application/x-bittorrent' ],
536 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
537 },
538 MAX_RECURSION_COMMENTS: 100,
539 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
540 VIDEO_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2, // 2 days
541 VIDEO_PLAYLIST_REFRESH_INTERVAL: 3600 * 24 * 1000 * 2 // 2 days
542 }
543
544 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
545 GROUP: 'Group',
546 PERSON: 'Person',
547 APPLICATION: 'Application',
548 ORGANIZATION: 'Organization',
549 SERVICE: 'Service'
550 }
551
552 const HTTP_SIGNATURE = {
553 HEADER_NAME: 'signature',
554 ALGORITHM: 'rsa-sha256',
555 HEADERS_TO_SIGN: [ '(request-target)', 'host', 'date', 'digest' ],
556 REQUIRED_HEADERS: {
557 ALL: [ '(request-target)', 'host', 'date' ],
558 POST: [ '(request-target)', 'host', 'date', 'digest' ]
559 },
560 CLOCK_SKEW_SECONDS: 1800
561 }
562
563 // ---------------------------------------------------------------------------
564
565 let PRIVATE_RSA_KEY_SIZE = 2048
566
567 // Password encryption
568 const BCRYPT_SALT_SIZE = 10
569
570 const USER_PASSWORD_RESET_LIFETIME = 60000 * 60 // 60 minutes
571 const USER_PASSWORD_CREATE_LIFETIME = 60000 * 60 * 24 * 7 // 7 days
572
573 const USER_EMAIL_VERIFY_LIFETIME = 60000 * 60 // 60 minutes
574
575 const NSFW_POLICY_TYPES: { [ id: string ]: NSFWPolicyType } = {
576 DO_NOT_LIST: 'do_not_list',
577 BLUR: 'blur',
578 DISPLAY: 'display'
579 }
580
581 // ---------------------------------------------------------------------------
582
583 // Express static paths (router)
584 const STATIC_PATHS = {
585 THUMBNAILS: '/static/thumbnails/',
586 WEBSEED: '/static/webseed/',
587 REDUNDANCY: '/static/redundancy/',
588 STREAMING_PLAYLISTS: {
589 HLS: '/static/streaming-playlists/hls'
590 }
591 }
592 const STATIC_DOWNLOAD_PATHS = {
593 TORRENTS: '/download/torrents/',
594 VIDEOS: '/download/videos/',
595 HLS_VIDEOS: '/download/streaming-playlists/hls/videos/'
596 }
597 const LAZY_STATIC_PATHS = {
598 BANNERS: '/lazy-static/banners/',
599 AVATARS: '/lazy-static/avatars/',
600 PREVIEWS: '/lazy-static/previews/',
601 VIDEO_CAPTIONS: '/lazy-static/video-captions/',
602 TORRENTS: '/lazy-static/torrents/'
603 }
604
605 // Cache control
606 const STATIC_MAX_AGE = {
607 SERVER: '2h',
608 LAZY_SERVER: '2d',
609 CLIENT: '30d'
610 }
611
612 // Videos thumbnail size
613 const THUMBNAILS_SIZE = {
614 width: 280,
615 height: 157,
616 minWidth: 150
617 }
618 const PREVIEWS_SIZE = {
619 width: 850,
620 height: 480,
621 minWidth: 400
622 }
623 const ACTOR_IMAGES_SIZE = {
624 AVATARS: {
625 width: 120,
626 height: 120
627 },
628 BANNERS: {
629 width: 1920,
630 height: 317 // 6/1 ratio
631 }
632 }
633
634 const EMBED_SIZE = {
635 width: 560,
636 height: 315
637 }
638
639 // Sub folders of cache directory
640 const FILES_CACHE = {
641 PREVIEWS: {
642 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'previews'),
643 MAX_AGE: 1000 * 3600 * 3 // 3 hours
644 },
645 VIDEO_CAPTIONS: {
646 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'video-captions'),
647 MAX_AGE: 1000 * 3600 * 3 // 3 hours
648 },
649 TORRENTS: {
650 DIRECTORY: join(CONFIG.STORAGE.CACHE_DIR, 'torrents'),
651 MAX_AGE: 1000 * 3600 * 3 // 3 hours
652 }
653 }
654
655 const LRU_CACHE = {
656 USER_TOKENS: {
657 MAX_SIZE: 1000
658 },
659 ACTOR_IMAGE_STATIC: {
660 MAX_SIZE: 500
661 }
662 }
663
664 const RESUMABLE_UPLOAD_DIRECTORY = join(CONFIG.STORAGE.TMP_DIR, 'resumable-uploads')
665 const HLS_STREAMING_PLAYLIST_DIRECTORY = join(CONFIG.STORAGE.STREAMING_PLAYLISTS_DIR, 'hls')
666 const HLS_REDUNDANCY_DIRECTORY = join(CONFIG.STORAGE.REDUNDANCY_DIR, 'hls')
667
668 const RESUMABLE_UPLOAD_SESSION_LIFETIME = SCHEDULER_INTERVALS_MS.REMOVE_DANGLING_RESUMABLE_UPLOADS
669
670 const VIDEO_LIVE = {
671 EXTENSION: '.ts',
672 CLEANUP_DELAY: 1000 * 60 * 5, // 5 minutes
673 SEGMENT_TIME_SECONDS: 4, // 4 seconds
674 SEGMENTS_LIST_SIZE: 15, // 15 maximum segments in live playlist
675 REPLAY_DIRECTORY: 'replay',
676 EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION: 4,
677 MAX_SOCKET_WAITING_DATA: 1024 * 1000 * 100, // 100MB
678 RTMP: {
679 CHUNK_SIZE: 60000,
680 GOP_CACHE: true,
681 PING: 60,
682 PING_TIMEOUT: 30,
683 BASE_PATH: 'live'
684 }
685 }
686
687 const MEMOIZE_TTL = {
688 OVERVIEWS_SAMPLE: 1000 * 3600 * 4, // 4 hours
689 INFO_HASH_EXISTS: 1000 * 3600 * 12, // 12 hours
690 LIVE_ABLE_TO_UPLOAD: 1000 * 60, // 1 minute
691 LIVE_CHECK_SOCKET_HEALTH: 1000 * 60 // 1 minute
692 }
693
694 const MEMOIZE_LENGTH = {
695 INFO_HASH_EXISTS: 200
696 }
697
698 const QUEUE_CONCURRENCY = {
699 ACTOR_PROCESS_IMAGE: 3
700 }
701
702 const REDUNDANCY = {
703 VIDEOS: {
704 RANDOMIZED_FACTOR: 5
705 }
706 }
707
708 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
709
710 const ASSETS_PATH = {
711 DEFAULT_AUDIO_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-audio-background.jpg'),
712 DEFAULT_LIVE_BACKGROUND: join(root(), 'dist', 'server', 'assets', 'default-live-background.jpg')
713 }
714
715 // ---------------------------------------------------------------------------
716
717 const CUSTOM_HTML_TAG_COMMENTS = {
718 TITLE: '<!-- title tag -->',
719 DESCRIPTION: '<!-- description tag -->',
720 CUSTOM_CSS: '<!-- custom css tag -->',
721 META_TAGS: '<!-- meta tags -->',
722 SERVER_CONFIG: '<!-- server config -->'
723 }
724
725 // ---------------------------------------------------------------------------
726
727 const FEEDS = {
728 COUNT: 20
729 }
730
731 const MAX_LOGS_OUTPUT_CHARACTERS = 10 * 1000 * 1000
732 const LOG_FILENAME = 'peertube.log'
733 const AUDIT_LOG_FILENAME = 'peertube-audit.log'
734
735 // ---------------------------------------------------------------------------
736
737 const TRACKER_RATE_LIMITS = {
738 INTERVAL: 60000 * 5, // 5 minutes
739 ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
740 ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
741 BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
742 }
743
744 const P2P_MEDIA_LOADER_PEER_VERSION = 2
745
746 // ---------------------------------------------------------------------------
747
748 const PLUGIN_GLOBAL_CSS_FILE_NAME = 'plugins-global.css'
749 const PLUGIN_GLOBAL_CSS_PATH = join(CONFIG.STORAGE.TMP_DIR, PLUGIN_GLOBAL_CSS_FILE_NAME)
750
751 let PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 1000 * 60 * 5 // 5 minutes
752
753 const DEFAULT_THEME_NAME = 'default'
754 const DEFAULT_USER_THEME_NAME = 'instance-default'
755
756 // ---------------------------------------------------------------------------
757
758 const SEARCH_INDEX = {
759 ROUTES: {
760 VIDEOS: '/api/v1/search/videos',
761 VIDEO_CHANNELS: '/api/v1/search/video-channels'
762 }
763 }
764
765 // ---------------------------------------------------------------------------
766
767 // Special constants for a test instance
768 if (isTestInstance() === true) {
769 PRIVATE_RSA_KEY_SIZE = 1024
770
771 ACTOR_FOLLOW_SCORE.BASE = 20
772
773 REMOTE_SCHEME.HTTP = 'http'
774 REMOTE_SCHEME.WS = 'ws'
775
776 STATIC_MAX_AGE.SERVER = '0'
777
778 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
779 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
780 ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
781 ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
782
783 CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max = 100 * 1024 // 100KB
784 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max = 400 * 1024 // 400KB
785
786 SCHEDULER_INTERVALS_MS.ACTOR_FOLLOW_SCORES = 1000
787 SCHEDULER_INTERVALS_MS.REMOVE_OLD_JOBS = 10000
788 SCHEDULER_INTERVALS_MS.REMOVE_OLD_HISTORY = 5000
789 SCHEDULER_INTERVALS_MS.REMOVE_OLD_VIEWS = 5000
790 SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS = 5000
791 SCHEDULER_INTERVALS_MS.AUTO_FOLLOW_INDEX_INSTANCES = 5000
792 SCHEDULER_INTERVALS_MS.UPDATE_INBOX_STATS = 5000
793 SCHEDULER_INTERVALS_MS.CHECK_PEERTUBE_VERSION = 2000
794 REPEAT_JOBS['videos-views'] = { every: 5000 }
795 REPEAT_JOBS['activitypub-cleaner'] = { every: 5000 }
796
797 REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
798
799 VIEW_LIFETIME.VIDEO = 1000 // 1 second
800 VIEW_LIFETIME.LIVE = 1000 * 5 // 5 second
801 CONTACT_FORM_LIFETIME = 1000 // 1 second
802
803 JOB_ATTEMPTS['email'] = 1
804
805 FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE = 3000
806 MEMOIZE_TTL.OVERVIEWS_SAMPLE = 3000
807 MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD = 3000
808 OVERVIEWS.VIDEOS.SAMPLE_THRESHOLD = 2
809
810 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME = 5000
811
812 VIDEO_LIVE.CLEANUP_DELAY = 5000
813 VIDEO_LIVE.SEGMENT_TIME_SECONDS = 2
814 VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION = 1
815 }
816
817 updateWebserverUrls()
818 updateWebserverConfig()
819
820 registerConfigChangedHandler(() => {
821 updateWebserverUrls()
822 updateWebserverConfig()
823 })
824
825 // ---------------------------------------------------------------------------
826
827 const FILES_CONTENT_HASH = {
828 MANIFEST: generateContentHash(),
829 FAVICON: generateContentHash(),
830 LOGO: generateContentHash()
831 }
832
833 // ---------------------------------------------------------------------------
834
835 export {
836 WEBSERVER,
837 API_VERSION,
838 VIDEO_LIVE,
839 PEERTUBE_VERSION,
840 LAZY_STATIC_PATHS,
841 SEARCH_INDEX,
842 RESUMABLE_UPLOAD_DIRECTORY,
843 RESUMABLE_UPLOAD_SESSION_LIFETIME,
844 HLS_REDUNDANCY_DIRECTORY,
845 P2P_MEDIA_LOADER_PEER_VERSION,
846 ACTOR_IMAGES_SIZE,
847 ACCEPT_HEADERS,
848 BCRYPT_SALT_SIZE,
849 TRACKER_RATE_LIMITS,
850 FILES_CACHE,
851 LOG_FILENAME,
852 CONSTRAINTS_FIELDS,
853 EMBED_SIZE,
854 REDUNDANCY,
855 JOB_CONCURRENCY,
856 JOB_ATTEMPTS,
857 AP_CLEANER_CONCURRENCY,
858 LAST_MIGRATION_VERSION,
859 OAUTH_LIFETIME,
860 CUSTOM_HTML_TAG_COMMENTS,
861 BROADCAST_CONCURRENCY,
862 AUDIT_LOG_FILENAME,
863 PAGINATION,
864 ACTOR_FOLLOW_SCORE,
865 PREVIEWS_SIZE,
866 REMOTE_SCHEME,
867 FOLLOW_STATES,
868 DEFAULT_USER_THEME_NAME,
869 SERVER_ACTOR_NAME,
870 PLUGIN_GLOBAL_CSS_FILE_NAME,
871 PLUGIN_GLOBAL_CSS_PATH,
872 PRIVATE_RSA_KEY_SIZE,
873 ROUTE_CACHE_LIFETIME,
874 SORTABLE_COLUMNS,
875 HLS_STREAMING_PLAYLIST_DIRECTORY,
876 FEEDS,
877 JOB_TTL,
878 DEFAULT_THEME_NAME,
879 NSFW_POLICY_TYPES,
880 STATIC_MAX_AGE,
881 STATIC_PATHS,
882 VIDEO_IMPORT_TIMEOUT,
883 VIDEO_PLAYLIST_TYPES,
884 MAX_LOGS_OUTPUT_CHARACTERS,
885 ACTIVITY_PUB,
886 ACTIVITY_PUB_ACTOR_TYPES,
887 THUMBNAILS_SIZE,
888 VIDEO_CATEGORIES,
889 MEMOIZE_LENGTH,
890 VIDEO_LANGUAGES,
891 VIDEO_PRIVACIES,
892 VIDEO_LICENCES,
893 VIDEO_STATES,
894 QUEUE_CONCURRENCY,
895 VIDEO_RATE_TYPES,
896 JOB_PRIORITY,
897 VIDEO_TRANSCODING_FPS,
898 FFMPEG_NICE,
899 ABUSE_STATES,
900 VIDEO_CHANNELS,
901 LRU_CACHE,
902 REQUEST_TIMEOUT,
903 USER_PASSWORD_RESET_LIFETIME,
904 USER_PASSWORD_CREATE_LIFETIME,
905 MEMOIZE_TTL,
906 USER_EMAIL_VERIFY_LIFETIME,
907 OVERVIEWS,
908 SCHEDULER_INTERVALS_MS,
909 REPEAT_JOBS,
910 STATIC_DOWNLOAD_PATHS,
911 MIMETYPES,
912 CRAWL_REQUEST_CONCURRENCY,
913 DEFAULT_AUDIO_RESOLUTION,
914 BINARY_CONTENT_TYPES,
915 JOB_COMPLETED_LIFETIME,
916 HTTP_SIGNATURE,
917 VIDEO_IMPORT_STATES,
918 VIEW_LIFETIME,
919 CONTACT_FORM_LIFETIME,
920 VIDEO_PLAYLIST_PRIVACIES,
921 PLUGIN_EXTERNAL_AUTH_TOKEN_LIFETIME,
922 ASSETS_PATH,
923 FILES_CONTENT_HASH,
924 loadLanguages,
925 buildLanguages,
926 generateContentHash
927 }
928
929 // ---------------------------------------------------------------------------
930
931 function buildVideoMimetypeExt () {
932 const data = {
933 // streamable formats that warrant cross-browser compatibility
934 'video/webm': '.webm',
935 // We'll add .ogg if additional extensions are enabled
936 // We could add .ogg here but since it could be an audio file,
937 // it would be confusing for users because PeerTube will refuse their file (based on the mimetype)
938 'video/ogg': [ '.ogv' ],
939 'video/mp4': '.mp4'
940 }
941
942 if (CONFIG.TRANSCODING.ENABLED) {
943 if (CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS) {
944 data['video/ogg'].push('.ogg')
945
946 Object.assign(data, {
947 'video/x-matroska': '.mkv',
948
949 // Developed by Apple
950 'video/quicktime': [ '.mov', '.qt', '.mqv' ], // often used as output format by editing software
951 'video/x-m4v': '.m4v',
952 'video/m4v': '.m4v',
953
954 // Developed by the Adobe Flash Platform
955 'video/x-flv': '.flv',
956 'video/x-f4v': '.f4v', // replacement for flv
957
958 // Developed by Microsoft
959 'video/x-ms-wmv': '.wmv',
960 'video/x-msvideo': '.avi',
961 'video/avi': '.avi',
962
963 // Developed by 3GPP
964 // common video formats for cell phones
965 'video/3gpp': [ '.3gp', '.3gpp' ],
966 'video/3gpp2': [ '.3g2', '.3gpp2' ],
967
968 // Developed by FFmpeg/Mplayer
969 'application/x-nut': '.nut',
970
971 // The standard video format used by many Sony and Panasonic HD camcorders.
972 // It is also used for storing high definition video on Blu-ray discs.
973 'video/mp2t': '.mts',
974 'video/m2ts': '.m2ts',
975
976 // Old formats reliant on MPEG-1/MPEG-2
977 'video/mpv': '.mpv',
978 'video/mpeg2': '.m2v',
979 'video/mpeg': [ '.m1v', '.mpg', '.mpe', '.mpeg', '.vob' ],
980 'video/dvd': '.vob',
981
982 // Could be anything
983 'application/octet-stream': null,
984 'application/mxf': '.mxf' // often used as exchange format by editing software
985 })
986 }
987
988 if (CONFIG.TRANSCODING.ALLOW_AUDIO_FILES) {
989 Object.assign(data, MIMETYPES.AUDIO.MIMETYPE_EXT)
990 }
991 }
992
993 return data
994 }
995
996 function updateWebserverUrls () {
997 WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
998 WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
999 WEBSERVER.WS = CONFIG.WEBSERVER.WS
1000
1001 WEBSERVER.SCHEME = CONFIG.WEBSERVER.SCHEME
1002 WEBSERVER.HOSTNAME = CONFIG.WEBSERVER.HOSTNAME
1003 WEBSERVER.PORT = CONFIG.WEBSERVER.PORT
1004
1005 WEBSERVER.RTMP_URL = 'rtmp://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.LIVE.RTMP.PORT + '/' + VIDEO_LIVE.RTMP.BASE_PATH
1006 }
1007
1008 function updateWebserverConfig () {
1009 MIMETYPES.VIDEO.MIMETYPE_EXT = buildVideoMimetypeExt()
1010 MIMETYPES.VIDEO.MIMETYPES_REGEX = buildMimetypesRegex(MIMETYPES.VIDEO.MIMETYPE_EXT)
1011
1012 ACTIVITY_PUB.URL_MIME_TYPES.VIDEO = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT)
1013
1014 MIMETYPES.VIDEO.EXT_MIMETYPE = buildVideoExtMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT)
1015
1016 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME = Object.keys(MIMETYPES.VIDEO.EXT_MIMETYPE)
1017 }
1018
1019 function buildVideoExtMimetype (obj: { [ id: string ]: string | string[] }) {
1020 const result: { [id: string]: string } = {}
1021
1022 for (const mimetype of Object.keys(obj)) {
1023 const value = obj[mimetype]
1024 if (!value) continue
1025
1026 const extensions = Array.isArray(value) ? value : [ value ]
1027
1028 for (const extension of extensions) {
1029 result[extension] = mimetype
1030 }
1031 }
1032
1033 return result
1034 }
1035
1036 function buildMimetypesRegex (obj: { [id: string]: string | string[] }) {
1037 return Object.keys(obj)
1038 .map(m => `(${m})`)
1039 .join('|')
1040 }
1041
1042 function loadLanguages () {
1043 Object.assign(VIDEO_LANGUAGES, buildLanguages())
1044 }
1045
1046 function buildLanguages () {
1047 const iso639 = require('iso-639-3')
1048
1049 const languages: { [id: string]: string } = {}
1050
1051 const additionalLanguages = {
1052 sgn: true, // Sign languages (macro language)
1053 ase: true, // American sign language
1054 sdl: true, // Arabian sign language
1055 bfi: true, // British sign language
1056 bzs: true, // Brazilian sign language
1057 csl: true, // Chinese sign language
1058 cse: true, // Czech sign language
1059 dsl: true, // Danish sign language
1060 fsl: true, // French sign language
1061 gsg: true, // German sign language
1062 pks: true, // Pakistan sign language
1063 jsl: true, // Japanese sign language
1064 sfs: true, // South African sign language
1065 swl: true, // Swedish sign language
1066 rsl: true, // Russian sign language
1067
1068 kab: true, // Kabyle
1069
1070 lat: true, // Latin
1071
1072 epo: true, // Esperanto
1073 tlh: true, // Klingon
1074 jbo: true, // Lojban
1075 avk: true // Kotava
1076 }
1077
1078 // Only add ISO639-1 languages and some sign languages (ISO639-3)
1079 iso639
1080 .filter(l => {
1081 return (l.iso6391 !== undefined && l.type === 'living') ||
1082 additionalLanguages[l.iso6393] === true
1083 })
1084 .forEach(l => { languages[l.iso6391 || l.iso6393] = l.name })
1085
1086 // Override Occitan label
1087 languages['oc'] = 'Occitan'
1088 languages['el'] = 'Greek'
1089
1090 return languages
1091 }
1092
1093 function generateContentHash () {
1094 return randomBytes(20).toString('hex')
1095 }