]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/constants.ts
add import-youtube guide inside documentation (#298)
[github/Chocobozzz/PeerTube.git] / server / initializers / constants.ts
1 import { IConfig } from 'config'
2 import { dirname, join } from 'path'
3 import { JobType, VideoRateType } from '../../shared/models'
4 import { ActivityPubActorType } from '../../shared/models/activitypub'
5 import { FollowState } from '../../shared/models/actors'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 // Do not use barrels, remain constants as independent as possible
8 import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
9
10 // Use a variable to reload the configuration if we need
11 let config: IConfig = require('config')
12
13 // ---------------------------------------------------------------------------
14
15 const LAST_MIGRATION_VERSION = 195
16
17 // ---------------------------------------------------------------------------
18
19 // API version
20 const API_VERSION = 'v1'
21
22 // Number of results by default for the pagination
23 const PAGINATION_COUNT_DEFAULT = 15
24
25 // Sortable columns per schema
26 const SORTABLE_COLUMNS = {
27 USERS: [ 'id', 'username', 'createdAt' ],
28 ACCOUNTS: [ 'createdAt' ],
29 JOBS: [ 'createdAt' ],
30 VIDEO_ABUSES: [ 'id', 'createdAt' ],
31 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
32 VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ],
33 VIDEO_COMMENT_THREADS: [ 'createdAt' ],
34 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
35 FOLLOWERS: [ 'createdAt' ],
36 FOLLOWING: [ 'createdAt' ]
37 }
38
39 const OAUTH_LIFETIME = {
40 ACCESS_TOKEN: 3600 * 4, // 4 hours
41 REFRESH_TOKEN: 1209600 // 2 weeks
42 }
43
44 // ---------------------------------------------------------------------------
45
46 // Number of points we add/remove after a successful/bad request
47 const ACTOR_FOLLOW_SCORE = {
48 PENALTY: -10,
49 BONUS: 10,
50 BASE: 1000,
51 MAX: 10000
52 }
53
54 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
55 PENDING: 'pending',
56 ACCEPTED: 'accepted'
57 }
58
59 const REMOTE_SCHEME = {
60 HTTP: 'https',
61 WS: 'wss'
62 }
63
64 const JOB_ATTEMPTS: { [ id in JobType ]: number } = {
65 'activitypub-http-broadcast': 5,
66 'activitypub-http-unicast': 5,
67 'activitypub-http-fetcher': 5,
68 'video-file': 1,
69 'email': 5
70 }
71 const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
72 'activitypub-http-broadcast': 1,
73 'activitypub-http-unicast': 5,
74 'activitypub-http-fetcher': 1,
75 'video-file': 1,
76 'email': 5
77 }
78 // 2 days
79 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2
80
81 // 1 hour
82 let SCHEDULER_INTERVAL = 60000 * 60
83
84 // ---------------------------------------------------------------------------
85
86 const CONFIG = {
87 CUSTOM_FILE: getLocalConfigFilePath(),
88 LISTEN: {
89 PORT: config.get<number>('listen.port')
90 },
91 DATABASE: {
92 DBNAME: 'peertube' + config.get<string>('database.suffix'),
93 HOSTNAME: config.get<string>('database.hostname'),
94 PORT: config.get<number>('database.port'),
95 USERNAME: config.get<string>('database.username'),
96 PASSWORD: config.get<string>('database.password')
97 },
98 REDIS: {
99 HOSTNAME: config.get<string>('redis.hostname'),
100 PORT: config.get<number>('redis.port'),
101 AUTH: config.get<string>('redis.auth')
102 },
103 SMTP: {
104 HOSTNAME: config.get<string>('smtp.hostname'),
105 PORT: config.get<number>('smtp.port'),
106 USERNAME: config.get<string>('smtp.username'),
107 PASSWORD: config.get<string>('smtp.password'),
108 TLS: config.get<boolean>('smtp.tls'),
109 CA_FILE: config.get<string>('smtp.ca_file'),
110 FROM_ADDRESS: config.get<string>('smtp.from_address')
111 },
112 STORAGE: {
113 AVATARS_DIR: buildPath(config.get<string>('storage.avatars')),
114 LOG_DIR: buildPath(config.get<string>('storage.logs')),
115 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
116 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
117 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
118 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
119 CACHE_DIR: buildPath(config.get<string>('storage.cache'))
120 },
121 WEBSERVER: {
122 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
123 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
124 HOSTNAME: config.get<string>('webserver.hostname'),
125 PORT: config.get<number>('webserver.port'),
126 URL: '',
127 HOST: ''
128 },
129 LOG: {
130 LEVEL: config.get<string>('log.level')
131 },
132 ADMIN: {
133 get EMAIL () { return config.get<string>('admin.email') }
134 },
135 SIGNUP: {
136 get ENABLED () { return config.get<boolean>('signup.enabled') },
137 get LIMIT () { return config.get<number>('signup.limit') }
138 },
139 USER: {
140 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }
141 },
142 TRANSCODING: {
143 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
144 get THREADS () { return config.get<number>('transcoding.threads') },
145 RESOLUTIONS: {
146 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
147 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
148 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
149 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
150 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') }
151 }
152 },
153 CACHE: {
154 PREVIEWS: {
155 get SIZE () { return config.get<number>('cache.previews.size') }
156 }
157 },
158 INSTANCE: {
159 get NAME () { return config.get<string>('instance.name') },
160 get DESCRIPTION () { return config.get<string>('instance.description') },
161 get TERMS () { return config.get<string>('instance.terms') }
162 }
163 }
164
165 // ---------------------------------------------------------------------------
166
167 const CONSTRAINTS_FIELDS = {
168 USERS: {
169 USERNAME: { min: 3, max: 20 }, // Length
170 PASSWORD: { min: 6, max: 255 }, // Length
171 DESCRIPTION: { min: 3, max: 250 }, // Length
172 VIDEO_QUOTA: { min: -1 }
173 },
174 VIDEO_ABUSES: {
175 REASON: { min: 2, max: 300 } // Length
176 },
177 VIDEO_CHANNELS: {
178 NAME: { min: 3, max: 120 }, // Length
179 DESCRIPTION: { min: 3, max: 250 }, // Length
180 SUPPORT: { min: 3, max: 300 }, // Length
181 URL: { min: 3, max: 2000 } // Length
182 },
183 VIDEOS: {
184 NAME: { min: 3, max: 120 }, // Length
185 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
186 DESCRIPTION: { min: 3, max: 10000 }, // Length
187 SUPPORT: { min: 3, max: 300 }, // Length
188 IMAGE: {
189 EXTNAME: [ '.jpg', '.jpeg' ],
190 FILE_SIZE: {
191 max: 2 * 1024 * 1024 // 2MB
192 }
193 },
194 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
195 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
196 DURATION: { min: 1 }, // Number
197 TAGS: { min: 0, max: 5 }, // Number of total tags
198 TAG: { min: 2, max: 30 }, // Length
199 THUMBNAIL: { min: 2, max: 30 },
200 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
201 VIEWS: { min: 0 },
202 LIKES: { min: 0 },
203 DISLIKES: { min: 0 },
204 FILE_SIZE: { min: 10 },
205 URL: { min: 3, max: 2000 } // Length
206 },
207 ACTORS: {
208 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
209 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
210 URL: { min: 3, max: 2000 }, // Length
211 AVATAR: {
212 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
213 FILE_SIZE: {
214 max: 2 * 1024 * 1024 // 2MB
215 }
216 }
217 },
218 VIDEO_EVENTS: {
219 COUNT: { min: 0 }
220 },
221 VIDEO_COMMENTS: {
222 TEXT: { min: 2, max: 3000 }, // Length
223 URL: { min: 3, max: 2000 } // Length
224 },
225 VIDEO_SHARE: {
226 URL: { min: 3, max: 2000 } // Length
227 }
228 }
229
230 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
231 LIKE: 'like',
232 DISLIKE: 'dislike'
233 }
234
235 const VIDEO_CATEGORIES = {
236 1: 'Music',
237 2: 'Films',
238 3: 'Vehicles',
239 4: 'Art',
240 5: 'Sports',
241 6: 'Travels',
242 7: 'Gaming',
243 8: 'People',
244 9: 'Comedy',
245 10: 'Entertainment',
246 11: 'News',
247 12: 'How To',
248 13: 'Education',
249 14: 'Activism',
250 15: 'Science & Technology',
251 16: 'Animals',
252 17: 'Kids',
253 18: 'Food'
254 }
255
256 // See https://creativecommons.org/licenses/?lang=en
257 const VIDEO_LICENCES = {
258 1: 'Attribution',
259 2: 'Attribution - Share Alike',
260 3: 'Attribution - No Derivatives',
261 4: 'Attribution - Non Commercial',
262 5: 'Attribution - Non Commercial - Share Alike',
263 6: 'Attribution - Non Commercial - No Derivatives',
264 7: 'Public Domain Dedication'
265 }
266
267 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
268 const VIDEO_LANGUAGES = {
269 1: 'English',
270 2: 'Spanish',
271 3: 'Mandarin',
272 4: 'Hindi',
273 5: 'Arabic',
274 6: 'Portuguese',
275 7: 'Bengali',
276 8: 'Russian',
277 9: 'Japanese',
278 10: 'Punjabi',
279 11: 'German',
280 12: 'Korean',
281 13: 'French',
282 14: 'Italian'
283 }
284
285 const VIDEO_PRIVACIES = {
286 [VideoPrivacy.PUBLIC]: 'Public',
287 [VideoPrivacy.UNLISTED]: 'Unlisted',
288 [VideoPrivacy.PRIVATE]: 'Private'
289 }
290
291 const VIDEO_MIMETYPE_EXT = {
292 'video/webm': '.webm',
293 'video/ogg': '.ogv',
294 'video/mp4': '.mp4'
295 }
296
297 const IMAGE_MIMETYPE_EXT = {
298 'image/png': '.png',
299 'image/jpg': '.jpg',
300 'image/jpeg': '.jpg'
301 }
302
303 // ---------------------------------------------------------------------------
304
305 const SERVER_ACTOR_NAME = 'peertube'
306
307 const ACTIVITY_PUB = {
308 POTENTIAL_ACCEPT_HEADERS: [
309 'application/activity+json',
310 'application/ld+json',
311 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
312 ],
313 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
314 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
315 COLLECTION_ITEMS_PER_PAGE: 10,
316 FETCH_PAGE_LIMIT: 100,
317 URL_MIME_TYPES: {
318 VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
319 TORRENT: [ 'application/x-bittorrent' ],
320 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
321 },
322 MAX_RECURSION_COMMENTS: 100,
323 ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
324 }
325
326 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
327 GROUP: 'Group',
328 PERSON: 'Person',
329 APPLICATION: 'Application'
330 }
331
332 // ---------------------------------------------------------------------------
333
334 const PRIVATE_RSA_KEY_SIZE = 2048
335
336 // Password encryption
337 const BCRYPT_SALT_SIZE = 10
338
339 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
340
341 // ---------------------------------------------------------------------------
342
343 // Express static paths (router)
344 const STATIC_PATHS = {
345 PREVIEWS: '/static/previews/',
346 THUMBNAILS: '/static/thumbnails/',
347 TORRENTS: '/static/torrents/',
348 WEBSEED: '/static/webseed/',
349 AVATARS: '/static/avatars/'
350 }
351
352 // Cache control
353 let STATIC_MAX_AGE = '30d'
354
355 // Videos thumbnail size
356 const THUMBNAILS_SIZE = {
357 width: 200,
358 height: 110
359 }
360 const PREVIEWS_SIZE = {
361 width: 560,
362 height: 315
363 }
364 const AVATARS_SIZE = {
365 width: 120,
366 height: 120
367 }
368
369 const EMBED_SIZE = {
370 width: 560,
371 height: 315
372 }
373
374 // Sub folders of cache directory
375 const CACHE = {
376 DIRECTORIES: {
377 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
378 }
379 }
380
381 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
382
383 // ---------------------------------------------------------------------------
384
385 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
386
387 // ---------------------------------------------------------------------------
388
389 // Special constants for a test instance
390 if (isTestInstance() === true) {
391 ACTOR_FOLLOW_SCORE.BASE = 20
392 REMOTE_SCHEME.HTTP = 'http'
393 REMOTE_SCHEME.WS = 'ws'
394 STATIC_MAX_AGE = '0'
395 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
396 ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
397 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
398 SCHEDULER_INTERVAL = 10000
399 }
400
401 updateWebserverConfig()
402
403 // ---------------------------------------------------------------------------
404
405 export {
406 API_VERSION,
407 AVATARS_SIZE,
408 ACCEPT_HEADERS,
409 BCRYPT_SALT_SIZE,
410 CACHE,
411 CONFIG,
412 CONSTRAINTS_FIELDS,
413 EMBED_SIZE,
414 JOB_CONCURRENCY,
415 JOB_ATTEMPTS,
416 LAST_MIGRATION_VERSION,
417 OAUTH_LIFETIME,
418 OPENGRAPH_AND_OEMBED_COMMENT,
419 PAGINATION_COUNT_DEFAULT,
420 ACTOR_FOLLOW_SCORE,
421 PREVIEWS_SIZE,
422 REMOTE_SCHEME,
423 FOLLOW_STATES,
424 SERVER_ACTOR_NAME,
425 PRIVATE_RSA_KEY_SIZE,
426 SORTABLE_COLUMNS,
427 STATIC_MAX_AGE,
428 STATIC_PATHS,
429 ACTIVITY_PUB,
430 ACTIVITY_PUB_ACTOR_TYPES,
431 THUMBNAILS_SIZE,
432 VIDEO_CATEGORIES,
433 VIDEO_LANGUAGES,
434 VIDEO_PRIVACIES,
435 VIDEO_LICENCES,
436 VIDEO_RATE_TYPES,
437 VIDEO_MIMETYPE_EXT,
438 USER_PASSWORD_RESET_LIFETIME,
439 IMAGE_MIMETYPE_EXT,
440 SCHEDULER_INTERVAL,
441 JOB_COMPLETED_LIFETIME
442 }
443
444 // ---------------------------------------------------------------------------
445
446 function getLocalConfigFilePath () {
447 const configSources = config.util.getConfigSources()
448 if (configSources.length === 0) throw new Error('Invalid config source.')
449
450 let filename = 'local'
451 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
452 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
453
454 return join(dirname(configSources[ 0 ].name), filename + '.json')
455 }
456
457 function updateWebserverConfig () {
458 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
459 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
460 }
461
462 export function reloadConfig () {
463
464 function directory () {
465 if (process.env.NODE_CONFIG_DIR) {
466 return process.env.NODE_CONFIG_DIR
467 }
468
469 return join(root(), 'config')
470 }
471
472 function purge () {
473 for (const fileName in require.cache) {
474 if (-1 === fileName.indexOf(directory())) {
475 continue
476 }
477
478 delete require.cache[fileName]
479 }
480
481 delete require.cache[require.resolve('config')]
482 }
483
484 purge()
485
486 config = require('config')
487
488 updateWebserverConfig()
489 }