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