1 import * as express from 'express'
2 import { body, param, query } from 'express-validator/check'
3 import { UserRight, VideoPrivacy } from '../../../shared'
4 import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc'
6 isVideoAbuseReasonValid,
8 isVideoDescriptionValid,
16 isVideoRatingTypeValid,
18 } from '../../helpers/custom-validators/videos'
19 import { getDurationFromVideoFile } from '../../helpers/ffmpeg-utils'
20 import { logger } from '../../helpers/logger'
21 import { CONSTRAINTS_FIELDS } from '../../initializers'
22 import { database as db } from '../../initializers/database'
23 import { UserInstance } from '../../models/account/user-interface'
24 import { VideoInstance } from '../../models/video/video-interface'
25 import { authenticate } from '../oauth'
26 import { areValidationErrors } from './utils'
28 const videosAddValidator = [
29 body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
30 'This file is not supported. Please, make sure it is of the following type : '
31 + CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
33 body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
34 body('category').custom(isVideoCategoryValid).withMessage('Should have a valid category'),
35 body('licence').custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
36 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
37 body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
38 body('description').custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
39 body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
40 body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
41 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
43 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
46 if (areValidationErrors(req, res)) return
48 const videoFile: Express.Multer.File = req.files['videofile'][0]
49 const user = res.locals.oauth.token.User
51 const videoChannel = await db.VideoChannel.loadByIdAndAccount(req.body.channelId, user.Account.id)
54 .json({ error: 'Unknown video video channel for this account.' })
60 res.locals.videoChannel = videoChannel
62 const isAble = await user.isAbleToUploadVideo(videoFile)
63 if (isAble === false) {
65 .json({ error: 'The user video quota is exceeded with this video.' })
74 duration = await getDurationFromVideoFile(videoFile.path)
76 logger.error('Invalid input file in videosAddValidator.', err)
78 .json({ error: 'Invalid input file.' })
84 videoFile['duration'] = duration
90 const videosUpdateValidator = [
91 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
92 body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
93 body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
94 body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
95 body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
96 body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
97 body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
98 body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
99 body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
101 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
102 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
104 if (areValidationErrors(req, res)) return
105 if (!await isVideoExist(req.params.id, res)) return
107 const video = res.locals.video
109 // We need to make additional checks
110 if (video.isOwned() === false) {
111 return res.status(403)
112 .json({ error: 'Cannot update video of another server' })
116 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
117 return res.status(403)
118 .json({ error: 'Cannot update video of another user' })
122 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
123 return res.status(409)
124 .json({ error: 'Cannot set "private" a video that was not private anymore.' })
132 const videosGetValidator = [
133 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
135 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
136 logger.debug('Checking videosGet parameters', { parameters: req.params })
138 if (areValidationErrors(req, res)) return
139 if (!await isVideoExist(req.params.id, res)) return
141 const video = res.locals.video
143 // Video is not private, anyone can access it
144 if (video.privacy !== VideoPrivacy.PRIVATE) return next()
146 authenticate(req, res, () => {
147 if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
148 return res.status(403)
149 .json({ error: 'Cannot get this private video of another user' })
158 const videosRemoveValidator = [
159 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
161 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
162 logger.debug('Checking videosRemove parameters', { parameters: req.params })
164 if (areValidationErrors(req, res)) return
165 if (!await isVideoExist(req.params.id, res)) return
167 // Check if the user who did the request is able to delete the video
168 if (!checkUserCanDeleteVideo(res.locals.oauth.token.User, res.locals.video, res)) return
174 const videosSearchValidator = [
175 query('search').not().isEmpty().withMessage('Should have a valid search'),
177 (req: express.Request, res: express.Response, next: express.NextFunction) => {
178 logger.debug('Checking videosSearch parameters', { parameters: req.params })
180 if (areValidationErrors(req, res)) return
186 const videoAbuseReportValidator = [
187 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
188 body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
190 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
191 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
193 if (areValidationErrors(req, res)) return
194 if (!await isVideoExist(req.params.id, res)) return
200 const videoRateValidator = [
201 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
202 body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
204 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
205 logger.debug('Checking videoRate parameters', { parameters: req.body })
207 if (areValidationErrors(req, res)) return
208 if (!await isVideoExist(req.params.id, res)) return
214 const videosShareValidator = [
215 param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
216 param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'),
218 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
219 logger.debug('Checking videoShare parameters', { parameters: req.params })
221 if (areValidationErrors(req, res)) return
222 if (!await isVideoExist(req.params.id, res)) return
224 const share = await db.VideoShare.load(req.params.accountId, res.locals.video.id, undefined)
226 return res.status(404)
230 res.locals.videoShare = share
235 // ---------------------------------------------------------------------------
239 videosUpdateValidator,
241 videosRemoveValidator,
242 videosSearchValidator,
243 videosShareValidator,
245 videoAbuseReportValidator,
250 // ---------------------------------------------------------------------------
252 function checkUserCanDeleteVideo (user: UserInstance, video: VideoInstance, res: express.Response) {
253 // Retrieve the user who did the request
254 if (video.isOwned() === false) {
256 .json({ error: 'Cannot remove video of another server, blacklist it' })
261 // Check if the user can delete the video
262 // The user can delete it if s/he is an admin
263 // Or if s/he is the video's account
264 const account = video.VideoChannel.Account
265 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
267 .json({ error: 'Cannot remove video of another user' })