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'
38 import { cleanUpReqFiles } from '../../helpers/utils'
40 const videosAddValidator = getCommonVideoAttributes().concat([
42 .custom((value, { req }) => isVideoFile(req.files)).withMessage(
43 'This file is not supported or too large. Please, make sure it is of the following type: '
44 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
46 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
49 .custom(isIdValid).withMessage('Should have correct video channel id'),
51 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
52 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
54 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
55 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
57 const videoFile: Express.Multer.File = req.files['videofile'][0]
58 const user = res.locals.oauth.token.User
60 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
62 const isAble = await user.isAbleToUploadVideo(videoFile)
63 if (isAble === false) {
65 .json({ error: 'The user video quota is exceeded with this video.' })
68 return cleanUpReqFiles(req)
74 duration = await getDurationFromVideoFile(videoFile.path)
76 logger.error('Invalid input file in videosAddValidator.', { err })
78 .json({ error: 'Invalid input file.' })
81 return cleanUpReqFiles(req)
84 videoFile['duration'] = duration
90 const videosUpdateValidator = getCommonVideoAttributes().concat([
91 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
94 .custom(isVideoNameValid).withMessage('Should have a valid name'),
98 .custom(isIdValid).withMessage('Should have correct video channel id'),
100 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
101 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
103 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
104 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
105 if (!await isVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
107 const video = res.locals.video
109 // Check if the user who did the request is able to update the video
110 const user = res.locals.oauth.token.User
111 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
113 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
115 return res.status(409)
116 .json({ error: 'Cannot set "private" a video that was not private.' })
120 if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
126 const videosGetValidator = [
127 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
129 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
130 logger.debug('Checking videosGet parameters', { parameters: req.params })
132 if (areValidationErrors(req, res)) return
133 if (!await isVideoExist(req.params.id, res)) return
135 const video = res.locals.video
137 // Video is public, anyone can access it
138 if (video.privacy === VideoPrivacy.PUBLIC) return next()
140 // Video is unlisted, check we used the uuid to fetch it
141 if (video.privacy === VideoPrivacy.UNLISTED) {
142 if (isUUIDValid(req.params.id)) return next()
144 // Don't leak this unlisted video
145 return res.status(404).end()
148 // Video is private, check the user
149 authenticate(req, res, () => {
150 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
151 return res.status(403)
152 .json({ error: 'Cannot get this private video of another user' })
161 const videosRemoveValidator = [
162 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
164 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
165 logger.debug('Checking videosRemove parameters', { parameters: req.params })
167 if (areValidationErrors(req, res)) return
168 if (!await isVideoExist(req.params.id, res)) return
170 // Check if the user who did the request is able to delete the video
171 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.video, UserRight.REMOVE_ANY_VIDEO, res)) return
177 const videoAbuseReportValidator = [
178 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
179 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
181 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
182 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
184 if (areValidationErrors(req, res)) return
185 if (!await isVideoExist(req.params.id, res)) return
191 const videoRateValidator = [
192 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
193 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
195 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
196 logger.debug('Checking videoRate parameters', { parameters: req.body })
198 if (areValidationErrors(req, res)) return
199 if (!await isVideoExist(req.params.id, res)) return
205 const videosShareValidator = [
206 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
207 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
209 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
210 logger.debug('Checking videoShare parameters', { parameters: req.params })
212 if (areValidationErrors(req, res)) return
213 if (!await isVideoExist(req.params.id, res)) return
215 const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
217 return res.status(404)
221 res.locals.videoShare = share
226 function getCommonVideoAttributes () {
228 body('thumbnailfile')
229 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
230 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: '
231 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
234 .custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
235 'This preview file is not supported or too large. Please, make sure it is of the following type: '
236 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
241 .customSanitizer(toIntOrNull)
242 .custom(isVideoCategoryValid).withMessage('Should have a valid category'),
245 .customSanitizer(toIntOrNull)
246 .custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
249 .customSanitizer(toValueOrNull)
250 .custom(isVideoLanguageValid).withMessage('Should have a valid language'),
254 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
255 body('waitTranscoding')
258 .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'),
262 .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
265 .customSanitizer(toValueOrNull)
266 .custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
269 .customSanitizer(toValueOrNull)
270 .custom(isVideoSupportValid).withMessage('Should have a valid support text'),
273 .customSanitizer(toValueOrNull)
274 .custom(isVideoTagsValid).withMessage('Should have correct tags'),
275 body('commentsEnabled')
278 .custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
280 body('scheduleUpdate')
282 .customSanitizer(toValueOrNull),
283 body('scheduleUpdate.updateAt')
285 .custom(isDateValid).withMessage('Should have a valid schedule update date'),
286 body('scheduleUpdate.privacy')
289 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
290 ] as (ValidationChain | express.Handler)[]
293 // ---------------------------------------------------------------------------
297 videosUpdateValidator,
299 videosRemoveValidator,
300 videosShareValidator,
302 videoAbuseReportValidator,
306 getCommonVideoAttributes
309 // ---------------------------------------------------------------------------
311 function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
312 if (req.body.scheduleUpdate) {
313 if (!req.body.scheduleUpdate.updateAt) {
315 .json({ error: 'Schedule update at is mandatory.' })