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