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'
10 const addVideoCaptionValidator = [
11 isValidVideoIdParam('videoId'),
13 param('captionLanguage')
14 .custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
17 .custom((_, { req }) => isVideoCaptionFile(req.files, 'captionfile'))
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(', ')
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
28 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
29 if (!await doesVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
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)
39 const deleteVideoCaptionValidator = [
40 isValidVideoIdParam('videoId'),
42 param('captionLanguage')
43 .custom(isVideoCaptionLanguageValid).not().isEmpty().withMessage('Should have a valid caption language'),
45 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
46 logger.debug('Checking deleteVideoCaption parameters', { parameters: req.params })
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
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
60 const listVideoCaptionsValidator = [
61 isValidVideoIdParam('videoId'),
63 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
64 logger.debug('Checking listVideoCaptions parameters', { parameters: req.params })
66 if (areValidationErrors(req, res)) return
67 if (!await doesVideoExist(req.params.videoId, res, 'id')) return
74 addVideoCaptionValidator,
75 listVideoCaptionsValidator,
76 deleteVideoCaptionValidator