Support additional video extensions
[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 { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
7 import { CONFIG, CONSTRAINTS_FIELDS, reloadConfig } from '../../initializers'
8 import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../middlewares'
9 import { customConfigUpdateValidator } from '../../middlewares/validators/config'
10 import { ClientHtml } from '../../lib/client-html'
11 import { auditLoggerFactory, CustomConfigAuditView, getAuditIdFromRes } from '../../helpers/audit-logger'
12 import { remove, writeJSON } from 'fs-extra'
13 import { getServerCommit } from '../../helpers/utils'
14 import { Emailer } from '../../lib/emailer'
15
16 const packageJSON = require('../../../../package.json')
17 const configRouter = express.Router()
18
19 const auditLogger = auditLoggerFactory('config')
20
21 configRouter.get('/about', getAbout)
22 configRouter.get('/',
23   asyncMiddleware(getConfig)
24 )
25
26 configRouter.get('/custom',
27   authenticate,
28   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
29   asyncMiddleware(getCustomConfig)
30 )
31 configRouter.put('/custom',
32   authenticate,
33   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
34   asyncMiddleware(customConfigUpdateValidator),
35   asyncMiddleware(updateCustomConfig)
36 )
37 configRouter.delete('/custom',
38   authenticate,
39   ensureUserHasRight(UserRight.MANAGE_CONFIGURATION),
40   asyncMiddleware(deleteCustomConfig)
41 )
42
43 let serverCommit: string
44 async function getConfig (req: express.Request, res: express.Response) {
45   const allowed = await isSignupAllowed()
46   const allowedForCurrentIP = isSignupAllowedForCurrentIP(req.ip)
47
48   if (serverCommit === undefined) serverCommit = await getServerCommit()
49
50   const enabledResolutions = Object.keys(CONFIG.TRANSCODING.RESOLUTIONS)
51    .filter(key => CONFIG.TRANSCODING.ENABLED === CONFIG.TRANSCODING.RESOLUTIONS[key] === true)
52    .map(r => parseInt(r, 10))
53
54   const json: ServerConfig = {
55     instance: {
56       name: CONFIG.INSTANCE.NAME,
57       shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
58       defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
59       defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
60       customizations: {
61         javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT,
62         css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS
63       }
64     },
65     email: {
66       enabled: Emailer.Instance.isEnabled()
67     },
68     serverVersion: packageJSON.version,
69     serverCommit,
70     signup: {
71       allowed,
72       allowedForCurrentIP,
73       requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
74     },
75     transcoding: {
76       enabledResolutions
77     },
78     import: {
79       videos: {
80         http: {
81           enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
82         },
83         torrent: {
84           enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
85         }
86       }
87     },
88     avatar: {
89       file: {
90         size: {
91           max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
92         },
93         extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
94       }
95     },
96     video: {
97       image: {
98         extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
99         size: {
100           max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
101         }
102       },
103       file: {
104         extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
105       }
106     },
107     videoCaption: {
108       file: {
109         size: {
110           max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
111         },
112         extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
113       }
114     },
115     user: {
116       videoQuota: CONFIG.USER.VIDEO_QUOTA,
117       videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
118     }
119   }
120
121   return res.json(json)
122 }
123
124 function getAbout (req: express.Request, res: express.Response, next: express.NextFunction) {
125   const about: About = {
126     instance: {
127       name: CONFIG.INSTANCE.NAME,
128       shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
129       description: CONFIG.INSTANCE.DESCRIPTION,
130       terms: CONFIG.INSTANCE.TERMS
131     }
132   }
133
134   return res.json(about).end()
135 }
136
137 async function getCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
138   const data = customConfig()
139
140   return res.json(data).end()
141 }
142
143 async function deleteCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
144   await remove(CONFIG.CUSTOM_FILE)
145
146   auditLogger.delete(getAuditIdFromRes(res), new CustomConfigAuditView(customConfig()))
147
148   reloadConfig()
149   ClientHtml.invalidCache()
150
151   const data = customConfig()
152
153   return res.json(data).end()
154 }
155
156 async function updateCustomConfig (req: express.Request, res: express.Response, next: express.NextFunction) {
157   const toUpdate: CustomConfig = req.body
158   const oldCustomConfigAuditKeys = new CustomConfigAuditView(customConfig())
159
160   // Force number conversion
161   toUpdate.cache.previews.size = parseInt('' + toUpdate.cache.previews.size, 10)
162   toUpdate.cache.captions.size = parseInt('' + toUpdate.cache.captions.size, 10)
163   toUpdate.signup.limit = parseInt('' + toUpdate.signup.limit, 10)
164   toUpdate.user.videoQuota = parseInt('' + toUpdate.user.videoQuota, 10)
165   toUpdate.user.videoQuotaDaily = parseInt('' + toUpdate.user.videoQuotaDaily, 10)
166   toUpdate.transcoding.threads = parseInt('' + toUpdate.transcoding.threads, 10)
167
168   // camelCase to snake_case key
169   const toUpdateJSON = omit(
170     toUpdate,
171     'user.videoQuota',
172     'instance.defaultClientRoute',
173     'instance.shortDescription',
174     'cache.videoCaptions',
175     'signup.requiresEmailVerification',
176     'transcoding.allowAdditionalExtensions'
177   )
178   toUpdateJSON.user['video_quota'] = toUpdate.user.videoQuota
179   toUpdateJSON.user['video_quota_daily'] = toUpdate.user.videoQuotaDaily
180   toUpdateJSON.instance['default_client_route'] = toUpdate.instance.defaultClientRoute
181   toUpdateJSON.instance['short_description'] = toUpdate.instance.shortDescription
182   toUpdateJSON.instance['default_nsfw_policy'] = toUpdate.instance.defaultNSFWPolicy
183   toUpdateJSON.signup['requires_email_verification'] = toUpdate.signup.requiresEmailVerification
184   toUpdateJSON.transcoding['allow_additional_extensions'] = toUpdate.transcoding.allowAdditionalExtensions
185
186   await writeJSON(CONFIG.CUSTOM_FILE, toUpdateJSON, { spaces: 2 })
187
188   reloadConfig()
189   ClientHtml.invalidCache()
190
191   const data = customConfig()
192
193   auditLogger.update(
194     getAuditIdFromRes(res),
195     new CustomConfigAuditView(data),
196     oldCustomConfigAuditKeys
197   )
198
199   return res.json(data).end()
200 }
201
202 // ---------------------------------------------------------------------------
203
204 export {
205   configRouter
206 }
207
208 // ---------------------------------------------------------------------------
209
210 function customConfig (): CustomConfig {
211   return {
212     instance: {
213       name: CONFIG.INSTANCE.NAME,
214       shortDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
215       description: CONFIG.INSTANCE.DESCRIPTION,
216       terms: CONFIG.INSTANCE.TERMS,
217       defaultClientRoute: CONFIG.INSTANCE.DEFAULT_CLIENT_ROUTE,
218       defaultNSFWPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
219       customizations: {
220         css: CONFIG.INSTANCE.CUSTOMIZATIONS.CSS,
221         javascript: CONFIG.INSTANCE.CUSTOMIZATIONS.JAVASCRIPT
222       }
223     },
224     services: {
225       twitter: {
226         username: CONFIG.SERVICES.TWITTER.USERNAME,
227         whitelisted: CONFIG.SERVICES.TWITTER.WHITELISTED
228       }
229     },
230     cache: {
231       previews: {
232         size: CONFIG.CACHE.PREVIEWS.SIZE
233       },
234       captions: {
235         size: CONFIG.CACHE.VIDEO_CAPTIONS.SIZE
236       }
237     },
238     signup: {
239       enabled: CONFIG.SIGNUP.ENABLED,
240       limit: CONFIG.SIGNUP.LIMIT,
241       requiresEmailVerification: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION
242     },
243     admin: {
244       email: CONFIG.ADMIN.EMAIL
245     },
246     user: {
247       videoQuota: CONFIG.USER.VIDEO_QUOTA,
248       videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
249     },
250     transcoding: {
251       enabled: CONFIG.TRANSCODING.ENABLED,
252       allowAdditionalExtensions: CONFIG.TRANSCODING.ALLOW_ADDITIONAL_EXTENSIONS,
253       threads: CONFIG.TRANSCODING.THREADS,
254       resolutions: {
255         '240p': CONFIG.TRANSCODING.RESOLUTIONS[ '240p' ],
256         '360p': CONFIG.TRANSCODING.RESOLUTIONS[ '360p' ],
257         '480p': CONFIG.TRANSCODING.RESOLUTIONS[ '480p' ],
258         '720p': CONFIG.TRANSCODING.RESOLUTIONS[ '720p' ],
259         '1080p': CONFIG.TRANSCODING.RESOLUTIONS[ '1080p' ]
260       }
261     },
262     import: {
263       videos: {
264         http: {
265           enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
266         },
267         torrent: {
268           enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
269         }
270       }
271     }
272   }
273 }