]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/config.ts
Add ability to import video with youtube-dl
[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'
fd206f0b 6import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils'
ff2c1fe8 7import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/utils'
fd206f0b
C
8import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
9import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
10import { customConfigUpdateValidator } from '../../middlewares/validators/config'
e032aec9 11import { ClientHtml } from '../../lib/client-html'
80e36cd9 12import { CustomConfigAuditView, auditLoggerFactory } from '../../helpers/audit-logger'
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
C
45 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
46 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
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
RK
62 allowed,
63 allowedForCurrentIP
eb080476
C
64 },
65 transcoding: {
66 enabledResolutions
01de67b9
C
67 },
68 avatar: {
69 file: {
70 size: {
71 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
72 },
73 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
74 }
75 },
76 video: {
6de36768
C
77 image: {
78 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
79 size: {
80 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
81 }
82 },
01de67b9
C
83 file: {
84 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
85 }
1869c875 86 },
40e87e9e
C
87 videoCaption: {
88 file: {
89 size: {
90 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
91 },
92 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
93 }
94 },
1869c875
RK
95 user: {
96 videoQuota: CONFIG.USER.VIDEO_QUOTA
65fcc311 97 }
eb080476 98 }
6a84aafd 99
eb080476 100 return res.json(json)
65fcc311
C
101}
102
36f9424f
C
103function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
104 const about: About = {
105 instance: {
106 name: CONFIG.INSTANCE.NAME,
2e3a0215 107 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
36f9424f
C
108 description: CONFIG.INSTANCE.DESCRIPTION,
109 terms: CONFIG.INSTANCE.TERMS
110 }
111 }
112
113 return res.json(about).end()
114}
115
fd206f0b
C
116async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
117 const data = customConfig()
118
119 return res.json(data).end()
120}
121
122async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
123 await unlinkPromise(CONFIG.CUSTOM_FILE)
124
80e36cd9
AB
125 auditLogger.delete(
126 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
127 new CustomConfigAuditView(customConfig())
128 )
129
fd206f0b 130 reloadConfig()
e032aec9 131 ClientHtml.invalidCache()
fd206f0b
C
132
133 const data = customConfig()
134
135 return res.json(data).end()
136}
137
138async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
139 const toUpdate: CustomConfig = req.body
80e36cd9 140 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
fd206f0b 141
be1fc4bc
C
142 // Force number conversion
143 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
40e87e9e 144 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
be1fc4bc
C
145 toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
146 toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
147 toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
148
149 // camelCase to snake_case key
40e87e9e 150 const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription', 'cache.videoCaptions')
fd206f0b 151 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
901637bb 152 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
2e3a0215 153 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
0883b324 154 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
fd206f0b 155
2ad42952 156 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
fd206f0b
C
157
158 reloadConfig()
e032aec9 159 ClientHtml.invalidCache()
fd206f0b
C
160
161 const data = customConfig()
80e36cd9
AB
162
163 auditLogger.update(
164 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
165 new CustomConfigAuditView(data),
166 oldCustomConfigAuditKeys
167 )
168
fd206f0b
C
169 return res.json(data).end()
170}
171
65fcc311
C
172// ---------------------------------------------------------------------------
173
174export {
175 configRouter
176}
fd206f0b
C
177
178// ---------------------------------------------------------------------------
179
180function customConfig (): CustomConfig {
181 return {
66b16caf
C
182 instance: {
183 name: CONFIG.INSTANCE.NAME,
2e3a0215 184 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
66b16caf 185 description: CONFIG.INSTANCE.DESCRIPTION,
00b5556c 186 terms: CONFIG.INSTANCE.TERMS,
901637bb 187 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
0883b324 188 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
00b5556c
C
189 customizations: {
190 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
191 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
192 }
66b16caf 193 },
8be1afa1
C
194 services: {
195 twitter: {
196 username: CONFIG.SERVICES.TWITTER.USERNAME,
197 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
198 }
199 },
fd206f0b
C
200 cache: {
201 previews: {
202 size: CONFIG.CACHE.PREVIEWS.SIZE
40e87e9e
C
203 },
204 captions: {
205 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
fd206f0b
C
206 }
207 },
208 signup: {
209 enabled: CONFIG.SIGNUP.ENABLED,
210 limit: CONFIG.SIGNUP.LIMIT
211 },
212 admin: {
213 email: CONFIG.ADMIN.EMAIL
214 },
215 user: {
216 videoQuota: CONFIG.USER.VIDEO_QUOTA
217 },
218 transcoding: {
219 enabled: CONFIG.TRANSCODING.ENABLED,
220 threads: CONFIG.TRANSCODING.THREADS,
221 resolutions: {
222 '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
223 '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
224 '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
225 '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
226 '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
227 }
228 }
229 }
230}