1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param, ValidationChain } from 'express-validator/check'
4 import { UserRight, VideoPrivacy } from '../../../shared'
13 } from '../../helpers/custom-validators/misc'
15 checkUserCanManageVideo,
16 isScheduleVideoUpdatePrivacyValid,
17 isVideoAbuseReasonValid,
19 isVideoChannelOfAccountExist,
20 isVideoDescriptionValid,
28 isVideoRatingTypeValid,
31 } from '../../helpers/custom-validators/videos'
32 import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
33 import { logger } from '../../helpers/logger'
34 import { CONSTRAINTS_FIELDS } from '../../initializers'
35 import { VideoShareModel } from '../../models/video/video-share'
36 import { authenticate } from '../oauth'
37 import { areValidationErrors } from './utils'
39 const videosAddValidator = getCommonVideoAttributes().concat([
41 .custom((value, { req }) => isVideoFile(req.files)).withMessage(
42 'This file is not supported or too large. Please, make sure it is of the following type: '
43 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
45 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
48 .custom(isIdValid).withMessage('Should have correct video channel id'),
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
53 if (areValidationErrors(req, res)) return
54 if (areErrorsInScheduleUpdate(req, res)) return
56 const videoFile: Express.Multer.File = req.files['videofile'][0]
57 const user = res.locals.oauth.token.User
59 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return
61 const isAble = await user.isAbleToUploadVideo(videoFile)
62 if (isAble === false) {
64 .json({ error: 'The user video quota is exceeded with this video.' })
73 duration = await getDurationFromVideoFile(videoFile.path)
75 logger.error('Invalid input file in videosAddValidator.', { err })
77 .json({ error: 'Invalid input file.' })
83 videoFile['duration'] = duration
89 const videosUpdateValidator = getCommonVideoAttributes().concat([
90 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
93 .custom(isVideoNameValid).withMessage('Should have a valid name'),
97 .custom(isIdValid).withMessage('Should have correct video channel id'),
99 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
100 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
102 if (areValidationErrors(req, res)) return
103 if (areErrorsInScheduleUpdate(req, res)) return
104 if (!await isVideoExist(req.params.id, res)) return
106 const video = res.locals.video
108 // Check if the user who did the request is able to update the video
109 const user = res.locals.oauth.token.User
110 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return
112 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
113 return res.status(409)
114 .json({ error: 'Cannot set "private" a video that was not private.' })
118 if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return
124 const videosGetValidator = [
125 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
127 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
128 logger.debug('Checking videosGet parameters', { parameters: req.params })
130 if (areValidationErrors(req, res)) return
131 if (!await isVideoExist(req.params.id, res)) return
133 const video = res.locals.video
135 // Video is public, anyone can access it
136 if (video.privacy === VideoPrivacy.PUBLIC) return next()
138 // Video is unlisted, check we used the uuid to fetch it
139 if (video.privacy === VideoPrivacy.UNLISTED) {
140 if (isUUIDValid(req.params.id)) return next()
142 // Don't leak this unlisted video
143 return res.status(404).end()
146 // Video is private, check the user
147 authenticate(req, res, () => {
148 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
149 return res.status(403)
150 .json({ error: 'Cannot get this private video of another user' })
159 const videosRemoveValidator = [
160 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
162 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
163 logger.debug('Checking videosRemove parameters', { parameters: req.params })
165 if (areValidationErrors(req, res)) return
166 if (!await isVideoExist(req.params.id, res)) return
168 // Check if the user who did the request is able to delete the video
169 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.video, UserRight.REMOVE_ANY_VIDEO, res)) return
175 const videoAbuseReportValidator = [
176 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
177 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
179 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
180 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
182 if (areValidationErrors(req, res)) return
183 if (!await isVideoExist(req.params.id, res)) return
189 const videoRateValidator = [
190 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
191 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
193 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
194 logger.debug('Checking videoRate parameters', { parameters: req.body })
196 if (areValidationErrors(req, res)) return
197 if (!await isVideoExist(req.params.id, res)) return
203 const videosShareValidator = [
204 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
205 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
207 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
208 logger.debug('Checking videoShare parameters', { parameters: req.params })
210 if (areValidationErrors(req, res)) return
211 if (!await isVideoExist(req.params.id, res)) return
213 const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
215 return res.status(404)
219 res.locals.videoShare = share
224 // ---------------------------------------------------------------------------
228 videosUpdateValidator,
230 videosRemoveValidator,
231 videosShareValidator,
233 videoAbuseReportValidator,
238 // ---------------------------------------------------------------------------
240 function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
241 if (req.body.scheduleUpdate) {
242 if (!req.body.scheduleUpdate.updateAt) {
244 .json({ error: 'Schedule update at is mandatory.' })
254 function getCommonVideoAttributes () {
256 body('thumbnailfile')
257 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
258 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: '
259 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
262 .custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
263 'This preview file is not supported or too large. Please, make sure it is of the following type: '
264 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
269 .customSanitizer(toIntOrNull)
270 .custom(isVideoCategoryValid).withMessage('Should have a valid category'),
273 .customSanitizer(toIntOrNull)
274 .custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
277 .customSanitizer(toValueOrNull)
278 .custom(isVideoLanguageValid).withMessage('Should have a valid language'),
282 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
283 body('waitTranscoding')
286 .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'),
290 .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
293 .customSanitizer(toValueOrNull)
294 .custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
297 .customSanitizer(toValueOrNull)
298 .custom(isVideoSupportValid).withMessage('Should have a valid support text'),
301 .customSanitizer(toValueOrNull)
302 .custom(isVideoTagsValid).withMessage('Should have correct tags'),
303 body('commentsEnabled')
306 .custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
308 body('scheduleUpdate')
310 .customSanitizer(toValueOrNull),
311 body('scheduleUpdate.updateAt')
313 .custom(isDateValid).withMessage('Should have a valid schedule update date'),
314 body('scheduleUpdate.privacy')
317 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
318 ] as (ValidationChain | express.Handler)[]