1
0
Fork 0
peertube/server/middlewares/validators/videos.ts

233 lines
8.2 KiB
TypeScript
Raw Normal View History

2017-09-15 10:17:08 +00:00
import { body, param, query } from 'express-validator/check'
2017-06-10 20:15:25 +00:00
import * as express from 'express'
2017-05-22 18:58:25 +00:00
import { database as db } from '../../initializers/database'
2017-05-15 20:22:03 +00:00
import { checkErrors } from './utils'
import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers'
2017-09-15 10:17:08 +00:00
import {
logger,
isVideoDurationValid,
isVideoFile,
isVideoNameValid,
isVideoCategoryValid,
isVideoLicenceValid,
isVideoDescriptionValid,
isVideoLanguageValid,
isVideoTagsValid,
isVideoNSFWValid,
isVideoIdOrUUIDValid,
isVideoAbuseReasonValid,
isVideoRatingTypeValid,
2017-10-10 08:02:18 +00:00
getDurationFromVideoFile,
checkVideoExists
2017-09-15 10:17:08 +00:00
} from '../../helpers'
2015-11-07 13:16:26 +00:00
2017-09-15 10:17:08 +00: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 10:17:08 +00: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'),
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
user.isAbleToUploadVideo(videoFile)
.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 10:17:08 +00: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
if (duration === undefined) return
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 18:07:54 +00:00
return undefined
2017-09-15 10:17:08 +00:00
})
})
}
]
const videosUpdateValidator = [
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
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('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, () => {
// We need to make additional checks
if (res.locals.video.isOwned() === false) {
return res.status(403)
.json({ error: 'Cannot update video of another pod' })
.end()
2017-09-04 18:07:54 +00:00
}
2017-09-15 10:17:08 +00:00
if (res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
return res.status(403)
.json({ error: 'Cannot update video of another user' })
.end()
}
2016-05-16 17:49:10 +00:00
next()
})
2017-01-11 18:15:23 +00:00
})
2017-09-15 10:17:08 +00:00
}
]
2016-02-04 20:10:33 +00:00
2017-09-15 10:17:08 +00:00
const videosGetValidator = [
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
2015-11-07 13:16:26 +00:00
2017-09-15 10:17:08 +00:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videosGet parameters', { parameters: req.params })
2016-12-29 18:07:05 +00:00
2017-09-15 10:17:08 +00:00
checkErrors(req, res, () => {
checkVideoExists(req.params.id, res, next)
})
}
]
2015-11-07 13:16:26 +00:00
2017-09-15 10:17:08 +00:00
const videosRemoveValidator = [
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
2015-11-07 13:16:26 +00:00
2017-09-15 10:17:08 +00:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videosRemove parameters', { parameters: req.params })
2015-11-07 13:16:26 +00:00
2017-09-15 10:17:08 +00: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 19:22:10 +00:00
})
2015-11-07 13:16:26 +00:00
})
2017-09-15 10:17:08 +00:00
}
]
2015-11-07 13:16:26 +00:00
2017-09-15 10:17:08 +00: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 10:23:52 +00:00
2017-09-15 10:17:08 +00:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videosSearch parameters', { parameters: req.params })
2016-01-31 10:23:52 +00:00
2017-09-15 10:17:08 +00:00
checkErrors(req, res, next)
}
]
2016-01-31 10:23:52 +00:00
2017-09-15 10:17:08 +00:00
const videoAbuseReportValidator = [
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
body('reason').custom(isVideoAbuseReasonValid).withMessage('Should have a valid reason'),
2017-01-04 19:59:23 +00:00
2017-09-15 10:17:08 +00:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
2017-01-04 19:59:23 +00:00
2017-09-15 10:17:08 +00:00
checkErrors(req, res, () => {
checkVideoExists(req.params.id, res, next)
})
}
]
2017-01-04 19:59:23 +00:00
2017-09-15 10:17:08 +00:00
const videoRateValidator = [
param('id').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
body('rating').custom(isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
2017-03-08 20:35:43 +00:00
2017-09-15 10:17:08 +00:00
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking videoRate parameters', { parameters: req.body })
2017-03-08 20:35:43 +00:00
2017-09-15 10:17:08 +00:00
checkErrors(req, res, () => {
checkVideoExists(req.params.id, res, next)
})
}
]
2017-03-08 20:35:43 +00:00
// ---------------------------------------------------------------------------
2016-01-31 10:23:52 +00:00
2017-05-15 20:22:03 +00:00
export {
videosAddValidator,
videosUpdateValidator,
videosGetValidator,
videosRemoveValidator,
videosSearchValidator,
videoAbuseReportValidator,
2017-10-10 08:02:18 +00:00
videoRateValidator
2017-05-15 20:22:03 +00:00
}
2016-12-29 18:07:05 +00:00
// ---------------------------------------------------------------------------
2017-06-10 20:15:25 +00: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 19:22:10 +00:00
// Retrieve the user who did the request
db.User.loadById(userId)
.then(user => {
2017-09-12 10:53:55 +00:00
if (res.locals.video.isOwned() === false) {
return res.status(403)
.json({ error: 'Cannot remove video of another pod, blacklist it' })
.end()
2017-09-12 10:53:55 +00:00
}
// Check if the user can delete the video
// The user can delete it if s/he is an admin
// Or if s/he is the video's author
2017-09-12 10:53:55 +00:00
if (user.isAdmin() === false && res.locals.video.Author.userId !== res.locals.oauth.token.User.id) {
return res.status(403)
.json({ error: 'Cannot remove video of another user' })
.end()
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 19:22:10 +00:00
}
// If we reach this comment, we can delete the video
callback()
})
.catch(err => {
2017-07-07 16:26:12 +00:00
logger.error('Error in video request validator.', err)
return res.sendStatus(500)
})
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 19:22:10 +00:00
}