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