]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import 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. ' + | |
586a7478 C |
20 | `Please, make sure it is under ${CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max} bytes ` + |
21 | 'and one of the following mimetypes: ' + | |
a1587156 C |
22 | Object.keys(MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT).map(key => `${key} (${MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT[key]})`).join(', ') |
23 | ), | |
40e87e9e C |
24 | |
25 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
26 | logger.debug('Checking addVideoCaption parameters', { parameters: req.body }) | |
27 | ||
cf7a61b5 | 28 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) |
0f6acda1 | 29 | if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req) |
40e87e9e C |
30 | |
31 | // Check if the user who did the request is able to update the video | |
32 | const user = res.locals.oauth.token.User | |
453e83ea | 33 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req) |
40e87e9e C |
34 | |
35 | return next() | |
36 | } | |
37 | ] | |
38 | ||
39 | const deleteVideoCaptionValidator = [ | |
d4a8e7a6 C |
40 | isValidVideoIdParam('videoId'), |
41 | ||
42 | param('captionLanguage') | |
43 | .custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'), | |
40e87e9e C |
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 | |
0f6acda1 | 49 | if (!await doesVideoExist(req.params.videoId, res)) return |
453e83ea | 50 | if (!await doesVideoCaptionExist(res.locals.videoAll, req.params.captionLanguage, res)) return |
40e87e9e C |
51 | |
52 | // Check if the user who did the request is able to update the video | |
53 | const user = res.locals.oauth.token.User | |
453e83ea | 54 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return |
40e87e9e C |
55 | |
56 | return next() | |
57 | } | |
58 | ] | |
59 | ||
60 | const listVideoCaptionsValidator = [ | |
d4a8e7a6 | 61 | isValidVideoIdParam('videoId'), |
40e87e9e C |
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 | |
0f6acda1 | 67 | if (!await doesVideoExist(req.params.videoId, res, 'id')) return |
40e87e9e C |
68 | |
69 | return next() | |
70 | } | |
71 | ] | |
72 | ||
73 | export { | |
74 | addVideoCaptionValidator, | |
75 | listVideoCaptionsValidator, | |
76 | deleteVideoCaptionValidator | |
77 | } |