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