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