]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/constants.ts
d2bcea44379aa21db2ae29d215eee036476da6de
[github/Chocobozzz/PeerTube.git] / server / initializers / constants.ts
1 import * as config from 'config'
2 import { join } from 'path'
3 import { JobCategory, JobState, 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 { isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
9
10 // ---------------------------------------------------------------------------
11
12 const LAST_MIGRATION_VERSION = 155
13
14 // ---------------------------------------------------------------------------
15
16 // API version
17 const API_VERSION = 'v1'
18
19 // Number of results by default for the pagination
20 const PAGINATION_COUNT_DEFAULT = 15
21
22 // Sortable columns per schema
23 const SORTABLE_COLUMNS = {
24 USERS: [ 'id', 'username', 'createdAt' ],
25 ACCOUNTS: [ 'createdAt' ],
26 JOBS: [ 'id', 'createdAt' ],
27 VIDEO_ABUSES: [ 'id', 'createdAt' ],
28 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
29 VIDEOS: [ 'name', 'duration', 'createdAt', 'views', 'likes' ],
30 VIDEO_COMMENT_THREADS: [ 'createdAt' ],
31 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
32 FOLLOWERS: [ 'createdAt' ],
33 FOLLOWING: [ 'createdAt' ]
34 }
35
36 const OAUTH_LIFETIME = {
37 ACCESS_TOKEN: 3600 * 4, // 4 hours
38 REFRESH_TOKEN: 1209600 // 2 weeks
39 }
40
41 // ---------------------------------------------------------------------------
42
43 // Number of points we add/remove from a friend after a successful/bad request
44 const SERVERS_SCORE = {
45 PENALTY: -10,
46 BONUS: 10,
47 BASE: 100,
48 MAX: 1000
49 }
50
51 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
52 PENDING: 'pending',
53 ACCEPTED: 'accepted'
54 }
55
56 const REMOTE_SCHEME = {
57 HTTP: 'https',
58 WS: 'wss'
59 }
60
61 const JOB_STATES: { [ id: string ]: JobState } = {
62 PENDING: 'pending',
63 PROCESSING: 'processing',
64 ERROR: 'error',
65 SUCCESS: 'success'
66 }
67 const JOB_CATEGORIES: { [ id: string ]: JobCategory } = {
68 TRANSCODING: 'transcoding',
69 ACTIVITYPUB_HTTP: 'activitypub-http'
70 }
71 // How many maximum jobs we fetch from the database per cycle
72 const JOBS_FETCH_LIMIT_PER_CYCLE = {
73 transcoding: 10,
74 httpRequest: 20
75 }
76 // 1 minutes
77 let JOBS_FETCHING_INTERVAL = 60000
78
79 // ---------------------------------------------------------------------------
80
81 const CONFIG = {
82 LISTEN: {
83 PORT: config.get<number>('listen.port')
84 },
85 DATABASE: {
86 DBNAME: 'peertube' + config.get<string>('database.suffix'),
87 HOSTNAME: config.get<string>('database.hostname'),
88 PORT: config.get<number>('database.port'),
89 USERNAME: config.get<string>('database.username'),
90 PASSWORD: config.get<string>('database.password')
91 },
92 STORAGE: {
93 AVATARS_DIR: join(root(), config.get<string>('storage.avatars')),
94 LOG_DIR: join(root(), config.get<string>('storage.logs')),
95 VIDEOS_DIR: join(root(), config.get<string>('storage.videos')),
96 THUMBNAILS_DIR: join(root(), config.get<string>('storage.thumbnails')),
97 PREVIEWS_DIR: join(root(), config.get<string>('storage.previews')),
98 TORRENTS_DIR: join(root(), config.get<string>('storage.torrents')),
99 CACHE_DIR: join(root(), config.get<string>('storage.cache'))
100 },
101 WEBSERVER: {
102 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
103 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
104 HOSTNAME: config.get<string>('webserver.hostname'),
105 PORT: config.get<number>('webserver.port'),
106 URL: '',
107 HOST: ''
108 },
109 ADMIN: {
110 EMAIL: config.get<string>('admin.email')
111 },
112 SIGNUP: {
113 ENABLED: config.get<boolean>('signup.enabled'),
114 LIMIT: config.get<number>('signup.limit')
115 },
116 USER: {
117 VIDEO_QUOTA: config.get<number>('user.video_quota')
118 },
119 TRANSCODING: {
120 ENABLED: config.get<boolean>('transcoding.enabled'),
121 THREADS: config.get<number>('transcoding.threads'),
122 RESOLUTIONS: {
123 '240' : config.get<boolean>('transcoding.resolutions.240p'),
124 '360': config.get<boolean>('transcoding.resolutions.360p'),
125 '480': config.get<boolean>('transcoding.resolutions.480p'),
126 '720': config.get<boolean>('transcoding.resolutions.720p'),
127 '1080': config.get<boolean>('transcoding.resolutions.1080p')
128 }
129 },
130 CACHE: {
131 PREVIEWS: {
132 SIZE: config.get<number>('cache.previews.size')
133 }
134 }
135 }
136
137 // ---------------------------------------------------------------------------
138
139 const CONSTRAINTS_FIELDS = {
140 USERS: {
141 USERNAME: { min: 3, max: 20 }, // Length
142 PASSWORD: { min: 6, max: 255 }, // Length
143 VIDEO_QUOTA: { min: -1 }
144 },
145 VIDEO_ABUSES: {
146 REASON: { min: 2, max: 300 } // Length
147 },
148 VIDEO_CHANNELS: {
149 NAME: { min: 3, max: 120 }, // Length
150 DESCRIPTION: { min: 3, max: 250 }, // Length
151 URL: { min: 3, max: 2000 } // Length
152 },
153 VIDEOS: {
154 NAME: { min: 3, max: 120 }, // Length
155 TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
156 DESCRIPTION: { min: 3, max: 3000 }, // Length
157 EXTNAME: [ '.mp4', '.ogv', '.webm' ],
158 INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
159 DURATION: { min: 1 }, // Number
160 TAGS: { min: 0, max: 5 }, // Number of total tags
161 TAG: { min: 2, max: 30 }, // Length
162 THUMBNAIL: { min: 2, max: 30 },
163 THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
164 VIEWS: { min: 0 },
165 LIKES: { min: 0 },
166 DISLIKES: { min: 0 },
167 FILE_SIZE: { min: 10 },
168 URL: { min: 3, max: 2000 } // Length
169 },
170 ACTORS: {
171 PUBLIC_KEY: { min: 10, max: 5000 }, // Length
172 PRIVATE_KEY: { min: 10, max: 5000 }, // Length
173 URL: { min: 3, max: 2000 }, // Length
174 AVATAR: {
175 EXTNAME: [ '.png', '.jpeg', '.jpg' ],
176 FILE_SIZE: {
177 max: 2 * 1024 * 1024 // 2MB
178 }
179 }
180 },
181 VIDEO_EVENTS: {
182 COUNT: { min: 0 }
183 },
184 VIDEO_COMMENTS: {
185 TEXT: { min: 2, max: 3000 }, // Length
186 URL: { min: 3, max: 2000 } // Length
187 }
188 }
189
190 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
191 LIKE: 'like',
192 DISLIKE: 'dislike'
193 }
194
195 const VIDEO_CATEGORIES = {
196 1: 'Music',
197 2: 'Films',
198 3: 'Vehicles',
199 4: 'Art',
200 5: 'Sports',
201 6: 'Travels',
202 7: 'Gaming',
203 8: 'People',
204 9: 'Comedy',
205 10: 'Entertainment',
206 11: 'News',
207 12: 'How To',
208 13: 'Education',
209 14: 'Activism',
210 15: 'Science & Technology',
211 16: 'Animals',
212 17: 'Kids',
213 18: 'Food'
214 }
215
216 // See https://creativecommons.org/licenses/?lang=en
217 const VIDEO_LICENCES = {
218 1: 'Attribution',
219 2: 'Attribution - Share Alike',
220 3: 'Attribution - No Derivatives',
221 4: 'Attribution - Non Commercial',
222 5: 'Attribution - Non Commercial - Share Alike',
223 6: 'Attribution - Non Commercial - No Derivatives',
224 7: 'Public Domain Dedication'
225 }
226
227 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
228 const VIDEO_LANGUAGES = {
229 1: 'English',
230 2: 'Spanish',
231 3: 'Mandarin',
232 4: 'Hindi',
233 5: 'Arabic',
234 6: 'Portuguese',
235 7: 'Bengali',
236 8: 'Russian',
237 9: 'Japanese',
238 10: 'Punjabi',
239 11: 'German',
240 12: 'Korean',
241 13: 'French',
242 14: 'Italian'
243 }
244
245 const VIDEO_PRIVACIES = {
246 [VideoPrivacy.PUBLIC]: 'Public',
247 [VideoPrivacy.UNLISTED]: 'Unlisted',
248 [VideoPrivacy.PRIVATE]: 'Private'
249 }
250
251 const VIDEO_MIMETYPE_EXT = {
252 'video/webm': '.webm',
253 'video/ogg': '.ogv',
254 'video/mp4': '.mp4'
255 }
256
257 const AVATAR_MIMETYPE_EXT = {
258 'image/png': '.png',
259 'image/jpg': '.jpg',
260 'image/jpeg': '.jpg'
261 }
262
263 // ---------------------------------------------------------------------------
264
265 const SERVER_ACTOR_NAME = 'peertube'
266
267 const ACTIVITY_PUB = {
268 POTENTIAL_ACCEPT_HEADERS: [
269 'application/activity+json',
270 'application/ld+json'
271 ],
272 ACCEPT_HEADER: 'application/activity+json, application/ld+json',
273 PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
274 COLLECTION_ITEMS_PER_PAGE: 10,
275 FETCH_PAGE_LIMIT: 100,
276 MAX_HTTP_ATTEMPT: 5,
277 URL_MIME_TYPES: {
278 VIDEO: [ 'video/mp4', 'video/webm', 'video/ogg' ], // TODO: Merge with VIDEO_MIMETYPE_EXT
279 TORRENT: [ 'application/x-bittorrent' ],
280 MAGNET: [ 'application/x-bittorrent;x-scheme-handler/magnet' ]
281 }
282 }
283
284 const ACTIVITY_PUB_ACTOR_TYPES: { [ id: string ]: ActivityPubActorType } = {
285 GROUP: 'Group',
286 PERSON: 'Person',
287 APPLICATION: 'Application'
288 }
289
290 // ---------------------------------------------------------------------------
291
292 const PRIVATE_RSA_KEY_SIZE = 2048
293
294 // Password encryption
295 const BCRYPT_SALT_SIZE = 10
296
297 // ---------------------------------------------------------------------------
298
299 // Express static paths (router)
300 const STATIC_PATHS = {
301 PREVIEWS: '/static/previews/',
302 THUMBNAILS: '/static/thumbnails/',
303 TORRENTS: '/static/torrents/',
304 WEBSEED: '/static/webseed/',
305 AVATARS: '/static/avatars/'
306 }
307
308 // Cache control
309 let STATIC_MAX_AGE = '30d'
310
311 // Videos thumbnail size
312 const THUMBNAILS_SIZE = {
313 width: 200,
314 height: 110
315 }
316 const PREVIEWS_SIZE = {
317 width: 560,
318 height: 315
319 }
320 const AVATARS_SIZE = {
321 width: 120,
322 height: 120
323 }
324
325 const EMBED_SIZE = {
326 width: 560,
327 height: 315
328 }
329
330 // Sub folders of cache directory
331 const CACHE = {
332 DIRECTORIES: {
333 PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
334 }
335 }
336
337 const ACCEPT_HEADERS = [ 'html', 'application/json' ].concat(ACTIVITY_PUB.POTENTIAL_ACCEPT_HEADERS)
338
339 // ---------------------------------------------------------------------------
340
341 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
342
343 // ---------------------------------------------------------------------------
344
345 // Special constants for a test instance
346 if (isTestInstance() === true) {
347 SERVERS_SCORE.BASE = 20
348 JOBS_FETCHING_INTERVAL = 1000
349 REMOTE_SCHEME.HTTP = 'http'
350 REMOTE_SCHEME.WS = 'ws'
351 STATIC_MAX_AGE = '0'
352 ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE = 2
353 CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max = 100 * 1024 // 100KB
354 }
355
356 CONFIG.WEBSERVER.URL = sanitizeUrl(CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT)
357 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
358
359 // ---------------------------------------------------------------------------
360
361 export {
362 API_VERSION,
363 AVATARS_SIZE,
364 ACCEPT_HEADERS,
365 BCRYPT_SALT_SIZE,
366 CACHE,
367 CONFIG,
368 CONSTRAINTS_FIELDS,
369 EMBED_SIZE,
370 JOB_STATES,
371 JOBS_FETCH_LIMIT_PER_CYCLE,
372 JOBS_FETCHING_INTERVAL,
373 JOB_CATEGORIES,
374 LAST_MIGRATION_VERSION,
375 OAUTH_LIFETIME,
376 OPENGRAPH_AND_OEMBED_COMMENT,
377 PAGINATION_COUNT_DEFAULT,
378 SERVERS_SCORE,
379 PREVIEWS_SIZE,
380 REMOTE_SCHEME,
381 FOLLOW_STATES,
382 SERVER_ACTOR_NAME,
383 PRIVATE_RSA_KEY_SIZE,
384 SORTABLE_COLUMNS,
385 STATIC_MAX_AGE,
386 STATIC_PATHS,
387 ACTIVITY_PUB,
388 ACTIVITY_PUB_ACTOR_TYPES,
389 THUMBNAILS_SIZE,
390 VIDEO_CATEGORIES,
391 VIDEO_LANGUAGES,
392 VIDEO_PRIVACIES,
393 VIDEO_LICENCES,
394 VIDEO_RATE_TYPES,
395 VIDEO_MIMETYPE_EXT,
396 AVATAR_MIMETYPE_EXT
397 }