]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/config.ts
Fix overview cache
[github/Chocobozzz/PeerTube.git] / server / controllers / api / config.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
36f9424f 2import { omit } from 'lodash'
fd206f0b 3import { ServerConfig, UserRight } from '../../../shared'
09cababd
C
4import { About } from '../../../shared/models/server/about.model'
5import { CustomConfig } from '../../../shared/models/server/custom-config.model'
06215f15 6import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
fd206f0b
C
7import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
8import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
9import { customConfigUpdateValidator } from '../../middlewares/validators/config'
e032aec9 10import { ClientHtml } from '../../lib/client-html'
a84b8fa5 11import { auditLoggerFactory, CustomConfigAuditView } from '../../helpers/audit-logger'
62689b94 12import { remove, writeJSON } from 'fs-extra'
65fcc311 13
915c5bbe 14const packageJSON = require('../../../../package.json')
65fcc311
C
15const configRouter = express.Router()
16
80e36cd9
AB
17const auditLogger = auditLoggerFactory('config')
18
36f9424f 19configRouter.get('/about', getAbout)
eb080476
C
20configRouter.get('/',
21 asyncMiddleware(getConfig)
22)
36f9424f 23
fd206f0b
C
24configRouter.get('/custom',
25 authenticate,
26 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
27 asyncMiddleware(getCustomConfig)
28)
29configRouter.put('/custom',
30 authenticate,
31 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
32 asyncMiddleware(customConfigUpdateValidator),
33 asyncMiddleware(updateCustomConfig)
34)
35configRouter.delete('/custom',
36 authenticate,
37 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
38 asyncMiddleware(deleteCustomConfig)
39)
65fcc311 40
eb080476
C
41async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
42 const allowed = await isSignupAllowed()
ff2c1fe8 43 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
291e8d3e 44
eb080476 45 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
41a676db 46 .filter(key => CONFIG.TRANSCODING.ENABLED === CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
eb080476 47 .map(r => parseInt(r, 10))
6a84aafd 48
eb080476 49 const json: ServerConfig = {
36f9424f 50 instance: {
00b5556c 51 name: CONFIG.INSTANCE.NAME,
2e3a0215 52 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
901637bb 53 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
0883b324 54 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
00b5556c
C
55 customizations: {
56 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
57 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
58 }
36f9424f 59 },
915c5bbe 60 serverVersion: packageJSON.version,
eb080476 61 signup: {
ff2c1fe8 62 allowed,
d9eaee39
JM
63 allowedForCurrentIP,
64 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
eb080476
C
65 },
66 transcoding: {
67 enabledResolutions
01de67b9 68 },
5d08a6a7 69 import: {
b2977eec 70 videos: {
5d08a6a7
C
71 http: {
72 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
a84b8fa5
C
73 },
74 torrent: {
75 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
5d08a6a7
C
76 }
77 }
78 },
01de67b9
C
79 avatar: {
80 file: {
81 size: {
82 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
83 },
84 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
85 }
86 },
87 video: {
6de36768
C
88 image: {
89 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
90 size: {
91 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
92 }
93 },
01de67b9
C
94 file: {
95 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
96 }
1869c875 97 },
40e87e9e
C
98 videoCaption: {
99 file: {
100 size: {
101 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
102 },
103 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
104 }
105 },
1869c875 106 user: {
bee0abff
FA
107 videoQuota: CONFIG.USER.VIDEO_QUOTA,
108 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
65fcc311 109 }
eb080476 110 }
6a84aafd 111
eb080476 112 return res.json(json)
65fcc311
C
113}
114
36f9424f
C
115function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
116 const about: About = {
117 instance: {
118 name: CONFIG.INSTANCE.NAME,
2e3a0215 119 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
36f9424f
C
120 description: CONFIG.INSTANCE.DESCRIPTION,
121 terms: CONFIG.INSTANCE.TERMS
122 }
123 }
124
125 return res.json(about).end()
126}
127
fd206f0b
C
128async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
129 const data = customConfig()
130
131 return res.json(data).end()
132}
133
134async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
62689b94 135 await remove(CONFIG.CUSTOM_FILE)
fd206f0b 136
80e36cd9
AB
137 auditLogger.delete(
138 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
139 new CustomConfigAuditView(customConfig())
140 )
141
fd206f0b 142 reloadConfig()
e032aec9 143 ClientHtml.invalidCache()
fd206f0b
C
144
145 const data = customConfig()
146
147 return res.json(data).end()
148}
149
150async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
151 const toUpdate: CustomConfig = req.body
80e36cd9 152 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
fd206f0b 153
be1fc4bc
C
154 // Force number conversion
155 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
40e87e9e 156 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
be1fc4bc
C
157 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
158 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
bee0abff 159 toUpdate.user.videoQuotaDaily = parseInt('' + toUpdate.user.videoQuotaDaily, 10)
be1fc4bc
C
160 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
161
162 // camelCase to snake_case key
d9eaee39
JM
163 const toUpdateJSON = omit(
164 toUpdate,
165 'user.videoQuota',
166 'instance.defaultClientRoute',
167 'instance.shortDescription',
168 'cache.videoCaptions',
169 'signup.requiresEmailVerification'
170 )
fd206f0b 171 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
9ee92651 172 toUpdateJSON.user['video_quota_daily'] = toUpdate.user.videoQuotaDaily
901637bb 173 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
2e3a0215 174 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
0883b324 175 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
d9eaee39 176 toUpdateJSON.signup['requires_email_verification'] = toUpdate.signup.requiresEmailVerification
fd206f0b 177
62689b94 178 await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
fd206f0b
C
179
180 reloadConfig()
e032aec9 181 ClientHtml.invalidCache()
fd206f0b
C
182
183 const data = customConfig()
80e36cd9
AB
184
185 auditLogger.update(
186 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
187 new CustomConfigAuditView(data),
188 oldCustomConfigAuditKeys
189 )
190
fd206f0b
C
191 return res.json(data).end()
192}
193
65fcc311
C
194// ---------------------------------------------------------------------------
195
196export {
197 configRouter
198}
fd206f0b
C
199
200// ---------------------------------------------------------------------------
201
202function customConfig (): CustomConfig {
203 return {
66b16caf
C
204 instance: {
205 name: CONFIG.INSTANCE.NAME,
2e3a0215 206 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
66b16caf 207 description: CONFIG.INSTANCE.DESCRIPTION,
00b5556c 208 terms: CONFIG.INSTANCE.TERMS,
901637bb 209 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
0883b324 210 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
00b5556c
C
211 customizations: {
212 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
213 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
214 }
66b16caf 215 },
8be1afa1
C
216 services: {
217 twitter: {
218 username: CONFIG.SERVICES.TWITTER.USERNAME,
219 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
220 }
221 },
fd206f0b
C
222 cache: {
223 previews: {
224 size: CONFIG.CACHE.PREVIEWS.SIZE
40e87e9e
C
225 },
226 captions: {
227 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
fd206f0b
C
228 }
229 },
230 signup: {
231 enabled: CONFIG.SIGNUP.ENABLED,
d9eaee39
JM
232 limit: CONFIG.SIGNUP.LIMIT,
233 requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
fd206f0b
C
234 },
235 admin: {
236 email: CONFIG.ADMIN.EMAIL
237 },
238 user: {
bee0abff
FA
239 videoQuota: CONFIG.USER.VIDEO_QUOTA,
240 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
fd206f0b
C
241 },
242 transcoding: {
243 enabled: CONFIG.TRANSCODING.ENABLED,
244 threads: CONFIG.TRANSCODING.THREADS,
245 resolutions: {
246 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
247 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
248 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
249 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
250 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
251 }
5d08a6a7
C
252 },
253 import: {
254 videos: {
255 http: {
256 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
a84b8fa5
C
257 },
258 torrent: {
259 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
5d08a6a7
C
260 }
261 }
fd206f0b
C
262 }
263 }
264}