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