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