PeerTube/server/middlewares/validators/videos.ts

271 lines
9.6 KiB
TypeScript
Raw Normal View History

2017-09-15 05:17:08 -05:00
import { body, param, query } from 'express-validator/check'
2017-06-10 15:15:25 -05:00
import * as express from 'express'
2017-05-22 13:58:25 -05:00
import { database as db } from '../../initializers/database'
2017-05-15 15:22:03 -05:00
import { checkErrors } from './utils'
import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
2017-09-15 05:17:08 -05:00
import {
logger,
isVideoDurationValid,
isVideoFile,
isVideoNameValid,
isVideoCategoryValid,
isVideoLicenceValid,
isVideoDescriptionValid,
isVideoLanguageValid,
isVideoTagsValid,
isVideoNSFWValid,
2017-10-24 12:41:09 -05:00
isIdOrUUIDValid,
2017-09-15 05:17:08 -05:00
isVideoAbuseReasonValid,
isVideoRatingTypeValid,
2017-10-10 03:02:18 -05:00
getDurationFromVideoFile,
2017-10-24 12:41:09 -05:00
checkVideoExists,
2017-10-31 05:52:52 -05:00
isIdValid,
isVideoPrivacyValid
2017-09-15 05:17:08 -05:00
} from '../../helpers'
2017-10-31 05:52:52 -05:00
import { UserRight, VideoPrivacy } from '../../../shared'
import { authenticate } from '../oauth'
2015-11-07 07:16:26 -06:00
2017-09-15 05:17:08 -05:00
const videosAddValidator = [
body('videofile').custom((value, { req }) => isVideoFile(req.files)).withMessage(
'This file is not supported. Please, make sure it is of the following type : '
+ CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
),
2017-09-15 05:17:08 -05:00
body('name').custom(isVideoNameValid).withMessage('Should have a valid name'),
body('category').custom(isVideoCategoryValid).withMessage('Should have a valid category'),
body('licence').custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
body('nsfw').custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
body('description').custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
2017-10-24 12:41:09 -05:00
body('channelId').custom(isIdValid).withMessage('Should have correct video channel id'),
2017-10-31 05:52:52 -05:00
body('privacy').custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
2017-09-15 05:17:08 -05:00
body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
checkErrors(req, res, () => {
const videoFile: Express.Multer.File = req.files['videofile'][0]
const user = res.locals.oauth.token.User
2017-11-10 07:48:08 -06:00
return db.VideoChannel.loadByIdAndAccount(req.body.channelId, user.Account.id)
2017-10-24 12:41:09 -05:00
.then(videoChannel => {
if (!videoChannel) {
res.status(400)
2017-11-10 07:48:08 -06:00
.json({ error: 'Unknown video video channel for this account.' })
2017-10-24 12:41:09 -05:00
.end()
return undefined
}
res.locals.videoChannel = videoChannel
return user.isAbleToUploadVideo(videoFile)
})
2017-09-15 05:17:08 -05:00
.then(isAble => {
if (isAble === false) {
res.status(403)
.json({ error: 'The user video quota is exceeded with this video.' })
.end()
return undefined
}
return getDurationFromVideoFile(videoFile.path)
2017-09-15 05:17:08 -05:00
.catch(err => {
logger.error('Invalid input file in videosAddValidator.', err)
res.status(400)
.json({ error: 'Invalid input file.' })
.end()
return undefined
})
})
.then(duration => {
// Previous test failed, abort
2017-10-25 09:52:01 -05:00
if (duration === undefined) return undefined
2017-09-15 05:17:08 -05:00
if (!isVideoDurationValid('' + duration)) {
return res.status(400)
.json({
error: 'Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).'
})
.end()
}
videoFile['duration'] = duration
next()
})
.catch(err => {
logger.error('Error in video add validator', err)
res.sendStatus(500)
2017-09-04 13:07:54 -05:00
return undefined
2017-09-15 05:17:08 -05:00
})
})
}
]
const videosUpdateValidator = [
2017-10-24 12:41:09 -05:00
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
2017-09-15 05:17:08 -05:00
body('name').optional().custom(isVideoNameValid).withMessage('Should have a valid name'),
body('category').optional().custom(isVideoCategoryValid).withMessage('Should have a valid category'),
body('licence').optional().custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
body('language').optional().custom(isVideoLanguageValid).withMessage('Should have a valid language'),
body('nsfw').optional().custom(isVideoNSFWValid).withMessage('Should have a valid NSFW attribute'),
body('privacy').optional().custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
2017-09-15 05:17:08 -05:00
body('description').optional().custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
body('tags').optional().custom(isVideoTagsValid).withMessage('Should have correct tags'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videosUpdate parameters', { parameters: req.body })
checkErrors(req, res, () => {
checkVideoExists(req.params.id, res, () => {
2017-10-31 05:52:52 -05:00
const video = res.locals.video
2017-09-15 05:17:08 -05:00
// We need to make additional checks
2017-10-31 05:52:52 -05:00
if (video.isOwned() === false) {
2017-09-15 05:17:08 -05:00
return res.status(403)
2017-11-15 04:00:25 -06:00
.json({ error: 'Cannot update video of another server' })
2017-09-15 05:17:08 -05:00
.end()
2017-09-04 13:07:54 -05:00
}
2017-11-10 07:48:08 -06:00
if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
2017-09-15 05:17:08 -05:00
return res.status(403)
.json({ error: 'Cannot update video of another user' })
.end()
}
2016-05-16 12:49:10 -05:00
2017-10-31 05:52:52 -05:00
if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
return res.status(409)
.json({ error: 'Cannot set "private" a video that was not private anymore.' })
.end()
}
next()
})
2017-01-11 12:15:23 -06:00
})
2017-09-15 05:17:08 -05:00
}
]
2016-02-04 14:10:33 -06:00
2017-09-15 05:17:08 -05:00
const videosGetValidator = [
2017-10-24 12:41:09 -05:00
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
2015-11-07 07:16:26 -06:00
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videosGet parameters', { parameters: req.params })
2016-12-29 12:07:05 -06:00
2017-09-15 05:17:08 -05:00
checkErrors(req, res, () => {
checkVideoExists(req.params.id, res, () => {
const video = res.locals.video
// Video is not private, anyone can access it
if (video.privacy !== VideoPrivacy.PRIVATE) return next()
authenticate(req, res, () => {
2017-11-10 07:48:08 -06:00
if (video.VideoChannel.Account.userId !== res.locals.oauth.token.User.id) {
return res.status(403)
.json({ error: 'Cannot get this private video of another user' })
.end()
}
next()
})
})
2017-09-15 05:17:08 -05:00
})
}
]
2015-11-07 07:16:26 -06:00
2017-09-15 05:17:08 -05:00
const videosRemoveValidator = [
2017-10-24 12:41:09 -05:00
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
2015-11-07 07:16:26 -06:00
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videosRemove parameters', { parameters: req.params })
2015-11-07 07:16:26 -06:00
2017-09-15 05:17:08 -05:00
checkErrors(req, res, () => {
checkVideoExists(req.params.id, res, () => {
// Check if the user who did the request is able to delete the video
checkUserCanDeleteVideo(res.locals.oauth.token.User.id, res, () => {
next()
})
Add ability for an administrator to remove any video (#61) * Add ability for an admin to remove every video on the pod. * Server: add BlacklistedVideos relation. * Server: Insert in BlacklistedVideos relation upon deletion of a video. * Server: Modify BlacklistedVideos schema to add Pod id information. * Server: Moving insertion of a blacklisted video from the `afterDestroy` hook into the process of deletion of a video. To avoid inserting a video when it is removed on its origin pod. When a video is removed on its origin pod, the `afterDestroy` hook is fire, but no request is made on the delete('/:videoId') interface. Hence, we insert into `BlacklistedVideos` only on request on delete('/:videoId') (if requirements for insertion are met). * Server: Add removeVideoFromBlacklist hook on deletion of a video. We are going to proceed in another way :). We will add a new route : /:videoId/blacklist to blacklist a video. We do not blacklist a video upon its deletion now (to distinguish a video blacklist from a regular video delete) When we blacklist a video, the video remains in the DB, so we don't have any concern about its update. It just doesn't appear in the video list. When we remove a video, we then have to remove it from the blacklist too. We could also remove a video from the blacklist to 'unremove' it and make it appear again in the video list (will be another feature). * Server: Add handler for new route post(/:videoId/blacklist) * Client: Add isBlacklistable method * Client: Update isRemovableBy method. * Client: Move 'Delete video' feature from the video-list to the video-watch module. * Server: Exclude blacklisted videos from the video list * Server: Use findAll() in BlacklistedVideos.list() method * Server: Fix addVideoToBlacklist function. * Client: Add blacklist feature. * Server: Use JavaScript Standard Style. * Server: In checkUserCanDeleteVideo, move the callback call inside the db callback function * Server: Modify BlacklistVideo relation * Server: Modifiy Videos methods. * Server: Add checkVideoIsBlacklistable method * Server: Rewrite addVideoToBlacklist method * Server: Fix checkVideoIsBlacklistable method * Server: Add return to addVideoToBlacklist method
2017-04-26 14:22:10 -05:00
})
2015-11-07 07:16:26 -06:00
})
2017-09-15 05:17:08 -05:00
}
]
2015-11-07 07:16:26 -06:00
2017-09-15 05:17:08 -05:00
const videosSearchValidator = [
param('value').not().isEmpty().withMessage('Should have a valid search'),
query('field').optional().isIn(SEARCHABLE_COLUMNS.VIDEOS).withMessage('Should have correct searchable column'),
2016-01-31 04:23:52 -06:00
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videosSearch parameters', { parameters: req.params })
2016-01-31 04:23:52 -06:00
2017-09-15 05:17:08 -05:00
checkErrors(req, res, next)
}
]
2016-01-31 04:23:52 -06:00
2017-09-15 05:17:08 -05:00
const videoAbuseReportValidator = [
2017-10-24 12:41:09 -05:00
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
2017-09-15 05:17:08 -05:00
body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
2017-01-04 13:59:23 -06:00
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
2017-01-04 13:59:23 -06:00
2017-09-15 05:17:08 -05:00
checkErrors(req, res, () => {
checkVideoExists(req.params.id, res, next)
})
}
]
2017-01-04 13:59:23 -06:00
2017-09-15 05:17:08 -05:00
const videoRateValidator = [
2017-10-24 12:41:09 -05:00
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
2017-09-15 05:17:08 -05:00
body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
2017-03-08 14:35:43 -06:00
2017-09-15 05:17:08 -05:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoRate parameters', { parameters: req.body })
2017-03-08 14:35:43 -06:00
2017-09-15 05:17:08 -05:00
checkErrors(req, res, () => {
checkVideoExists(req.params.id, res, next)
})
}
]
2017-03-08 14:35:43 -06:00
// ---------------------------------------------------------------------------
2016-01-31 04:23:52 -06:00
2017-05-15 15:22:03 -05:00
export {
videosAddValidator,
videosUpdateValidator,
videosGetValidator,
videosRemoveValidator,
videosSearchValidator,
videoAbuseReportValidator,
2017-10-10 03:02:18 -05:00
videoRateValidator
2017-05-15 15:22:03 -05:00
}
2016-12-29 12:07:05 -06:00
// ---------------------------------------------------------------------------
2017-06-10 15:15:25 -05:00
function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) {
Add ability for an administrator to remove any video (#61) * Add ability for an admin to remove every video on the pod. * Server: add BlacklistedVideos relation. * Server: Insert in BlacklistedVideos relation upon deletion of a video. * Server: Modify BlacklistedVideos schema to add Pod id information. * Server: Moving insertion of a blacklisted video from the `afterDestroy` hook into the process of deletion of a video. To avoid inserting a video when it is removed on its origin pod. When a video is removed on its origin pod, the `afterDestroy` hook is fire, but no request is made on the delete('/:videoId') interface. Hence, we insert into `BlacklistedVideos` only on request on delete('/:videoId') (if requirements for insertion are met). * Server: Add removeVideoFromBlacklist hook on deletion of a video. We are going to proceed in another way :). We will add a new route : /:videoId/blacklist to blacklist a video. We do not blacklist a video upon its deletion now (to distinguish a video blacklist from a regular video delete) When we blacklist a video, the video remains in the DB, so we don't have any concern about its update. It just doesn't appear in the video list. When we remove a video, we then have to remove it from the blacklist too. We could also remove a video from the blacklist to 'unremove' it and make it appear again in the video list (will be another feature). * Server: Add handler for new route post(/:videoId/blacklist) * Client: Add isBlacklistable method * Client: Update isRemovableBy method. * Client: Move 'Delete video' feature from the video-list to the video-watch module. * Server: Exclude blacklisted videos from the video list * Server: Use findAll() in BlacklistedVideos.list() method * Server: Fix addVideoToBlacklist function. * Client: Add blacklist feature. * Server: Use JavaScript Standard Style. * Server: In checkUserCanDeleteVideo, move the callback call inside the db callback function * Server: Modify BlacklistVideo relation * Server: Modifiy Videos methods. * Server: Add checkVideoIsBlacklistable method * Server: Rewrite addVideoToBlacklist method * Server: Fix checkVideoIsBlacklistable method * Server: Add return to addVideoToBlacklist method
2017-04-26 14:22:10 -05:00
// Retrieve the user who did the request
if (res.locals.video.isOwned() === false) {
return res.status(403)
2017-11-15 04:00:25 -06:00
.json({ error: 'Cannot remove video of another server, blacklist it' })
.end()
}
// Check if the user can delete the video
// The user can delete it if s/he is an admin
2017-11-10 07:48:08 -06:00
// Or if s/he is the video's account
const account = res.locals.video.VideoChannel.Account
const user = res.locals.oauth.token.User
2017-11-10 07:48:08 -06:00
if (user.hasRight(UserRight.REMOVE_ANY_VIDEO) === false && account.userId !== user.id) {
return res.status(403)
.json({ error: 'Cannot remove video of another user' })
.end()
}
// If we reach this comment, we can delete the video
callback()
Add ability for an administrator to remove any video (#61) * Add ability for an admin to remove every video on the pod. * Server: add BlacklistedVideos relation. * Server: Insert in BlacklistedVideos relation upon deletion of a video. * Server: Modify BlacklistedVideos schema to add Pod id information. * Server: Moving insertion of a blacklisted video from the `afterDestroy` hook into the process of deletion of a video. To avoid inserting a video when it is removed on its origin pod. When a video is removed on its origin pod, the `afterDestroy` hook is fire, but no request is made on the delete('/:videoId') interface. Hence, we insert into `BlacklistedVideos` only on request on delete('/:videoId') (if requirements for insertion are met). * Server: Add removeVideoFromBlacklist hook on deletion of a video. We are going to proceed in another way :). We will add a new route : /:videoId/blacklist to blacklist a video. We do not blacklist a video upon its deletion now (to distinguish a video blacklist from a regular video delete) When we blacklist a video, the video remains in the DB, so we don't have any concern about its update. It just doesn't appear in the video list. When we remove a video, we then have to remove it from the blacklist too. We could also remove a video from the blacklist to 'unremove' it and make it appear again in the video list (will be another feature). * Server: Add handler for new route post(/:videoId/blacklist) * Client: Add isBlacklistable method * Client: Update isRemovableBy method. * Client: Move 'Delete video' feature from the video-list to the video-watch module. * Server: Exclude blacklisted videos from the video list * Server: Use findAll() in BlacklistedVideos.list() method * Server: Fix addVideoToBlacklist function. * Client: Add blacklist feature. * Server: Use JavaScript Standard Style. * Server: In checkUserCanDeleteVideo, move the callback call inside the db callback function * Server: Modify BlacklistVideo relation * Server: Modifiy Videos methods. * Server: Add checkVideoIsBlacklistable method * Server: Rewrite addVideoToBlacklist method * Server: Fix checkVideoIsBlacklistable method * Server: Add return to addVideoToBlacklist method
2017-04-26 14:22:10 -05:00
}