]>
Commit | Line | Data |
---|---|---|
40e87e9e | 1 | import * as express from 'express' |
6e46de09 | 2 | import { areValidationErrors } from '../utils' |
0f6acda1 | 3 | import { checkUserCanManageVideo, doesVideoExist } from '../../../helpers/custom-validators/videos' |
6e46de09 | 4 | import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' |
40e87e9e | 5 | import { body, param } from 'express-validator/check' |
74dc3bca | 6 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
6e46de09 C |
7 | import { UserRight } from '../../../../shared' |
8 | import { logger } from '../../../helpers/logger' | |
0f6acda1 | 9 | import { doesVideoCaptionExist, isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions' |
6e46de09 | 10 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
40e87e9e C |
11 | |
12 | const addVideoCaptionValidator = [ | |
13 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), | |
14 | param('captionLanguage').custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'), | |
15 | body('captionfile') | |
16 | .custom((value, { req }) => isVideoCaptionFile(req.files, 'captionfile')).withMessage( | |
17 | 'This caption file is not supported or too large. Please, make sure it is of the following type : ' | |
18 | + CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME.join(', ') | |
19 | ), | |
20 | ||
21 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
22 | logger.debug('Checking addVideoCaption parameters', { parameters: req.body }) | |
23 | ||
cf7a61b5 | 24 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) |
0f6acda1 | 25 | if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req) |
40e87e9e C |
26 | |
27 | // Check if the user who did the request is able to update the video | |
28 | const user = res.locals.oauth.token.User | |
cf7a61b5 | 29 | if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req) |
40e87e9e C |
30 | |
31 | return next() | |
32 | } | |
33 | ] | |
34 | ||
35 | const deleteVideoCaptionValidator = [ | |
36 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), | |
37 | param('captionLanguage').custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'), | |
38 | ||
39 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
40 | logger.debug('Checking deleteVideoCaption parameters', { parameters: req.params }) | |
41 | ||
42 | if (areValidationErrors(req, res)) return | |
0f6acda1 C |
43 | if (!await doesVideoExist(req.params.videoId, res)) return |
44 | if (!await doesVideoCaptionExist(res.locals.video, req.params.captionLanguage, res)) return | |
40e87e9e C |
45 | |
46 | // Check if the user who did the request is able to update the video | |
47 | const user = res.locals.oauth.token.User | |
48 | if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return | |
49 | ||
50 | return next() | |
51 | } | |
52 | ] | |
53 | ||
54 | const listVideoCaptionsValidator = [ | |
55 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), | |
56 | ||
57 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
58 | logger.debug('Checking listVideoCaptions parameters', { parameters: req.params }) | |
59 | ||
60 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 61 | if (!await doesVideoExist(req.params.videoId, res, 'id')) return |
40e87e9e C |
62 | |
63 | return next() | |
64 | } | |
65 | ] | |
66 | ||
67 | export { | |
68 | addVideoCaptionValidator, | |
69 | listVideoCaptionsValidator, | |
70 | deleteVideoCaptionValidator | |
71 | } |