Update migrations code
[oweals/peertube.git] / server / initializers / constants.js
1 'use strict'
2
3 const config = require('config')
4 const maxBy = require('lodash/maxBy')
5 const path = require('path')
6
7 // ---------------------------------------------------------------------------
8
9 // API version
10 const API_VERSION = 'v1'
11
12 // Number of results by default for the pagination
13 const PAGINATION_COUNT_DEFAULT = 15
14
15 // Sortable columns per schema
16 const SEARCHABLE_COLUMNS = {
17   VIDEOS: [ 'name', 'magnetUri', 'host', 'author', 'tags' ]
18 }
19
20 // Sortable columns per schema
21 const SORTABLE_COLUMNS = {
22   USERS: [ 'username', '-username', 'createdAt', '-createdAt' ],
23   VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdAt', '-createdAt' ]
24 }
25
26 const OAUTH_LIFETIME = {
27   ACCESS_TOKEN: 3600 * 4, // 4 hours
28   REFRESH_TOKEN: 1209600 // 2 weeks
29 }
30
31 // ---------------------------------------------------------------------------
32
33 const CONFIG = {
34   LISTEN: {
35     PORT: config.get('listen.port')
36   },
37   DATABASE: {
38     DBNAME: 'peertube' + config.get('database.suffix'),
39     HOSTNAME: config.get('database.hostname'),
40     PORT: config.get('database.port'),
41     USERNAME: config.get('database.username'),
42     PASSWORD: config.get('database.password')
43   },
44   STORAGE: {
45     CERT_DIR: path.join(__dirname, '..', '..', config.get('storage.certs')),
46     LOG_DIR: path.join(__dirname, '..', '..', config.get('storage.logs')),
47     VIDEOS_DIR: path.join(__dirname, '..', '..', config.get('storage.videos')),
48     THUMBNAILS_DIR: path.join(__dirname, '..', '..', config.get('storage.thumbnails')),
49     PREVIEWS_DIR: path.join(__dirname, '..', '..', config.get('storage.previews')),
50     TORRENTS_DIR: path.join(__dirname, '..', '..', config.get('storage.torrents'))
51   },
52   WEBSERVER: {
53     SCHEME: config.get('webserver.https') === true ? 'https' : 'http',
54     WS: config.get('webserver.https') === true ? 'wss' : 'ws',
55     HOSTNAME: config.get('webserver.hostname'),
56     PORT: config.get('webserver.port')
57   }
58 }
59 CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
60 CONFIG.WEBSERVER.HOST = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
61
62 // ---------------------------------------------------------------------------
63
64 const CONSTRAINTS_FIELDS = {
65   USERS: {
66     USERNAME: { min: 3, max: 20 }, // Length
67     PASSWORD: { min: 6, max: 255 } // Length
68   },
69   VIDEOS: {
70     NAME: { min: 3, max: 50 }, // Length
71     DESCRIPTION: { min: 3, max: 250 }, // Length
72     EXTNAME: [ '.mp4', '.ogv', '.webm' ],
73     INFO_HASH: { min: 10, max: 50 }, // Length
74     DURATION: { min: 1, max: 7200 }, // Number
75     TAGS: { min: 1, max: 3 }, // Number of total tags
76     TAG: { min: 2, max: 10 }, // Length
77     THUMBNAIL: { min: 2, max: 30 },
78     THUMBNAIL64: { min: 0, max: 20000 } // Bytes
79   }
80 }
81
82 // ---------------------------------------------------------------------------
83
84 // Score a pod has when we create it as a friend
85 const FRIEND_SCORE = {
86   BASE: 100,
87   MAX: 1000
88 }
89
90 // ---------------------------------------------------------------------------
91
92 const LAST_MIGRATION_VERSION = 0
93
94 // ---------------------------------------------------------------------------
95
96 // Number of points we add/remove from a friend after a successful/bad request
97 const PODS_SCORE = {
98   MALUS: -10,
99   BONUS: 10
100 }
101
102 // Time to wait between requests to the friends (10 min)
103 let REQUESTS_INTERVAL = 600000
104
105 // Number of requests in parallel we can make
106 const REQUESTS_IN_PARALLEL = 10
107
108 // How many requests we put in request
109 const REQUESTS_LIMIT = 10
110
111 // Number of requests to retry for replay requests module
112 const RETRY_REQUESTS = 5
113
114 const REQUEST_ENDPOINTS = {
115   VIDEOS: 'videos'
116 }
117
118 // ---------------------------------------------------------------------------
119
120 const REMOTE_SCHEME = {
121   HTTP: 'https',
122   WS: 'wss'
123 }
124
125 // Password encryption
126 const BCRYPT_SALT_SIZE = 10
127
128 // Express static paths (router)
129 const STATIC_PATHS = {
130   PREVIEWS: '/static/previews/',
131   THUMBNAILS: '/static/thumbnails/',
132   TORRENTS: '/static/torrents/',
133   WEBSEED: '/static/webseed/'
134 }
135
136 // Cache control
137 let STATIC_MAX_AGE = '30d'
138
139 // Videos thumbnail size
140 const THUMBNAILS_SIZE = '200x110'
141 const PREVIEWS_SIZE = '640x480'
142
143 const USER_ROLES = {
144   ADMIN: 'admin',
145   USER: 'user'
146 }
147
148 // ---------------------------------------------------------------------------
149
150 // Special constants for a test instance
151 if (isTestInstance() === true) {
152   CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14
153   FRIEND_SCORE.BASE = 20
154   REQUESTS_INTERVAL = 10000
155   REMOTE_SCHEME.HTTP = 'http'
156   REMOTE_SCHEME.WS = 'ws'
157   STATIC_MAX_AGE = 0
158 }
159
160 // ---------------------------------------------------------------------------
161
162 module.exports = {
163   API_VERSION,
164   BCRYPT_SALT_SIZE,
165   CONFIG,
166   CONSTRAINTS_FIELDS,
167   FRIEND_SCORE,
168   LAST_MIGRATION_VERSION,
169   OAUTH_LIFETIME,
170   PAGINATION_COUNT_DEFAULT,
171   PODS_SCORE,
172   PREVIEWS_SIZE,
173   REMOTE_SCHEME,
174   REQUEST_ENDPOINTS,
175   REQUESTS_IN_PARALLEL,
176   REQUESTS_INTERVAL,
177   REQUESTS_LIMIT,
178   RETRY_REQUESTS,
179   SEARCHABLE_COLUMNS,
180   SORTABLE_COLUMNS,
181   STATIC_MAX_AGE,
182   STATIC_PATHS,
183   THUMBNAILS_SIZE,
184   USER_ROLES
185 }
186
187 // ---------------------------------------------------------------------------
188
189 // This method exists in utils module but we want to let the constants module independent
190 function isTestInstance () {
191   return (process.env.NODE_ENV === 'test')
192 }