]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/config.ts
Add ability to import video with youtube-dl
[github/Chocobozzz/PeerTube.git] / server / controllers / api / config.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { omit } from 'lodash'
3import { ServerConfig, UserRight } from '../../../shared'
4import { About } from '../../../shared/models/server/about.model'
5import { CustomConfig } from '../../../shared/models/server/custom-config.model'
6import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils'
7import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/utils'
8import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
9import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
10import { customConfigUpdateValidator } from '../../middlewares/validators/config'
11import { ClientHtml } from '../../lib/client-html'
12import { CustomConfigAuditView, auditLoggerFactory } from '../../helpers/audit-logger'
13
14const packageJSON = require('../../../../package.json')
15const configRouter = express.Router()
16
17const auditLogger = auditLoggerFactory('config')
18
19configRouter.get('/about', getAbout)
20configRouter.get('/',
21 asyncMiddleware(getConfig)
22)
23
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)
40
41async function getConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
42 const allowed = await isSignupAllowed()
43 const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
44
45 const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
46 .filter(key => CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
47 .map(r => parseInt(r, 10))
48
49 const json: ServerConfig = {
50 instance: {
51 name: CONFIG.INSTANCE.NAME,
52 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
53 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
54 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
55 customizations: {
56 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
57 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
58 }
59 },
60 serverVersion: packageJSON.version,
61 signup: {
62 allowed,
63 allowedForCurrentIP
64 },
65 transcoding: {
66 enabledResolutions
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: {
77 image: {
78 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
79 size: {
80 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
81 }
82 },
83 file: {
84 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
85 }
86 },
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 },
95 user: {
96 videoQuota: CONFIG.USER.VIDEO_QUOTA
97 }
98 }
99
100 return res.json(json)
101}
102
103function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
104 const about: About = {
105 instance: {
106 name: CONFIG.INSTANCE.NAME,
107 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
108 description: CONFIG.INSTANCE.DESCRIPTION,
109 terms: CONFIG.INSTANCE.TERMS
110 }
111 }
112
113 return res.json(about).end()
114}
115
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
125 auditLogger.delete(
126 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
127 new CustomConfigAuditView(customConfig())
128 )
129
130 reloadConfig()
131 ClientHtml.invalidCache()
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
140 const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
141
142 // Force number conversion
143 toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
144 toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
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
150 const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription', 'cache.videoCaptions')
151 toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
152 toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
153 toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
154 toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
155
156 await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
157
158 reloadConfig()
159 ClientHtml.invalidCache()
160
161 const data = customConfig()
162
163 auditLogger.update(
164 res.locals.oauth.token.User.Account.Actor.getIdentifier(),
165 new CustomConfigAuditView(data),
166 oldCustomConfigAuditKeys
167 )
168
169 return res.json(data).end()
170}
171
172// ---------------------------------------------------------------------------
173
174export {
175 configRouter
176}
177
178// ---------------------------------------------------------------------------
179
180function customConfig (): CustomConfig {
181 return {
182 instance: {
183 name: CONFIG.INSTANCE.NAME,
184 shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
185 description: CONFIG.INSTANCE.DESCRIPTION,
186 terms: CONFIG.INSTANCE.TERMS,
187 defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
188 defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
189 customizations: {
190 css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
191 javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
192 }
193 },
194 services: {
195 twitter: {
196 username: CONFIG.SERVICES.TWITTER.USERNAME,
197 whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
198 }
199 },
200 cache: {
201 previews: {
202 size: CONFIG.CACHE.PREVIEWS.SIZE
203 },
204 captions: {
205 size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
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}