aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-02 15:34:09 +0200
committerChocobozzz <me@florianbigard.com>2018-08-06 11:19:16 +0200
commitfbad87b0472f574409f7aa3ae7f8b54927d0cdd6 (patch)
tree197b4209e75d57dabae7cdd6f2da5f765e427023 /server/middlewares/validators
parent5e319fb7898fd0482c399cc3ae9dcfc20d274a58 (diff)
downloadPeerTube-fbad87b0472f574409f7aa3ae7f8b54927d0cdd6.tar.gz
PeerTube-fbad87b0472f574409f7aa3ae7f8b54927d0cdd6.tar.zst
PeerTube-fbad87b0472f574409f7aa3ae7f8b54927d0cdd6.zip
Add ability to import video with youtube-dl
Diffstat (limited to 'server/middlewares/validators')
-rw-r--r--server/middlewares/validators/index.ts1
-rw-r--r--server/middlewares/validators/video-imports.ts51
-rw-r--r--server/middlewares/validators/videos.ts62
3 files changed, 84 insertions, 30 deletions
diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts
index e3f0f5963..c5400c8f5 100644
--- a/server/middlewares/validators/index.ts
+++ b/server/middlewares/validators/index.ts
@@ -11,3 +11,4 @@ export * from './video-blacklist'
11export * from './video-channels' 11export * from './video-channels'
12export * from './webfinger' 12export * from './webfinger'
13export * from './search' 13export * from './search'
14export * from './video-imports'
diff --git a/server/middlewares/validators/video-imports.ts b/server/middlewares/validators/video-imports.ts
new file mode 100644
index 000000000..0ba759ff0
--- /dev/null
+++ b/server/middlewares/validators/video-imports.ts
@@ -0,0 +1,51 @@
1import * as express from 'express'
2import { body, param } from 'express-validator/check'
3import { isIdValid } from '../../helpers/custom-validators/misc'
4import { logger } from '../../helpers/logger'
5import { areValidationErrors } from './utils'
6import { getCommonVideoAttributes } from './videos'
7import { isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
8import { cleanUpReqFiles } from '../../helpers/utils'
9import { isVideoChannelOfAccountExist, isVideoNameValid } from '../../helpers/custom-validators/videos'
10
11const videoImportAddValidator = getCommonVideoAttributes().concat([
12 body('targetUrl').custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
13 body('channelId')
14 .toInt()
15 .custom(isIdValid).withMessage('Should have correct video channel id'),
16 body('name')
17 .optional()
18 .custom(isVideoNameValid).withMessage('Should have a valid name'),
19
20 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
22
23 const user = res.locals.oauth.token.User
24
25 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
26 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
27
28 return next()
29 }
30])
31
32const videoImportDeleteValidator = [
33 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
34
35 (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking videoImportDeleteValidator parameters', { parameters: req.body })
37
38 if (areValidationErrors(req, res)) return
39
40 return next()
41 }
42]
43
44// ---------------------------------------------------------------------------
45
46export {
47 videoImportAddValidator,
48 videoImportDeleteValidator
49}
50
51// ---------------------------------------------------------------------------
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts
index 9357c1e39..c812d4677 100644
--- a/server/middlewares/validators/videos.ts
+++ b/server/middlewares/validators/videos.ts
@@ -223,36 +223,6 @@ const videosShareValidator = [
223 } 223 }
224] 224]
225 225
226// ---------------------------------------------------------------------------
227
228export {
229 videosAddValidator,
230 videosUpdateValidator,
231 videosGetValidator,
232 videosRemoveValidator,
233 videosShareValidator,
234
235 videoAbuseReportValidator,
236
237 videoRateValidator
238}
239
240// ---------------------------------------------------------------------------
241
242function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
243 if (req.body.scheduleUpdate) {
244 if (!req.body.scheduleUpdate.updateAt) {
245 res.status(400)
246 .json({ error: 'Schedule update at is mandatory.' })
247 .end()
248
249 return true
250 }
251 }
252
253 return false
254}
255
256function getCommonVideoAttributes () { 226function getCommonVideoAttributes () {
257 return [ 227 return [
258 body('thumbnailfile') 228 body('thumbnailfile')
@@ -319,3 +289,35 @@ function getCommonVideoAttributes () {
319 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy') 289 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
320 ] as (ValidationChain | express.Handler)[] 290 ] as (ValidationChain | express.Handler)[]
321} 291}
292
293// ---------------------------------------------------------------------------
294
295export {
296 videosAddValidator,
297 videosUpdateValidator,
298 videosGetValidator,
299 videosRemoveValidator,
300 videosShareValidator,
301
302 videoAbuseReportValidator,
303
304 videoRateValidator,
305
306 getCommonVideoAttributes
307}
308
309// ---------------------------------------------------------------------------
310
311function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
312 if (req.body.scheduleUpdate) {
313 if (!req.body.scheduleUpdate.updateAt) {
314 res.status(400)
315 .json({ error: 'Schedule update at is mandatory.' })
316 .end()
317
318 return true
319 }
320 }
321
322 return false
323}