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