aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/video-captions.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-07-12 19:02:00 +0200
committerChocobozzz <me@florianbigard.com>2018-07-16 11:50:08 +0200
commit40e87e9ecc54e3513fb586928330a7855eb192c6 (patch)
treeaf1111ecba85f9cd8286811ff332a67cf21be2f6 /server/middlewares/validators/video-captions.ts
parentd4557fd3ecc8d4ed4fb0e5c868929bc36c959ed2 (diff)
downloadPeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.gz
PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.tar.zst
PeerTube-40e87e9ecc54e3513fb586928330a7855eb192c6.zip
Implement captions/subtitles
Diffstat (limited to 'server/middlewares/validators/video-captions.ts')
-rw-r--r--server/middlewares/validators/video-captions.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/server/middlewares/validators/video-captions.ts b/server/middlewares/validators/video-captions.ts
new file mode 100644
index 000000000..b6d92d380
--- /dev/null
+++ b/server/middlewares/validators/video-captions.ts
@@ -0,0 +1,70 @@
1import * as express from 'express'
2import { areValidationErrors } from './utils'
3import { checkUserCanManageVideo, isVideoExist } from '../../helpers/custom-validators/videos'
4import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
5import { body, param } from 'express-validator/check'
6import { CONSTRAINTS_FIELDS } from '../../initializers'
7import { UserRight } from '../../../shared'
8import { logger } from '../../helpers/logger'
9import { isVideoCaptionExist, isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
10
11const addVideoCaptionValidator = [
12 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
13 param('captionLanguage').custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
14 body('captionfile')
15 .custom((value, { req }) => isVideoCaptionFile(req.files, 'captionfile')).withMessage(
16 'This caption file is not supported or too large. Please, make sure it is of the following type : '
17 + CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME.join(', ')
18 ),
19
20 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21 logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
22
23 if (areValidationErrors(req, res)) return
24 if (!await isVideoExist(req.params.videoId, res)) return
25
26 // Check if the user who did the request is able to update the video
27 const user = res.locals.oauth.token.User
28 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return
29
30 return next()
31 }
32]
33
34const deleteVideoCaptionValidator = [
35 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
36 param('captionLanguage').custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
37
38 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
39 logger.debug('Checking deleteVideoCaption parameters', { parameters: req.params })
40
41 if (areValidationErrors(req, res)) return
42 if (!await isVideoExist(req.params.videoId, res)) return
43 if (!await isVideoCaptionExist(res.locals.video, req.params.captionLanguage, res)) return
44
45 // Check if the user who did the request is able to update the video
46 const user = res.locals.oauth.token.User
47 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return
48
49 return next()
50 }
51]
52
53const listVideoCaptionsValidator = [
54 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
55
56 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
57 logger.debug('Checking listVideoCaptions parameters', { parameters: req.params })
58
59 if (areValidationErrors(req, res)) return
60 if (!await isVideoExist(req.params.videoId, res)) return
61
62 return next()
63 }
64]
65
66export {
67 addVideoCaptionValidator,
68 listVideoCaptionsValidator,
69 deleteVideoCaptionValidator
70}