5d0d39395ec10d346042e2539c128d1cb052f3eb
[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   },
126   VIDEOS: {
127     NAME: { min: 3, max: 120 }, // Length
128     TRUNCATED_DESCRIPTION: { min: 3, max: 250 }, // Length
129     DESCRIPTION: { min: 3, max: 3000 }, // Length
130     EXTNAME: [ '.mp4', '.ogv', '.webm' ],
131     INFO_HASH: { min: 40, max: 40 }, // Length, info hash is 20 bytes length but we represent it in hexadecimal so 20 * 2
132     DURATION: { min: 1, max: 7200 }, // Number
133     TAGS: { min: 0, max: 5 }, // Number of total tags
134     TAG: { min: 2, max: 30 }, // Length
135     THUMBNAIL: { min: 2, max: 30 },
136     THUMBNAIL_DATA: { min: 0, max: 20000 }, // Bytes
137     VIEWS: { min: 0 },
138     LIKES: { min: 0 },
139     DISLIKES: { min: 0 },
140     FILE_SIZE: { min: 10, max: 1024 * 1024 * 1024 * 3 /* 3Go */ }
141   },
142   VIDEO_EVENTS: {
143     COUNT: { min: 0 }
144   }
145 }
146
147 const VIDEO_RATE_TYPES: { [ id: string ]: VideoRateType } = {
148   LIKE: 'like',
149   DISLIKE: 'dislike'
150 }
151
152 const VIDEO_CATEGORIES = {
153   1: 'Music',
154   2: 'Films',
155   3: 'Vehicles',
156   4: 'Art',
157   5: 'Sports',
158   6: 'Travels',
159   7: 'Gaming',
160   8: 'People',
161   9: 'Comedy',
162   10: 'Entertainment',
163   11: 'News',
164   12: 'How To',
165   13: 'Education',
166   14: 'Activism',
167   15: 'Science & Technology',
168   16: 'Animals',
169   17: 'Kids',
170   18: 'Food'
171 }
172
173 // See https://creativecommons.org/licenses/?lang=en
174 const VIDEO_LICENCES = {
175   1: 'Attribution',
176   2: 'Attribution - Share Alike',
177   3: 'Attribution - No Derivatives',
178   4: 'Attribution - Non Commercial',
179   5: 'Attribution - Non Commercial - Share Alike',
180   6: 'Attribution - Non Commercial - No Derivatives',
181   7: 'Public Domain Dedication'
182 }
183
184 // See https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers#Nationalencyklopedin
185 const VIDEO_LANGUAGES = {
186   1: 'English',
187   2: 'Spanish',
188   3: 'Mandarin',
189   4: 'Hindi',
190   5: 'Arabic',
191   6: 'Portuguese',
192   7: 'Bengali',
193   8: 'Russian',
194   9: 'Japanese',
195   10: 'Punjabi',
196   11: 'German',
197   12: 'Korean',
198   13: 'French',
199   14: 'Italian'
200 }
201
202 const VIDEO_PRIVACIES = {
203   [VideoPrivacy.PUBLIC]: 'Public',
204   [VideoPrivacy.UNLISTED]: 'Unlisted',
205   [VideoPrivacy.PRIVATE]: 'Private'
206 }
207
208 const VIDEO_MIMETYPE_EXT = {
209   'video/webm': 'webm',
210   'video/ogg': 'ogv',
211   'video/mp4': 'mp4'
212 }
213
214 // ---------------------------------------------------------------------------
215
216 // Score a pod has when we create it as a friend
217 const FRIEND_SCORE = {
218   BASE: 100,
219   MAX: 1000
220 }
221
222 const ACTIVITY_PUB = {
223   COLLECTION_ITEMS_PER_PAGE: 10,
224   VIDEO_URL_MIME_TYPES: [
225     'video/mp4',
226     'video/webm',
227     'video/ogg',
228     'application/x-bittorrent',
229     'application/x-bittorrent;x-scheme-handler/magnet'
230   ]
231 }
232
233 // ---------------------------------------------------------------------------
234
235 // Number of points we add/remove from a friend after a successful/bad request
236 const PODS_SCORE = {
237   PENALTY: -10,
238   BONUS: 10
239 }
240
241 const FOLLOW_STATES: { [ id: string ]: FollowState } = {
242   PENDING: 'pending',
243   ACCEPTED: 'accepted'
244 }
245
246 const REMOTE_SCHEME = {
247   HTTP: 'https',
248   WS: 'wss'
249 }
250
251 const JOB_STATES: { [ id: string ]: JobState } = {
252   PENDING: 'pending',
253   PROCESSING: 'processing',
254   ERROR: 'error',
255   SUCCESS: 'success'
256 }
257 const JOB_CATEGORIES: { [ id: string ]: JobCategory } = {
258   TRANSCODING: 'transcoding',
259   HTTP_REQUEST: 'http-request'
260 }
261 // How many maximum jobs we fetch from the database per cycle
262 const JOBS_FETCH_LIMIT_PER_CYCLE = {
263   transcoding: 10,
264   httpRequest: 20
265 }
266 // 1 minutes
267 let JOBS_FETCHING_INTERVAL = 60000
268
269 // ---------------------------------------------------------------------------
270
271 const PRIVATE_RSA_KEY_SIZE = 2048
272
273 // Password encryption
274 const BCRYPT_SALT_SIZE = 10
275
276 // ---------------------------------------------------------------------------
277
278 // Express static paths (router)
279 const STATIC_PATHS = {
280   PREVIEWS: '/static/previews/',
281   THUMBNAILS: '/static/thumbnails/',
282   TORRENTS: '/static/torrents/',
283   WEBSEED: '/static/webseed/'
284 }
285
286 // Cache control
287 let STATIC_MAX_AGE = '30d'
288
289 // Videos thumbnail size
290 const THUMBNAILS_SIZE = {
291   width: 200,
292   height: 110
293 }
294 const PREVIEWS_SIZE = {
295   width: 560,
296   height: 315
297 }
298
299 const EMBED_SIZE = {
300   width: 560,
301   height: 315
302 }
303
304 // Sub folders of cache directory
305 const CACHE = {
306   DIRECTORIES: {
307     PREVIEWS: join(CONFIG.STORAGE.CACHE_DIR, 'previews')
308   }
309 }
310
311 // ---------------------------------------------------------------------------
312
313 const OPENGRAPH_AND_OEMBED_COMMENT = '<!-- open graph and oembed tags -->'
314
315 // ---------------------------------------------------------------------------
316
317 // Special constants for a test instance
318 if (isTestInstance() === true) {
319   CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
320   FRIEND_SCORE.BASE = 20
321   JOBS_FETCHING_INTERVAL = 10000
322   REMOTE_SCHEME.HTTP = 'http'
323   REMOTE_SCHEME.WS = 'ws'
324   STATIC_MAX_AGE = '0'
325 }
326
327 // ---------------------------------------------------------------------------
328
329 export {
330   API_VERSION,
331   BCRYPT_SALT_SIZE,
332   CACHE,
333   CONFIG,
334   CONSTRAINTS_FIELDS,
335   EMBED_SIZE,
336   FRIEND_SCORE,
337   JOB_STATES,
338   JOBS_FETCH_LIMIT_PER_CYCLE,
339   JOBS_FETCHING_INTERVAL,
340   JOB_CATEGORIES,
341   LAST_MIGRATION_VERSION,
342   OAUTH_LIFETIME,
343   OPENGRAPH_AND_OEMBED_COMMENT,
344   PAGINATION_COUNT_DEFAULT,
345   PODS_SCORE,
346   PREVIEWS_SIZE,
347   REMOTE_SCHEME,
348   FOLLOW_STATES,
349   SEARCHABLE_COLUMNS,
350   PRIVATE_RSA_KEY_SIZE,
351   SORTABLE_COLUMNS,
352   STATIC_MAX_AGE,
353   STATIC_PATHS,
354   ACTIVITY_PUB,
355   THUMBNAILS_SIZE,
356   VIDEO_CATEGORIES,
357   VIDEO_LANGUAGES,
358   VIDEO_PRIVACIES,
359   VIDEO_LICENCES,
360   VIDEO_RATE_TYPES,
361   VIDEO_MIMETYPE_EXT
362 }