]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/config.ts
Add ability to reset our password
[github/Chocobozzz/PeerTube.git] / server / controllers / api / config.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
fd206f0b
C
2import { ServerConfig, UserRight } from '../../../shared'
3import { CustomConfig } from '../../../shared/models/config/custom-config.model'
4import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils'
da854ddd 5import { isSignupAllowed } from '../../helpers/utils'
fd206f0b
C
6import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
7import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
8import { customConfigUpdateValidator } from '../../middlewares/validators/config'
9import { omit } from 'lodash'
65fcc311
C
10
11const configRouter = express.Router()
12
eb080476
C
13configRouter.get('/',
14 asyncMiddleware(getConfig)
15)
fd206f0b
C
16configRouter.get('/custom',
17 authenticate,
18 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
19 asyncMiddleware(getCustomConfig)
20)
21configRouter.put('/custom',
22 authenticate,
23 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
24 asyncMiddleware(customConfigUpdateValidator),
25 asyncMiddleware(updateCustomConfig)
26)
27configRouter.delete('/custom',
28 authenticate,
29 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
30 asyncMiddleware(deleteCustomConfig)
31)
65fcc311 32
eb080476
C
33async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
34 const allowed = await isSignupAllowed()
291e8d3e 35
eb080476
C
36 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
37 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
38 .map(r => parseInt(r, 10))
6a84aafd 39
eb080476
C
40 const json: ServerConfig = {
41 signup: {
42 allowed
43 },
44 transcoding: {
45 enabledResolutions
01de67b9
C
46 },
47 avatar: {
48 file: {
49 size: {
50 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
51 },
52 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
53 }
54 },
55 video: {
56 file: {
57 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
58 }
65fcc311 59 }
eb080476 60 }
6a84aafd 61
eb080476 62 return res.json(json)
65fcc311
C
63}
64
fd206f0b
C
65async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
66 const data = customConfig()
67
68 return res.json(data).end()
69}
70
71async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
72 await unlinkPromise(CONFIG.CUSTOM_FILE)
73
74 reloadConfig()
75
76 const data = customConfig()
77
78 return res.json(data).end()
79}
80
81async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
82 const toUpdate: CustomConfig = req.body
83
84 // Need to change the videoQuota key a little bit
85 const toUpdateJSON = omit(toUpdate, 'videoQuota')
86 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
87
88 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON))
89
90 reloadConfig()
91
92 const data = customConfig()
93 return res.json(data).end()
94}
95
65fcc311
C
96// ---------------------------------------------------------------------------
97
98export {
99 configRouter
100}
fd206f0b
C
101
102// ---------------------------------------------------------------------------
103
104function customConfig (): CustomConfig {
105 return {
106 cache: {
107 previews: {
108 size: CONFIG.CACHE.PREVIEWS.SIZE
109 }
110 },
111 signup: {
112 enabled: CONFIG.SIGNUP.ENABLED,
113 limit: CONFIG.SIGNUP.LIMIT
114 },
115 admin: {
116 email: CONFIG.ADMIN.EMAIL
117 },
118 user: {
119 videoQuota: CONFIG.USER.VIDEO_QUOTA
120 },
121 transcoding: {
122 enabled: CONFIG.TRANSCODING.ENABLED,
123 threads: CONFIG.TRANSCODING.THREADS,
124 resolutions: {
125 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
126 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
127 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
128 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
129 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
130 }
131 }
132 }
133}