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 { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
7 isVideoAbuseReasonValid, isVideoCategoryValid, isVideoDescriptionValid, isVideoExist, isVideoFile, isVideoLanguageValid,
8 isVideoLicenceValid, isVideoNameValid, isVideoNSFWValid, 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(isVideoNSFWValid).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'),
35 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
36 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
38 if (areValidationErrors(req, res)) return
40 const videoFile: Express.Multer.File = req.files['videofile'][0]
41 const user = res.locals.oauth.token.User
43 const videoChannel = await VideoChannelModel.loadByIdAndAccount(req.body.channelId, user.Account.id)
46 .json({ error: 'Unknown video video channel for this account.' })
52 res.locals.videoChannel = videoChannel
54 const isAble = await user.isAbleToUploadVideo(videoFile)
55 if (isAble === false) {
57 .json({ error: 'The user video quota is exceeded with this video.' })
66 duration = await getDurationFromVideoFile(videoFile.path)
68 logger.error('Invalid input file in videosAddValidator.', err)
70 .json({ error: 'Invalid input file.' })
76 videoFile['duration'] = duration
82 const videosUpdateValidator = [
83 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
84 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
85 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
86 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
87 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
88 body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
89 body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
90 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
91 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
93 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
94 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
96 if (areValidationErrors(req, res)) return
97 if (!await isVideoExist(req.params.id, res)) return
99 const video = res.locals.video
101 // We need to make additional checks
102 if (video.isOwned() === false) {
103 return res.status(403)
104 .json({ error: 'Cannot update video of another server' })
108 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
109 return res.status(403)
110 .json({ error: 'Cannot update video of another user' })
114 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 anymore.' })
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 not private, anyone can access it
136 if (video.privacy !== VideoPrivacy.PRIVATE) return next()
138 authenticate(req, res, () => {
139 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
140 return res.status(403)
141 .json({ error: 'Cannot get this private video of another user' })
150 const videosRemoveValidator = [
151 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
153 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
154 logger.debug('Checking videosRemove parameters', { parameters: req.params })
156 if (areValidationErrors(req, res)) return
157 if (!await isVideoExist(req.params.id, res)) return
159 // Check if the user who did the request is able to delete the video
160 if (!checkUserCanDeleteVideo(res.locals.oauth.token.User, res.locals.video, res)) return
166 const videosSearchValidator = [
167 query('search').not().isEmpty().withMessage('Should have a valid search'),
169 (req: express.Request, res: express.Response, next: express.NextFunction) => {
170 logger.debug('Checking videosSearch parameters', { parameters: req.params })
172 if (areValidationErrors(req, res)) return
178 const videoAbuseReportValidator = [
179 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
180 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
182 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
183 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
185 if (areValidationErrors(req, res)) return
186 if (!await isVideoExist(req.params.id, res)) return
192 const videoRateValidator = [
193 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
194 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
196 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
197 logger.debug('Checking videoRate parameters', { parameters: req.body })
199 if (areValidationErrors(req, res)) return
200 if (!await isVideoExist(req.params.id, res)) return
206 const videosShareValidator = [
207 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
208 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
210 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
211 logger.debug('Checking videoShare parameters', { parameters: req.params })
213 if (areValidationErrors(req, res)) return
214 if (!await isVideoExist(req.params.id, res)) return
216 const share = await VideoShareModel.load(req.params.accountId, res.locals.video.id, undefined)
218 return res.status(404)
222 res.locals.videoShare = share
227 // ---------------------------------------------------------------------------
231 videosUpdateValidator,
233 videosRemoveValidator,
234 videosSearchValidator,
235 videosShareValidator,
237 videoAbuseReportValidator,
242 // ---------------------------------------------------------------------------
244 function checkUserCanDeleteVideo (user: UserModel, video: VideoModel, res: express.Response) {
245 // Retrieve the user who did the request
246 if (video.isOwned() === false) {
248 .json({ error: 'Cannot remove video of another server, blacklist it' })
253 // Check if the user can delete the video
254 // The user can delete it if s/he is an admin
255 // Or if s/he is the video's account
256 const account = video.VideoChannel.Account
257 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
259 .json({ error: 'Cannot remove video of another user' })