Add import http enabled configuration
[oweals/peertube.git] / server / controllers / api / config.ts
1 import * as express from 'express'
2 import { omit } from 'lodash'
3 import { ServerConfig, UserRight } from '../../../shared'
4 import { About } from '../../../shared/models/server/about.model'
5 import { CustomConfig } from '../../../shared/models/server/custom-config.model'
6 import { unlinkPromise, writeFilePromise } from '../../helpers/core-utils'
7 import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/utils'
8 import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
9 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
10 import { customConfigUpdateValidator } from '../../middlewares/validators/config'
11 import { ClientHtml } from '../../lib/client-html'
12 import { CustomConfigAuditView, auditLoggerFactory } from '../../helpers/audit-logger'
13
14 const packageJSON = require('../../../../package.json')
15 const configRouter = express.Router()
16
17 const auditLogger = auditLoggerFactory('config')
18
19 configRouter.get('/about', getAbout)
20 configRouter.get('/',
21   asyncMiddleware(getConfig)
22 )
23
24 configRouter.get('/custom',
25   authenticate,
26   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
27   asyncMiddleware(getCustomConfig)
28 )
29 configRouter.put('/custom',
30   authenticate,
31   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
32   asyncMiddleware(customConfigUpdateValidator),
33   asyncMiddleware(updateCustomConfig)
34 )
35 configRouter.delete('/custom',
36   authenticate,
37   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
38   asyncMiddleware(deleteCustomConfig)
39 )
40
41 async 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     import: {
69       video: {
70         http: {
71           enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
72         }
73       }
74     },
75     avatar: {
76       file: {
77         size: {
78           max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
79         },
80         extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
81       }
82     },
83     video: {
84       image: {
85         extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
86         size: {
87           max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
88         }
89       },
90       file: {
91         extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
92       }
93     },
94     videoCaption: {
95       file: {
96         size: {
97           max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
98         },
99         extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
100       }
101     },
102     user: {
103       videoQuota: CONFIG.USER.VIDEO_QUOTA
104     }
105   }
106
107   return res.json(json)
108 }
109
110 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
111   const about: About = {
112     instance: {
113       name: CONFIG.INSTANCE.NAME,
114       shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
115       description: CONFIG.INSTANCE.DESCRIPTION,
116       terms: CONFIG.INSTANCE.TERMS
117     }
118   }
119
120   return res.json(about).end()
121 }
122
123 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
124   const data = customConfig()
125
126   return res.json(data).end()
127 }
128
129 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
130   await unlinkPromise(CONFIG.CUSTOM_FILE)
131
132   auditLogger.delete(
133     res.locals.oauth.token.User.Account.Actor.getIdentifier(),
134     new CustomConfigAuditView(customConfig())
135   )
136
137   reloadConfig()
138   ClientHtml.invalidCache()
139
140   const data = customConfig()
141
142   return res.json(data).end()
143 }
144
145 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
146   const toUpdate: CustomConfig = req.body
147   const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
148
149   // Force number conversion
150   toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
151   toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
152   toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
153   toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
154   toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
155
156   // camelCase to snake_case key
157   const toUpdateJSON = omit(toUpdate, 'user.videoQuota', 'instance.defaultClientRoute', 'instance.shortDescription', 'cache.videoCaptions')
158   toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
159   toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
160   toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
161   toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
162
163   await writeFilePromise(CONFIG.CUSTOM_FILE, JSON.stringify(toUpdateJSON, undefined, 2))
164
165   reloadConfig()
166   ClientHtml.invalidCache()
167
168   const data = customConfig()
169
170   auditLogger.update(
171     res.locals.oauth.token.User.Account.Actor.getIdentifier(),
172     new CustomConfigAuditView(data),
173     oldCustomConfigAuditKeys
174   )
175
176   return res.json(data).end()
177 }
178
179 // ---------------------------------------------------------------------------
180
181 export {
182   configRouter
183 }
184
185 // ---------------------------------------------------------------------------
186
187 function customConfig (): CustomConfig {
188   return {
189     instance: {
190       name: CONFIG.INSTANCE.NAME,
191       shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
192       description: CONFIG.INSTANCE.DESCRIPTION,
193       terms: CONFIG.INSTANCE.TERMS,
194       defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
195       defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
196       customizations: {
197         css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
198         javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
199       }
200     },
201     services: {
202       twitter: {
203         username: CONFIG.SERVICES.TWITTER.USERNAME,
204         whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
205       }
206     },
207     cache: {
208       previews: {
209         size: CONFIG.CACHE.PREVIEWS.SIZE
210       },
211       captions: {
212         size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
213       }
214     },
215     signup: {
216       enabled: CONFIG.SIGNUP.ENABLED,
217       limit: CONFIG.SIGNUP.LIMIT
218     },
219     admin: {
220       email: CONFIG.ADMIN.EMAIL
221     },
222     user: {
223       videoQuota: CONFIG.USER.VIDEO_QUOTA
224     },
225     transcoding: {
226       enabled: CONFIG.TRANSCODING.ENABLED,
227       threads: CONFIG.TRANSCODING.THREADS,
228       resolutions: {
229         '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
230         '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
231         '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
232         '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
233         '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
234       }
235     },
236     import: {
237       videos: {
238         http: {
239           enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
240         }
241       }
242     }
243   }
244 }