]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/config.ts
Fix lint
[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'
36f9424f 4import { About } from '../../../shared/models/config/about.model'
fd206f0b
C
5import { CustomConfig } from '../../../shared/models/config/custom-config.model'
6import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils'
da854ddd 7import { isSignupAllowed } from '../../helpers/utils'
fd206f0b
C
8import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
9import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
10import { customConfigUpdateValidator } from '../../middlewares/validators/config'
65fcc311 11
915c5bbe 12const packageJSON = require('../../../../package.json')
65fcc311
C
13const configRouter = express.Router()
14
36f9424f 15configRouter.get('/about', getAbout)
eb080476
C
16configRouter.get('/',
17 asyncMiddleware(getConfig)
18)
36f9424f 19
fd206f0b
C
20configRouter.get('/custom',
21 authenticate,
22 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
23 asyncMiddleware(getCustomConfig)
24)
25configRouter.put('/custom',
26 authenticate,
27 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
28 asyncMiddleware(customConfigUpdateValidator),
29 asyncMiddleware(updateCustomConfig)
30)
31configRouter.delete('/custom',
32 authenticate,
33 ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
34 asyncMiddleware(deleteCustomConfig)
35)
65fcc311 36
eb080476
C
37async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
38 const allowed = await isSignupAllowed()
291e8d3e 39
eb080476
C
40 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
41 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
42 .map(r => parseInt(r, 10))
6a84aafd 43
eb080476 44 const json: ServerConfig = {
36f9424f
C
45 instance: {
46 name: CONFIG.INSTANCE.NAME
47 },
915c5bbe 48 serverVersion: packageJSON.version,
eb080476
C
49 signup: {
50 allowed
51 },
52 transcoding: {
53 enabledResolutions
01de67b9
C
54 },
55 avatar: {
56 file: {
57 size: {
58 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
59 },
60 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
61 }
62 },
63 video: {
6de36768
C
64 image: {
65 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
66 size: {
67 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
68 }
69 },
01de67b9
C
70 file: {
71 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
72 }
65fcc311 73 }
eb080476 74 }
6a84aafd 75
eb080476 76 return res.json(json)
65fcc311
C
77}
78
36f9424f
C
79function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
80 const about: About = {
81 instance: {
82 name: CONFIG.INSTANCE.NAME,
83 description: CONFIG.INSTANCE.DESCRIPTION,
84 terms: CONFIG.INSTANCE.TERMS
85 }
86 }
87
88 return res.json(about).end()
89}
90
fd206f0b
C
91async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
92 const data = customConfig()
93
94 return res.json(data).end()
95}
96
97async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
98 await unlinkPromise(CONFIG.CUSTOM_FILE)
99
100 reloadConfig()
101
102 const data = customConfig()
103
104 return res.json(data).end()
105}
106
107async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
108 const toUpdate: CustomConfig = req.body
109
110 // Need to change the videoQuota key a little bit
111 const toUpdateJSON = omit(toUpdate, 'videoQuota')
112 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
113
114 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON))
115
116 reloadConfig()
117
118 const data = customConfig()
119 return res.json(data).end()
120}
121
65fcc311
C
122// ---------------------------------------------------------------------------
123
124export {
125 configRouter
126}
fd206f0b
C
127
128// ---------------------------------------------------------------------------
129
130function customConfig (): CustomConfig {
131 return {
66b16caf
C
132 instance: {
133 name: CONFIG.INSTANCE.NAME,
134 description: CONFIG.INSTANCE.DESCRIPTION,
135 terms: CONFIG.INSTANCE.TERMS
136 },
fd206f0b
C
137 cache: {
138 previews: {
139 size: CONFIG.CACHE.PREVIEWS.SIZE
140 }
141 },
142 signup: {
143 enabled: CONFIG.SIGNUP.ENABLED,
144 limit: CONFIG.SIGNUP.LIMIT
145 },
146 admin: {
147 email: CONFIG.ADMIN.EMAIL
148 },
149 user: {
150 videoQuota: CONFIG.USER.VIDEO_QUOTA
151 },
152 transcoding: {
153 enabled: CONFIG.TRANSCODING.ENABLED,
154 threads: CONFIG.TRANSCODING.THREADS,
155 resolutions: {
156 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
157 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
158 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
159 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
160 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
161 }
162 }
163 }
164}