]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/config.ts
Add messages about privacy concerns (P2P)
[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 45 instance: {
00b5556c
C
46 name: CONFIG.INSTANCE.NAME,
47 customizations: {
48 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
49 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
50 }
36f9424f 51 },
915c5bbe 52 serverVersion: packageJSON.version,
eb080476
C
53 signup: {
54 allowed
55 },
56 transcoding: {
57 enabledResolutions
01de67b9
C
58 },
59 avatar: {
60 file: {
61 size: {
62 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
63 },
64 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
65 }
66 },
67 video: {
6de36768
C
68 image: {
69 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
70 size: {
71 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
72 }
73 },
01de67b9
C
74 file: {
75 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
76 }
65fcc311 77 }
eb080476 78 }
6a84aafd 79
eb080476 80 return res.json(json)
65fcc311
C
81}
82
36f9424f
C
83function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
84 const about: About = {
85 instance: {
86 name: CONFIG.INSTANCE.NAME,
87 description: CONFIG.INSTANCE.DESCRIPTION,
88 terms: CONFIG.INSTANCE.TERMS
89 }
90 }
91
92 return res.json(about).end()
93}
94
fd206f0b
C
95async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
96 const data = customConfig()
97
98 return res.json(data).end()
99}
100
101async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
102 await unlinkPromise(CONFIG.CUSTOM_FILE)
103
104 reloadConfig()
105
106 const data = customConfig()
107
108 return res.json(data).end()
109}
110
111async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
112 const toUpdate: CustomConfig = req.body
113
114 // Need to change the videoQuota key a little bit
115 const toUpdateJSON = omit(toUpdate, 'videoQuota')
116 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
509cd56a 117 delete toUpdate.user.videoQuota
fd206f0b 118
2ad42952 119 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
fd206f0b
C
120
121 reloadConfig()
122
123 const data = customConfig()
124 return res.json(data).end()
125}
126
65fcc311
C
127// ---------------------------------------------------------------------------
128
129export {
130 configRouter
131}
fd206f0b
C
132
133// ---------------------------------------------------------------------------
134
135function customConfig (): CustomConfig {
136 return {
66b16caf
C
137 instance: {
138 name: CONFIG.INSTANCE.NAME,
139 description: CONFIG.INSTANCE.DESCRIPTION,
00b5556c
C
140 terms: CONFIG.INSTANCE.TERMS,
141 customizations: {
142 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
143 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
144 }
66b16caf 145 },
fd206f0b
C
146 cache: {
147 previews: {
148 size: CONFIG.CACHE.PREVIEWS.SIZE
149 }
150 },
151 signup: {
152 enabled: CONFIG.SIGNUP.ENABLED,
153 limit: CONFIG.SIGNUP.LIMIT
154 },
155 admin: {
156 email: CONFIG.ADMIN.EMAIL
157 },
158 user: {
159 videoQuota: CONFIG.USER.VIDEO_QUOTA
160 },
161 transcoding: {
162 enabled: CONFIG.TRANSCODING.ENABLED,
163 threads: CONFIG.TRANSCODING.THREADS,
164 resolutions: {
165 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
166 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
167 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
168 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
169 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
170 }
171 }
172 }
173}