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 } from '../../helpers/custom-validators/misc'
7 isVideoAbuseReasonValid, isVideoCategoryValid, isVideoDescriptionValid, isVideoExist, isVideoFile, isVideoLanguageValid,
8 isVideoLicenceValid, isVideoNameValid, isVideoPrivacyValid, isVideoRatingTypeValid, isVideoTagsValid
9 } from '../../helpers/custom-validators/videos'
10 import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
11 import { logger } from '../../helpers/logger'
12 import { CONSTRAINTS_FIELDS } from '../../initializers'
13 import { UserModel } from '../../models/account/user'
14 import { VideoModel } from '../../models/video/video'
15 import { VideoChannelModel } from '../../models/video/video-channel'
16 import { VideoShareModel } from '../../models/video/video-share'
17 import { authenticate } from '../oauth'
18 import { areValidationErrors } from './utils'
20 const videosAddValidator = [
21 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
22 'This file is not supported. Please, make sure it is of the following type : '
23 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
25 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
26 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
27 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
28 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
29 body('nsfw').custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
30 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
31 body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
32 body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
33 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
34 body('commentsEnabled').custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
36 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
37 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
39 if (areValidationErrors(req, res)) return
41 const videoFile: Express.Multer.File = req.files['videofile'][0]
42 const user = res.locals.oauth.token.User
44 const videoChannel = await VideoChannelModel.loadByIdAndAccount(req.body.channelId, user.Account.id)
47 .json({ error: 'Unknown video video channel for this account.' })
53 res.locals.videoChannel = videoChannel
55 const isAble = await user.isAbleToUploadVideo(videoFile)
56 if (isAble === false) {
58 .json({ error: 'The user video quota is exceeded with this video.' })
67 duration = await getDurationFromVideoFile(videoFile.path)
69 logger.error('Invalid input file in videosAddValidator.', err)
71 .json({ error: 'Invalid input file.' })
77 videoFile['duration'] = duration
83 const videosUpdateValidator = [
84 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
85 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
86 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
87 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
88 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
89 body('nsfw').optional().custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
90 body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
91 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
92 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
93 body('commentsEnabled').optional().custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
95 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
96 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
98 if (areValidationErrors(req, res)) return
99 if (!await isVideoExist(req.params.id, res)) return
101 const video = res.locals.video
103 // We need to make additional checks
104 if (video.isOwned() === false) {
105 return res.status(403)
106 .json({ error: 'Cannot update video of another server' })
110 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
111 return res.status(403)
112 .json({ error: 'Cannot update video of another user' })
116 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
117 return res.status(409)
118 .json({ error: 'Cannot set "private" a video that was not private anymore.' })
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 not private, anyone can access it
138 if (video.privacy !== VideoPrivacy.PRIVATE) return next()
140 authenticate(req, res, () => {
141 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
142 return res.status(403)
143 .json({ error: 'Cannot get this private video of another user' })
152 const videosRemoveValidator = [
153 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
155 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
156 logger.debug('Checking videosRemove parameters', { parameters: req.params })
158 if (areValidationErrors(req, res)) return
159 if (!await isVideoExist(req.params.id, res)) return
161 // Check if the user who did the request is able to delete the video
162 if (!checkUserCanDeleteVideo(res.locals.oauth.token.User, res.locals.video, res)) return
168 const videosSearchValidator = [
169 query('search').not().isEmpty().withMessage('Should have a valid search'),
171 (req: express.Request, res: express.Response, next: express.NextFunction) => {
172 logger.debug('Checking videosSearch parameters', { parameters: req.params })
174 if (areValidationErrors(req, res)) return
180 const videoAbuseReportValidator = [
181 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
182 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
184 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
185 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
187 if (areValidationErrors(req, res)) return
188 if (!await isVideoExist(req.params.id, res)) return
194 const videoRateValidator = [
195 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
196 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
198 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
199 logger.debug('Checking videoRate parameters', { parameters: req.body })
201 if (areValidationErrors(req, res)) return
202 if (!await isVideoExist(req.params.id, res)) return
208 const videosShareValidator = [
209 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
210 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
212 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
213 logger.debug('Checking videoShare parameters', { parameters: req.params })
215 if (areValidationErrors(req, res)) return
216 if (!await isVideoExist(req.params.id, res)) return
218 const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
220 return res.status(404)
224 res.locals.videoShare = share
229 // ---------------------------------------------------------------------------
233 videosUpdateValidator,
235 videosRemoveValidator,
236 videosSearchValidator,
237 videosShareValidator,
239 videoAbuseReportValidator,
244 // ---------------------------------------------------------------------------
246 function checkUserCanDeleteVideo (user: UserModel, video: VideoModel, res: express.Response) {
247 // Retrieve the user who did the request
248 if (video.isOwned() === false) {
250 .json({ error: 'Cannot remove video of another server, blacklist it' })
255 // Check if the user can delete the video
256 // The user can delete it if he has the right
257 // Or if s/he is the video's account
258 const account = video.VideoChannel.Account
259 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
261 .json({ error: 'Cannot remove video of another user' })