Add 30 fps limit in transcoding
[oweals/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     CUSTOMIZATIONS: {
163       get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
164       get CSS () { return config.get<string>('instance.customizations.css') }
165     }
166   }
167 }
168
169 // ---------------------------------------------------------------------------
170
171 const CONSTRAINTS_FIELDS = {
172   USERS: {
173     USERNAME: { min: 3, max: 20 }, // Length
174     PASSWORD: { min: 6, max: 255 }, // Length
175     DESCRIPTION: { min: 3, max: 250 }, // Length
176     VIDEO_QUOTA: { min: -1 }
177   },
178   VIDEO_ABUSES: {
179     REASON: { min: 2, max: 300 } // Length
180   },
181   VIDEO_CHANNELS: {
182     NAME: { min: 3, max: 120 }, // Length
183     DESCRIPTION: { min: 3, max: 250 }, // Length
184     SUPPORT: { min: 3, max: 300 }, // Length
185     URL: { min: 3, max: 2000 } // Length
186   },
187   VIDEOS: {
188     NAME: { min: 3, max: 120 }, // Length
189     TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
190     DESCRIPTION: { min: 3, max: 10000 }, // Length
191     SUPPORT: { min: 3, max: 300 }, // Length
192     IMAGE: {
193       EXTNAME: [ '.jpg', '.jpeg' ],
194       FILE_SIZE: {
195         max: 2 * 1024 * 1024 // 2MB
196       }
197     },
198     EXTNAME: [ '.mp4', '.ogv', '.webm' ],
199     INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
200     DURATION: { min: 1 }, // Number
201     TAGS: { min: 0, max: 5 }, // Number of total tags
202     TAG: { min: 2, max: 30 }, // Length
203     THUMBNAIL: { min: 2, max: 30 },
204     THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
205     VIEWS: { min: 0 },
206     LIKES: { min: 0 },
207     DISLIKES: { min: 0 },
208     FILE_SIZE: { min: 10 },
209     URL: { min: 3, max: 2000 } // Length
210   },
211   ACTORS: {
212     PUBLIC_KEY: { min: 10, max: 5000 }, // Length
213     PRIVATE_KEY: { min: 10, max: 5000 }, // Length
214     URL: { min: 3, max: 2000 }, // Length
215     AVATAR: {
216       EXTNAME: [ '.png', '.jpeg', '.jpg' ],
217       FILE_SIZE: {
218         max: 2 * 1024 * 1024 // 2MB
219       }
220     }
221   },
222   VIDEO_EVENTS: {
223     COUNT: { min: 0 }
224   },
225   VIDEO_COMMENTS: {
226     TEXT: { min: 2, max: 3000 }, // Length
227     URL: { min: 3, max: 2000 } // Length
228   },
229   VIDEO_SHARE: {
230     URL: { min: 3, max: 2000 } // Length
231   }
232 }
233
234 let VIDEO_VIEW_LIFETIME = 60000 * 60 // 1 hour
235 const MAX_VIDEO_TRANSCODING_FPS = 30
236
237 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
238   LIKE: 'like',
239   DISLIKE: 'dislike'
240 }
241
242 const VIDEO_CATEGORIES = {
243   1: 'Music',
244   2: 'Films',
245   3: 'Vehicles',
246   4: 'Art',
247   5: 'Sports',
248   6: 'Travels',
249   7: 'Gaming',
250   8: 'People',
251   9: 'Comedy',
252   10: 'Entertainment',
253   11: 'News',
254   12: 'How To',
255   13: 'Education',
256   14: 'Activism',
257   15: 'Science & Technology',
258   16: 'Animals',
259   17: 'Kids',
260   18: 'Food'
261 }
262
263 // See https://creativecommons.org/licenses/?lang=en
264 const VIDEO_LICENCES = {
265   1: 'Attribution',
266   2: 'Attribution - Share Alike',
267   3: 'Attribution - No Derivatives',
268   4: 'Attribution - Non Commercial',
269   5: 'Attribution - Non Commercial - Share Alike',
270   6: 'Attribution - Non Commercial - No Derivatives',
271   7: 'Public Domain Dedication'
272 }
273
274 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
275 const VIDEO_LANGUAGES = {
276   1: 'English',
277   2: 'Spanish',
278   3: 'Mandarin',
279   4: 'Hindi',
280   5: 'Arabic',
281   6: 'Portuguese',
282   7: 'Bengali',
283   8: 'Russian',
284   9: 'Japanese',
285   10: 'Punjabi',
286   11: 'German',
287   12: 'Korean',
288   13: 'French',
289   14: 'Italian'
290 }
291
292 const VIDEO_PRIVACIES = {
293   [VideoPrivacy.PUBLIC]: 'Public',
294   [VideoPrivacy.UNLISTED]: 'Unlisted',
295   [VideoPrivacy.PRIVATE]: 'Private'
296 }
297
298 const VIDEO_MIMETYPE_EXT = {
299   'video/webm': '.webm',
300   'video/ogg': '.ogv',
301   'video/mp4': '.mp4'
302 }
303
304 const IMAGE_MIMETYPE_EXT = {
305   'image/png': '.png',
306   'image/jpg': '.jpg',
307   'image/jpeg': '.jpg'
308 }
309
310 // ---------------------------------------------------------------------------
311
312 const SERVER_ACTOR_NAME = 'peertube'
313
314 const ACTIVITY_PUB = {
315   POTENTIAL_ACCEPT_HEADERS: [
316     'application/activity+json',
317     'application/ld+json',
318     'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
319   ],
320   ACCEPT_HEADER: 'application/activity+json, application/ld+json',
321   PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
322   COLLECTION_ITEMS_PER_PAGE: 10,
323   FETCH_PAGE_LIMIT: 100,
324   URL_MIME_TYPES: {
325     VIDEO: Object.keys(VIDEO_MIMETYPE_EXT),
326     TORRENT: [ 'application/x-bittorrent' ],
327     MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
328   },
329   MAX_RECURSION_COMMENTS: 100,
330   ACTOR_REFRESH_INTERVAL: 3600 * 24 * 1000 // 1 day
331 }
332
333 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
334   GROUP: 'Group',
335   PERSON: 'Person',
336   APPLICATION: 'Application'
337 }
338
339 // ---------------------------------------------------------------------------
340
341 const PRIVATE_RSA_KEY_SIZE = 2048
342
343 // Password encryption
344 const BCRYPT_SALT_SIZE = 10
345
346 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes
347
348 // ---------------------------------------------------------------------------
349
350 // Express static paths (router)
351 const STATIC_PATHS = {
352   PREVIEWS: '/static/previews/',
353   THUMBNAILS: '/static/thumbnails/',
354   TORRENTS: '/static/torrents/',
355   WEBSEED: '/static/webseed/',
356   AVATARS: '/static/avatars/'
357 }
358
359 // Cache control
360 let STATIC_MAX_AGE = '30d'
361
362 // Videos thumbnail size
363 const THUMBNAILS_SIZE = {
364   width: 200,
365   height: 110
366 }
367 const PREVIEWS_SIZE = {
368   width: 560,
369   height: 315
370 }
371 const AVATARS_SIZE = {
372   width: 120,
373   height: 120
374 }
375
376 const EMBED_SIZE = {
377   width: 560,
378   height: 315
379 }
380
381 // Sub folders of cache directory
382 const CACHE = {
383   DIRECTORIES: {
384     PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
385   }
386 }
387
388 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
389
390 // ---------------------------------------------------------------------------
391
392 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
393
394 // ---------------------------------------------------------------------------
395
396 // Special constants for a test instance
397 if (isTestInstance() === true) {
398   ACTOR_FOLLOW_SCORE.BASE = 20
399   REMOTE_SCHEME.HTTP = 'http'
400   REMOTE_SCHEME.WS = 'ws'
401   STATIC_MAX_AGE = '0'
402   ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
403   ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL = 10 * 1000 // 10 seconds
404   CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
405   SCHEDULER_INTERVAL = 10000
406   VIDEO_VIEW_LIFETIME = 1000 // 1 second
407 }
408
409 updateWebserverConfig()
410
411 // ---------------------------------------------------------------------------
412
413 export {
414   API_VERSION,
415   AVATARS_SIZE,
416   ACCEPT_HEADERS,
417   BCRYPT_SALT_SIZE,
418   CACHE,
419   CONFIG,
420   CONSTRAINTS_FIELDS,
421   EMBED_SIZE,
422   JOB_CONCURRENCY,
423   JOB_ATTEMPTS,
424   LAST_MIGRATION_VERSION,
425   OAUTH_LIFETIME,
426   OPENGRAPH_AND_OEMBED_COMMENT,
427   PAGINATION_COUNT_DEFAULT,
428   ACTOR_FOLLOW_SCORE,
429   PREVIEWS_SIZE,
430   REMOTE_SCHEME,
431   FOLLOW_STATES,
432   SERVER_ACTOR_NAME,
433   PRIVATE_RSA_KEY_SIZE,
434   SORTABLE_COLUMNS,
435   STATIC_MAX_AGE,
436   STATIC_PATHS,
437   ACTIVITY_PUB,
438   ACTIVITY_PUB_ACTOR_TYPES,
439   THUMBNAILS_SIZE,
440   VIDEO_CATEGORIES,
441   VIDEO_LANGUAGES,
442   VIDEO_PRIVACIES,
443   VIDEO_LICENCES,
444   VIDEO_RATE_TYPES,
445   VIDEO_MIMETYPE_EXT,
446   MAX_VIDEO_TRANSCODING_FPS,
447   USER_PASSWORD_RESET_LIFETIME,
448   IMAGE_MIMETYPE_EXT,
449   SCHEDULER_INTERVAL,
450   JOB_COMPLETED_LIFETIME,
451   VIDEO_VIEW_LIFETIME
452 }
453
454 // ---------------------------------------------------------------------------
455
456 function getLocalConfigFilePath () {
457   const configSources = config.util.getConfigSources()
458   if (configSources.length === 0) throw new Error('Invalid config source.')
459
460   let filename = 'local'
461   if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
462   if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
463
464   return join(dirname(configSources[ 0 ].name), filename + '.json')
465 }
466
467 function updateWebserverConfig () {
468   CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
469   CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
470 }
471
472 export function reloadConfig () {
473
474   function directory () {
475     if (process.env.NODE_CONFIG_DIR) {
476       return process.env.NODE_CONFIG_DIR
477     }
478
479     return join(root(), 'config')
480   }
481
482   function purge () {
483     for (const fileName in require.cache) {
484       if (-1 === fileName.indexOf(directory())) {
485         continue
486       }
487
488       delete require.cache[fileName]
489     }
490
491     delete require.cache[require.resolve('config')]
492   }
493
494   purge()
495
496   config = require('config')
497
498   updateWebserverConfig()
499 }