f87041a3ffe127e9ffbfda527f4fe9b2669a4e21
[oweals/peertube.git] / server / initializers / constants.ts
1 import * as config from 'config'
2 import { join } from 'path'
3
4 // Do not use barrels, remain constants as independent as possible
5 import { root, isTestInstance } from '../helpers/core-utils'
6
7 import {
8   UserRole,
9   VideoRateType,
10   RequestEndpoint,
11   RequestVideoEventType,
12   RequestVideoQaduType,
13   JobState
14 } from '../../shared/models'
15
16 // ---------------------------------------------------------------------------
17
18 const LAST_MIGRATION_VERSION = 75
19
20 // ---------------------------------------------------------------------------
21
22 // API version
23 const API_VERSION = 'v1'
24
25 // Number of results by default for the pagination
26 const PAGINATION_COUNT_DEFAULT = 15
27
28 // Sortable columns per schema
29 const SEARCHABLE_COLUMNS = {
30   VIDEOS: [ 'name', 'magnetUri', 'host', 'author', 'tags' ]
31 }
32
33 // Sortable columns per schema
34 const SORTABLE_COLUMNS = {
35   USERS: [ 'id', 'username', 'createdAt' ],
36   VIDEO_ABUSES: [ 'id', 'createdAt' ],
37   VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ],
38   BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ]
39 }
40
41 const OAUTH_LIFETIME = {
42   ACCESS_TOKEN: 3600 * 4, // 4 hours
43   REFRESH_TOKEN: 1209600 // 2 weeks
44 }
45
46 // ---------------------------------------------------------------------------
47
48 const CONFIG = {
49   LISTEN: {
50     PORT: config.get<number>('listen.port')
51   },
52   DATABASE: {
53     DBNAME: 'peertube' + config.get<string>('database.suffix'),
54     HOSTNAME: config.get<string>('database.hostname'),
55     PORT: config.get<number>('database.port'),
56     USERNAME: config.get<string>('database.username'),
57     PASSWORD: config.get<string>('database.password')
58   },
59   STORAGE: {
60     CERT_DIR: join(root(), config.get<string>('storage.certs')),
61     LOG_DIR: join(root(), config.get<string>('storage.logs')),
62     VIDEOS_DIR: join(root(), config.get<string>('storage.videos')),
63     THUMBNAILS_DIR: join(root(), config.get<string>('storage.thumbnails')),
64     PREVIEWS_DIR: join(root(), config.get<string>('storage.previews')),
65     TORRENTS_DIR: join(root(), config.get<string>('storage.torrents')),
66     CACHE_DIR: join(root(), config.get<string>('storage.cache'))
67   },
68   WEBSERVER: {
69     SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
70     WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
71     HOSTNAME: config.get<string>('webserver.hostname'),
72     PORT: config.get<number>('webserver.port'),
73     URL: '',
74     HOST: ''
75   },
76   ADMIN: {
77     EMAIL: config.get<string>('admin.email')
78   },
79   SIGNUP: {
80     ENABLED: config.get<boolean>('signup.enabled'),
81     LIMIT: config.get<number>('signup.limit')
82   },
83   USER: {
84     VIDEO_QUOTA: config.get<number>('user.video_quota')
85   },
86   TRANSCODING: {
87     ENABLED: config.get<boolean>('transcoding.enabled'),
88     THREADS: config.get<number>('transcoding.threads'),
89     RESOLUTIONS: {
90       '240' : config.get<boolean>('transcoding.resolutions.240p'),
91       '360': config.get<boolean>('transcoding.resolutions.360p'),
92       '480': config.get<boolean>('transcoding.resolutions.480p'),
93       '720': config.get<boolean>('transcoding.resolutions.720p'),
94       '1080': config.get<boolean>('transcoding.resolutions.1080p')
95     }
96   },
97   CACHE: {
98     PREVIEWS: {
99       SIZE: config.get<number>('cache.previews.size')
100     }
101   }
102 }
103 CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
104 CONFIG.WEBSERVER.HOST = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
105
106 // ---------------------------------------------------------------------------
107
108 const CONSTRAINTS_FIELDS = {
109   USERS: {
110     USERNAME: { min: 3, max: 20 }, // Length
111     PASSWORD: { min: 6, max: 255 }, // Length
112     VIDEO_QUOTA: { min: -1 }
113   },
114   VIDEO_ABUSES: {
115     REASON: { min: 2, max: 300 } // Length
116   },
117   VIDEOS: {
118     NAME: { min: 3, max: 50 }, // Length
119     DESCRIPTION: { min: 3, max: 250 }, // Length
120     EXTNAME: [ '.mp4', '.ogv', '.webm' ],
121     INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
122     DURATION: { min: 1, max: 7200 }, // Number
123     TAGS: { min: 0, max: 3 }, // Number of total tags
124     TAG: { min: 2, max: 10 }, // Length
125     THUMBNAIL: { min: 2, max: 30 },
126     THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
127     VIEWS: { min: 0 },
128     LIKES: { min: 0 },
129     DISLIKES: { min: 0 },
130     FILE_SIZE: { min: 10, max: 1024 * 1024 * 1024 * 3 /* 3Go */ }
131   },
132   VIDEO_EVENTS: {
133     COUNT: { min: 0 }
134   }
135 }
136
137 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
138   LIKE: 'like',
139   DISLIKE: 'dislike'
140 }
141
142 const VIDEO_CATEGORIES = {
143   1: 'Music',
144   2: 'Films',
145   3: 'Vehicles',
146   4: 'Art',
147   5: 'Sports',
148   6: 'Travels',
149   7: 'Gaming',
150   8: 'People',
151   9: 'Comedy',
152   10: 'Entertainment',
153   11: 'News',
154   12: 'How To',
155   13: 'Education',
156   14: 'Activism',
157   15: 'Science & Technology',
158   16: 'Animals',
159   17: 'Kids',
160   18: 'Food'
161 }
162
163 // See https://creativecommons.org/licenses/?lang=en
164 const VIDEO_LICENCES = {
165   1: 'Attribution',
166   2: 'Attribution - Share Alike',
167   3: 'Attribution - No Derivatives',
168   4: 'Attribution - Non Commercial',
169   5: 'Attribution - Non Commercial - Share Alike',
170   6: 'Attribution - Non Commercial - No Derivatives',
171   7: 'Public Domain Dedication'
172 }
173
174 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
175 const VIDEO_LANGUAGES = {
176   1: 'English',
177   2: 'Spanish',
178   3: 'Mandarin',
179   4: 'Hindi',
180   5: 'Arabic',
181   6: 'Portuguese',
182   7: 'Bengali',
183   8: 'Russian',
184   9: 'Japanese',
185   10: 'Punjabi',
186   11: 'German',
187   12: 'Korean',
188   13: 'French',
189   14: 'Italian'
190 }
191
192 // TODO: use VideoResolution when https://github.com/Microsoft/TypeScript/issues/13042 is fixed
193 const VIDEO_FILE_RESOLUTIONS: { [ id: number ]: string } = {
194   0: 'original',
195   240: '240p',
196   360: '360p',
197   480: '480p',
198   720: '720p',
199   1080: '1080p'
200 }
201
202 // ---------------------------------------------------------------------------
203
204 // Score a pod has when we create it as a friend
205 const FRIEND_SCORE = {
206   BASE: 100,
207   MAX: 1000
208 }
209
210 // ---------------------------------------------------------------------------
211
212 // Number of points we add/remove from a friend after a successful/bad request
213 const PODS_SCORE = {
214   PENALTY: -10,
215   BONUS: 10
216 }
217
218 // Time to wait between requests to the friends (10 min)
219 let REQUESTS_INTERVAL = 600000
220
221 // Number of requests in parallel we can make
222 const REQUESTS_IN_PARALLEL = 10
223
224 // To how many pods we send requests
225 const REQUESTS_LIMIT_PODS = 10
226 // How many requests we send to a pod per interval
227 const REQUESTS_LIMIT_PER_POD = 5
228
229 const REQUESTS_VIDEO_QADU_LIMIT_PODS = 10
230 // The QADU requests are not big
231 const REQUESTS_VIDEO_QADU_LIMIT_PER_POD = 50
232
233 const REQUESTS_VIDEO_EVENT_LIMIT_PODS = 10
234 // The EVENTS requests are not big
235 const REQUESTS_VIDEO_EVENT_LIMIT_PER_POD = 50
236
237 // Number of requests to retry for replay requests module
238 const RETRY_REQUESTS = 5
239
240 const REQUEST_ENDPOINTS: { [ id: string ]: RequestEndpoint } = {
241   VIDEOS: 'videos'
242 }
243
244 const REQUEST_ENDPOINT_ACTIONS: { [ id: string ]: any } = {}
245 REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] = {
246   ADD: 'add',
247   UPDATE: 'update',
248   REMOVE: 'remove',
249   REPORT_ABUSE: 'report-abuse'
250 }
251
252 const REQUEST_VIDEO_QADU_ENDPOINT = 'videos/qadu'
253 const REQUEST_VIDEO_EVENT_ENDPOINT = 'videos/events'
254
255 const REQUEST_VIDEO_QADU_TYPES: { [ id: string ]: RequestVideoQaduType } = {
256   LIKES: 'likes',
257   DISLIKES: 'dislikes',
258   VIEWS: 'views'
259 }
260
261 const REQUEST_VIDEO_EVENT_TYPES: { [ id: string ]: RequestVideoEventType } = {
262   LIKES: 'likes',
263   DISLIKES: 'dislikes',
264   VIEWS: 'views'
265 }
266
267 const REMOTE_SCHEME = {
268   HTTP: 'https',
269   WS: 'wss'
270 }
271
272 const JOB_STATES: { [ id: string ]: JobState } = {
273   PENDING: 'pending',
274   PROCESSING: 'processing',
275   ERROR: 'error',
276   SUCCESS: 'success'
277 }
278 // How many maximum jobs we fetch from the database per cycle
279 const JOBS_FETCH_LIMIT_PER_CYCLE = 10
280 const JOBS_CONCURRENCY = 1
281 // 1 minutes
282 let JOBS_FETCHING_INTERVAL = 60000
283
284 // ---------------------------------------------------------------------------
285
286 const PRIVATE_CERT_NAME = 'peertube.key.pem'
287 const PUBLIC_CERT_NAME = 'peertube.pub'
288 const SIGNATURE_ALGORITHM = 'RSA-SHA256'
289 const SIGNATURE_ENCODING = 'hex'
290
291 // Password encryption
292 const BCRYPT_SALT_SIZE = 10
293
294 // ---------------------------------------------------------------------------
295
296 // Express static paths (router)
297 const STATIC_PATHS = {
298   PREVIEWS: '/static/previews/',
299   THUMBNAILS: '/static/thumbnails/',
300   TORRENTS: '/static/torrents/',
301   WEBSEED: '/static/webseed/'
302 }
303
304 // Cache control
305 let STATIC_MAX_AGE = '30d'
306
307 // Videos thumbnail size
308 const THUMBNAILS_SIZE = '200x110'
309 const PREVIEWS_SIZE = '640x480'
310
311 // Sub folders of cache directory
312 const CACHE = {
313   DIRECTORIES: {
314     PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
315   }
316 }
317
318 // ---------------------------------------------------------------------------
319
320 const USER_ROLES: { [ id: string ]: UserRole } = {
321   ADMIN: 'admin',
322   USER: 'user'
323 }
324
325 // ---------------------------------------------------------------------------
326
327 const OPENGRAPH_COMMENT = '<!-- open graph tags -->'
328
329 // ---------------------------------------------------------------------------
330
331 // Special constants for a test instance
332 if (isTestInstance() === true) {
333   CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
334   FRIEND_SCORE.BASE = 20
335   REQUESTS_INTERVAL = 10000
336   JOBS_FETCHING_INTERVAL = 10000
337   REMOTE_SCHEME.HTTP = 'http'
338   REMOTE_SCHEME.WS = 'ws'
339   STATIC_MAX_AGE = '0'
340 }
341
342 // ---------------------------------------------------------------------------
343
344 export {
345   API_VERSION,
346   BCRYPT_SALT_SIZE,
347   CACHE,
348   CONFIG,
349   CONSTRAINTS_FIELDS,
350   FRIEND_SCORE,
351   JOB_STATES,
352   JOBS_CONCURRENCY,
353   JOBS_FETCH_LIMIT_PER_CYCLE,
354   JOBS_FETCHING_INTERVAL,
355   LAST_MIGRATION_VERSION,
356   OAUTH_LIFETIME,
357   OPENGRAPH_COMMENT,
358   PAGINATION_COUNT_DEFAULT,
359   PODS_SCORE,
360   PREVIEWS_SIZE,
361   PRIVATE_CERT_NAME,
362   PUBLIC_CERT_NAME,
363   REMOTE_SCHEME,
364   REQUEST_ENDPOINT_ACTIONS,
365   REQUEST_ENDPOINTS,
366   REQUEST_VIDEO_EVENT_ENDPOINT,
367   REQUEST_VIDEO_EVENT_TYPES,
368   REQUEST_VIDEO_QADU_ENDPOINT,
369   REQUEST_VIDEO_QADU_TYPES,
370   REQUESTS_IN_PARALLEL,
371   REQUESTS_INTERVAL,
372   REQUESTS_LIMIT_PER_POD,
373   REQUESTS_LIMIT_PODS,
374   REQUESTS_VIDEO_EVENT_LIMIT_PER_POD,
375   REQUESTS_VIDEO_EVENT_LIMIT_PODS,
376   REQUESTS_VIDEO_QADU_LIMIT_PER_POD,
377   REQUESTS_VIDEO_QADU_LIMIT_PODS,
378   RETRY_REQUESTS,
379   SEARCHABLE_COLUMNS,
380   SIGNATURE_ALGORITHM,
381   SIGNATURE_ENCODING,
382   SORTABLE_COLUMNS,
383   STATIC_MAX_AGE,
384   STATIC_PATHS,
385   THUMBNAILS_SIZE,
386   USER_ROLES,
387   VIDEO_CATEGORIES,
388   VIDEO_FILE_RESOLUTIONS,
389   VIDEO_LANGUAGES,
390   VIDEO_LICENCES,
391   VIDEO_RATE_TYPES
392 }