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