1 import * as express from 'express'
2 import 'express-validator'
3 import { body, param, query } from 'express-validator/check'
4 import { UserRight, VideoPrivacy } from '../../../shared'
5 import { isBooleanValid, isIdOrUUIDValid, isIdValid, isUUIDValid, toIntOrNull, toStringOrNull } from '../../helpers/custom-validators/misc'
7 isVideoAbuseReasonValid,
9 isVideoDescriptionValid,
17 isVideoRatingTypeValid,
20 } from '../../helpers/custom-validators/videos'
21 import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
22 import { logger } from '../../helpers/logger'
23 import { CONSTRAINTS_FIELDS } from '../../initializers'
24 import { UserModel } from '../../models/account/user'
25 import { VideoModel } from '../../models/video/video'
26 import { VideoChannelModel } from '../../models/video/video-channel'
27 import { VideoShareModel } from '../../models/video/video-share'
28 import { authenticate } from '../oauth'
29 import { areValidationErrors } from './utils'
31 const videosAddValidator = [
32 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
33 'This file is not supported. Please, make sure it is of the following type : '
34 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
36 body('thumbnailfile').custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
37 'This thumbnail file is not supported. Please, make sure it is of the following type : '
38 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
40 body('previewfile').custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
41 'This preview file is not supported. Please, make sure it is of the following type : '
42 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
44 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
47 .customSanitizer(toIntOrNull)
48 .custom(isVideoCategoryValid).withMessage('Should have a valid category'),
51 .customSanitizer(toIntOrNull)
52 .custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
55 .customSanitizer(toStringOrNull)
56 .custom(isVideoLanguageValid).withMessage('Should have a valid language'),
59 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
62 .customSanitizer(toStringOrNull)
63 .custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
66 .customSanitizer(toStringOrNull)
67 .custom(isVideoSupportValid).withMessage('Should have a valid support text'),
70 .custom(isVideoTagsValid).withMessage('Should have correct tags'),
71 body('commentsEnabled')
73 .custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
77 .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
78 body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
80 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
81 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
83 if (areValidationErrors(req, res)) return
84 if (areErrorsInVideoImageFiles(req, res)) return
86 const videoFile: Express.Multer.File = req.files['videofile'][0]
87 const user = res.locals.oauth.token.User
89 const videoChannel = await VideoChannelModel.loadByIdAndAccount(req.body.channelId, user.Account.id)
92 .json({ error: 'Unknown video video channel for this account.' })
98 res.locals.videoChannel = videoChannel
100 const isAble = await user.isAbleToUploadVideo(videoFile)
101 if (isAble === false) {
103 .json({ error: 'The user video quota is exceeded with this video.' })
112 duration = await getDurationFromVideoFile(videoFile.path)
114 logger.error('Invalid input file in videosAddValidator.', { err })
116 .json({ error: 'Invalid input file.' })
122 videoFile['duration'] = duration
128 const videosUpdateValidator = [
129 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
130 body('thumbnailfile').custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
131 'This thumbnail file is not supported. Please, make sure it is of the following type : '
132 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
134 body('previewfile').custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
135 'This preview file is not supported. Please, make sure it is of the following type : '
136 + CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
140 .custom(isVideoNameValid).withMessage('Should have a valid name'),
143 .customSanitizer(toIntOrNull)
144 .custom(isVideoCategoryValid).withMessage('Should have a valid category'),
147 .customSanitizer(toIntOrNull)
148 .custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
151 .customSanitizer(toStringOrNull)
152 .custom(isVideoLanguageValid).withMessage('Should have a valid language'),
156 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
160 .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
163 .customSanitizer(toStringOrNull)
164 .custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
167 .customSanitizer(toStringOrNull)
168 .custom(isVideoSupportValid).withMessage('Should have a valid support text'),
171 .custom(isVideoTagsValid).withMessage('Should have correct tags'),
172 body('commentsEnabled')
175 .custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
177 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
178 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
180 if (areValidationErrors(req, res)) return
181 if (areErrorsInVideoImageFiles(req, res)) return
182 if (!await isVideoExist(req.params.id, res)) return
184 const video = res.locals.video
186 // Check if the user who did the request is able to update the video
187 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return
189 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
190 return res.status(409)
191 .json({ error: 'Cannot set "private" a video that was not private anymore.' })
199 const videosGetValidator = [
200 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
202 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
203 logger.debug('Checking videosGet parameters', { parameters: req.params })
205 if (areValidationErrors(req, res)) return
206 if (!await isVideoExist(req.params.id, res)) return
208 const video = res.locals.video
210 // Video is public, anyone can access it
211 if (video.privacy === VideoPrivacy.PUBLIC) return next()
213 // Video is unlisted, check we used the uuid to fetch it
214 if (video.privacy === VideoPrivacy.UNLISTED) {
215 if (isUUIDValid(req.params.id)) return next()
217 // Don't leak this unlisted video
218 return res.status(404).end()
221 // Video is private, check the user
222 authenticate(req, res, () => {
223 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
224 return res.status(403)
225 .json({ error: 'Cannot get this private video of another user' })
234 const videosRemoveValidator = [
235 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
237 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
238 logger.debug('Checking videosRemove parameters', { parameters: req.params })
240 if (areValidationErrors(req, res)) return
241 if (!await isVideoExist(req.params.id, res)) return
243 // Check if the user who did the request is able to delete the video
244 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.video, UserRight.REMOVE_ANY_VIDEO, res)) return
250 const videosSearchValidator = [
251 query('search').not().isEmpty().withMessage('Should have a valid search'),
253 (req: express.Request, res: express.Response, next: express.NextFunction) => {
254 logger.debug('Checking videosSearch parameters', { parameters: req.params })
256 if (areValidationErrors(req, res)) return
262 const videoAbuseReportValidator = [
263 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
264 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
266 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
267 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
269 if (areValidationErrors(req, res)) return
270 if (!await isVideoExist(req.params.id, res)) return
276 const videoRateValidator = [
277 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
278 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
280 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
281 logger.debug('Checking videoRate parameters', { parameters: req.body })
283 if (areValidationErrors(req, res)) return
284 if (!await isVideoExist(req.params.id, res)) return
290 const videosShareValidator = [
291 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
292 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
294 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
295 logger.debug('Checking videoShare parameters', { parameters: req.params })
297 if (areValidationErrors(req, res)) return
298 if (!await isVideoExist(req.params.id, res)) return
300 const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
302 return res.status(404)
306 res.locals.videoShare = share
311 // ---------------------------------------------------------------------------
315 videosUpdateValidator,
317 videosRemoveValidator,
318 videosSearchValidator,
319 videosShareValidator,
321 videoAbuseReportValidator,
326 // ---------------------------------------------------------------------------
328 function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: express.Response) {
329 // Retrieve the user who did the request
330 if (video.isOwned() === false) {
332 .json({ error: 'Cannot remove video of another server, blacklist it' })
337 // Check if the user can delete the video
338 // The user can delete it if he has the right
339 // Or if s/he is the video's account
340 const account = video.VideoChannel.Account
341 if (user.hasRight(right) === false && account.userId !== user.id) {
343 .json({ error: 'Cannot remove video of another user' })
351 function areErrorsInVideoImageFiles (req: express.Request, res: express.Response) {
352 // Files are optional
353 if (!req.files) return false
355 for (const imageField of [ 'thumbnail', 'preview' ]) {
356 if (!req.files[ imageField ]) continue
358 const imageFile = req.files[ imageField ][ 0 ] as Express.Multer.File
359 if (imageFile.size > CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max) {
361 .send({ error: `The size of the ${imageField} is too big (>${CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max}).` })