aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares')
-rw-r--r--server/middlewares/validators/avatar.ts3
-rw-r--r--server/middlewares/validators/config.ts1
-rw-r--r--server/middlewares/validators/index.ts1
-rw-r--r--server/middlewares/validators/sort.ts3
-rw-r--r--server/middlewares/validators/video-captions.ts7
-rw-r--r--server/middlewares/validators/video-channels.ts4
-rw-r--r--server/middlewares/validators/video-imports.ts47
-rw-r--r--server/middlewares/validators/videos.ts84
8 files changed, 103 insertions, 47 deletions
diff --git a/server/middlewares/validators/avatar.ts b/server/middlewares/validators/avatar.ts
index f346ea92f..5860735c6 100644
--- a/server/middlewares/validators/avatar.ts
+++ b/server/middlewares/validators/avatar.ts
@@ -4,6 +4,7 @@ import { isAvatarFile } from '../../helpers/custom-validators/users'
4import { areValidationErrors } from './utils' 4import { areValidationErrors } from './utils'
5import { CONSTRAINTS_FIELDS } from '../../initializers' 5import { CONSTRAINTS_FIELDS } from '../../initializers'
6import { logger } from '../../helpers/logger' 6import { logger } from '../../helpers/logger'
7import { cleanUpReqFiles } from '../../helpers/utils'
7 8
8const updateAvatarValidator = [ 9const updateAvatarValidator = [
9 body('avatarfile').custom((value, { req }) => isAvatarFile(req.files)).withMessage( 10 body('avatarfile').custom((value, { req }) => isAvatarFile(req.files)).withMessage(
@@ -14,7 +15,7 @@ const updateAvatarValidator = [
14 (req: express.Request, res: express.Response, next: express.NextFunction) => { 15 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking updateAvatarValidator parameters', { files: req.files }) 16 logger.debug('Checking updateAvatarValidator parameters', { files: req.files })
16 17
17 if (areValidationErrors(req, res)) return 18 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
18 19
19 return next() 20 return next()
20 } 21 }
diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts
index f58c0676c..9d303eee2 100644
--- a/server/middlewares/validators/config.ts
+++ b/server/middlewares/validators/config.ts
@@ -24,6 +24,7 @@ const customConfigUpdateValidator = [
24 body('transcoding.resolutions.480p').isBoolean().withMessage('Should have a valid transcoding 480p resolution enabled boolean'), 24 body('transcoding.resolutions.480p').isBoolean().withMessage('Should have a valid transcoding 480p resolution enabled boolean'),
25 body('transcoding.resolutions.720p').isBoolean().withMessage('Should have a valid transcoding 720p resolution enabled boolean'), 25 body('transcoding.resolutions.720p').isBoolean().withMessage('Should have a valid transcoding 720p resolution enabled boolean'),
26 body('transcoding.resolutions.1080p').isBoolean().withMessage('Should have a valid transcoding 1080p resolution enabled boolean'), 26 body('transcoding.resolutions.1080p').isBoolean().withMessage('Should have a valid transcoding 1080p resolution enabled boolean'),
27 body('import.videos.http.enabled').isBoolean().withMessage('Should have a valid import video http enabled boolean'),
27 28
28 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 29 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
29 logger.debug('Checking customConfigUpdateValidator parameters', { parameters: req.body }) 30 logger.debug('Checking customConfigUpdateValidator parameters', { parameters: req.body })
diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts
index e3f0f5963..c5400c8f5 100644
--- a/server/middlewares/validators/index.ts
+++ b/server/middlewares/validators/index.ts
@@ -11,3 +11,4 @@ export * from './video-blacklist'
11export * from './video-channels' 11export * from './video-channels'
12export * from './webfinger' 12export * from './webfinger'
13export * from './search' 13export * from './search'
14export * from './video-imports'
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts
index 00bde548c..d85611773 100644
--- a/server/middlewares/validators/sort.ts
+++ b/server/middlewares/validators/sort.ts
@@ -8,6 +8,7 @@ const SORTABLE_JOBS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.JOBS)
8const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) 8const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
9const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) 9const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
10const SORTABLE_VIDEOS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS_SEARCH) 10const SORTABLE_VIDEOS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS_SEARCH)
11const SORTABLE_VIDEO_IMPORTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_IMPORTS)
11const SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS) 12const SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS)
12const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) 13const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS)
13const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS) 14const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS)
@@ -19,6 +20,7 @@ const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS)
19const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS) 20const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS)
20const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS) 21const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS)
21const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) 22const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS)
23const videoImportsSortValidator = checkSort(SORTABLE_VIDEO_IMPORTS_COLUMNS)
22const videosSearchSortValidator = checkSort(SORTABLE_VIDEOS_SEARCH_COLUMNS) 24const videosSearchSortValidator = checkSort(SORTABLE_VIDEOS_SEARCH_COLUMNS)
23const videoCommentThreadsSortValidator = checkSort(SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS) 25const videoCommentThreadsSortValidator = checkSort(SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS)
24const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) 26const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS)
@@ -32,6 +34,7 @@ export {
32 usersSortValidator, 34 usersSortValidator,
33 videoAbusesSortValidator, 35 videoAbusesSortValidator,
34 videoChannelsSortValidator, 36 videoChannelsSortValidator,
37 videoImportsSortValidator,
35 videosSearchSortValidator, 38 videosSearchSortValidator,
36 videosSortValidator, 39 videosSortValidator,
37 blacklistSortValidator, 40 blacklistSortValidator,
diff --git a/server/middlewares/validators/video-captions.ts b/server/middlewares/validators/video-captions.ts
index b6d92d380..18d537bc4 100644
--- a/server/middlewares/validators/video-captions.ts
+++ b/server/middlewares/validators/video-captions.ts
@@ -7,6 +7,7 @@ import { CONSTRAINTS_FIELDS } from '../../initializers'
7import { UserRight } from '../../../shared' 7import { UserRight } from '../../../shared'
8import { logger } from '../../helpers/logger' 8import { logger } from '../../helpers/logger'
9import { isVideoCaptionExist, isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions' 9import { isVideoCaptionExist, isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
10import { cleanUpReqFiles } from '../../helpers/utils'
10 11
11const addVideoCaptionValidator = [ 12const addVideoCaptionValidator = [
12 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), 13 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
@@ -20,12 +21,12 @@ const addVideoCaptionValidator = [
20 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 21 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
21 logger.debug('Checking addVideoCaption parameters', { parameters: req.body }) 22 logger.debug('Checking addVideoCaption parameters', { parameters: req.body })
22 23
23 if (areValidationErrors(req, res)) return 24 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
24 if (!await isVideoExist(req.params.videoId, res)) return 25 if (!await isVideoExist(req.params.videoId, res)) return cleanUpReqFiles(req)
25 26
26 // Check if the user who did the request is able to update the video 27 // Check if the user who did the request is able to update the video
27 const user = res.locals.oauth.token.User 28 const user = res.locals.oauth.token.User
28 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return 29 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
29 30
30 return next() 31 return next()
31 } 32 }
diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts
index 7f65f7290..143ce9582 100644
--- a/server/middlewares/validators/video-channels.ts
+++ b/server/middlewares/validators/video-channels.ts
@@ -1,7 +1,7 @@
1import * as express from 'express' 1import * as express from 'express'
2import { body, param } from 'express-validator/check' 2import { body, param } from 'express-validator/check'
3import { UserRight } from '../../../shared' 3import { UserRight } from '../../../shared'
4import { isAccountIdExist, isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts' 4import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
5import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' 5import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc'
6import { 6import {
7 isVideoChannelDescriptionValid, 7 isVideoChannelDescriptionValid,
@@ -13,8 +13,6 @@ import { logger } from '../../helpers/logger'
13import { UserModel } from '../../models/account/user' 13import { UserModel } from '../../models/account/user'
14import { VideoChannelModel } from '../../models/video/video-channel' 14import { VideoChannelModel } from '../../models/video/video-channel'
15import { areValidationErrors } from './utils' 15import { areValidationErrors } from './utils'
16import { isAvatarFile } from '../../helpers/custom-validators/users'
17import { CONSTRAINTS_FIELDS } from '../../initializers'
18 16
19const listVideoAccountChannelsValidator = [ 17const listVideoAccountChannelsValidator = [
20 param('accountName').exists().withMessage('Should have a valid account name'), 18 param('accountName').exists().withMessage('Should have a valid account name'),
diff --git a/server/middlewares/validators/video-imports.ts b/server/middlewares/validators/video-imports.ts
new file mode 100644
index 000000000..d806edfa3
--- /dev/null
+++ b/server/middlewares/validators/video-imports.ts
@@ -0,0 +1,47 @@
1import * as express from 'express'
2import { body } from 'express-validator/check'
3import { isIdValid } from '../../helpers/custom-validators/misc'
4import { logger } from '../../helpers/logger'
5import { areValidationErrors } from './utils'
6import { getCommonVideoAttributes } from './videos'
7import { isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
8import { cleanUpReqFiles } from '../../helpers/utils'
9import { isVideoChannelOfAccountExist, isVideoNameValid } from '../../helpers/custom-validators/videos'
10import { CONFIG } from '../../initializers/constants'
11
12const videoImportAddValidator = getCommonVideoAttributes().concat([
13 body('targetUrl').custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
14 body('channelId')
15 .toInt()
16 .custom(isIdValid).withMessage('Should have correct video channel id'),
17 body('name')
18 .optional()
19 .custom(isVideoNameValid).withMessage('Should have a valid name'),
20
21 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
22 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
23
24 const user = res.locals.oauth.token.User
25
26 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
27
28 if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true) {
29 cleanUpReqFiles(req)
30 return res.status(409)
31 .json({ error: 'Import is not enabled on this instance.' })
32 .end()
33 }
34
35 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
36
37 return next()
38 }
39])
40
41// ---------------------------------------------------------------------------
42
43export {
44 videoImportAddValidator
45}
46
47// ---------------------------------------------------------------------------
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts
index d9af2aa0a..c812d4677 100644
--- a/server/middlewares/validators/videos.ts
+++ b/server/middlewares/validators/videos.ts
@@ -35,6 +35,7 @@ import { CONSTRAINTS_FIELDS } from '../../initializers'
35import { VideoShareModel } from '../../models/video/video-share' 35import { VideoShareModel } from '../../models/video/video-share'
36import { authenticate } from '../oauth' 36import { authenticate } from '../oauth'
37import { areValidationErrors } from './utils' 37import { areValidationErrors } from './utils'
38import { cleanUpReqFiles } from '../../helpers/utils'
38 39
39const videosAddValidator = getCommonVideoAttributes().concat([ 40const videosAddValidator = getCommonVideoAttributes().concat([
40 body('videofile') 41 body('videofile')
@@ -50,13 +51,13 @@ const videosAddValidator = getCommonVideoAttributes().concat([
50 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 51 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
51 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files }) 52 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
52 53
53 if (areValidationErrors(req, res)) return 54 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
54 if (areErrorsInScheduleUpdate(req, res)) return 55 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
55 56
56 const videoFile: Express.Multer.File = req.files['videofile'][0] 57 const videoFile: Express.Multer.File = req.files['videofile'][0]
57 const user = res.locals.oauth.token.User 58 const user = res.locals.oauth.token.User
58 59
59 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return 60 if (!await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
60 61
61 const isAble = await user.isAbleToUploadVideo(videoFile) 62 const isAble = await user.isAbleToUploadVideo(videoFile)
62 if (isAble === false) { 63 if (isAble === false) {
@@ -64,7 +65,7 @@ const videosAddValidator = getCommonVideoAttributes().concat([
64 .json({ error: 'The user video quota is exceeded with this video.' }) 65 .json({ error: 'The user video quota is exceeded with this video.' })
65 .end() 66 .end()
66 67
67 return 68 return cleanUpReqFiles(req)
68 } 69 }
69 70
70 let duration: number 71 let duration: number
@@ -77,7 +78,7 @@ const videosAddValidator = getCommonVideoAttributes().concat([
77 .json({ error: 'Invalid input file.' }) 78 .json({ error: 'Invalid input file.' })
78 .end() 79 .end()
79 80
80 return 81 return cleanUpReqFiles(req)
81 } 82 }
82 83
83 videoFile['duration'] = duration 84 videoFile['duration'] = duration
@@ -99,23 +100,24 @@ const videosUpdateValidator = getCommonVideoAttributes().concat([
99 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 100 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
100 logger.debug('Checking videosUpdate parameters', { parameters: req.body }) 101 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
101 102
102 if (areValidationErrors(req, res)) return 103 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
103 if (areErrorsInScheduleUpdate(req, res)) return 104 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
104 if (!await isVideoExist(req.params.id, res)) return 105 if (!await isVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
105 106
106 const video = res.locals.video 107 const video = res.locals.video
107 108
108 // Check if the user who did the request is able to update the video 109 // Check if the user who did the request is able to update the video
109 const user = res.locals.oauth.token.User 110 const user = res.locals.oauth.token.User
110 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return 111 if (!checkUserCanManageVideo(user, res.locals.video, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
111 112
112 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) { 113 if (video.privacy !== VideoPrivacy.PRIVATE && req.body.privacy === VideoPrivacy.PRIVATE) {
114 cleanUpReqFiles(req)
113 return res.status(409) 115 return res.status(409)
114 .json({ error: 'Cannot set "private" a video that was not private.' }) 116 .json({ error: 'Cannot set "private" a video that was not private.' })
115 .end() 117 .end()
116 } 118 }
117 119
118 if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return 120 if (req.body.channelId && !await isVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
119 121
120 return next() 122 return next()
121 } 123 }
@@ -221,36 +223,6 @@ const videosShareValidator = [
221 } 223 }
222] 224]
223 225
224// ---------------------------------------------------------------------------
225
226export {
227 videosAddValidator,
228 videosUpdateValidator,
229 videosGetValidator,
230 videosRemoveValidator,
231 videosShareValidator,
232
233 videoAbuseReportValidator,
234
235 videoRateValidator
236}
237
238// ---------------------------------------------------------------------------
239
240function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
241 if (req.body.scheduleUpdate) {
242 if (!req.body.scheduleUpdate.updateAt) {
243 res.status(400)
244 .json({ error: 'Schedule update at is mandatory.' })
245 .end()
246
247 return true
248 }
249 }
250
251 return false
252}
253
254function getCommonVideoAttributes () { 226function getCommonVideoAttributes () {
255 return [ 227 return [
256 body('thumbnailfile') 228 body('thumbnailfile')
@@ -317,3 +289,35 @@ function getCommonVideoAttributes () {
317 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy') 289 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
318 ] as (ValidationChain | express.Handler)[] 290 ] as (ValidationChain | express.Handler)[]
319} 291}
292
293// ---------------------------------------------------------------------------
294
295export {
296 videosAddValidator,
297 videosUpdateValidator,
298 videosGetValidator,
299 videosRemoveValidator,
300 videosShareValidator,
301
302 videoAbuseReportValidator,
303
304 videoRateValidator,
305
306 getCommonVideoAttributes
307}
308
309// ---------------------------------------------------------------------------
310
311function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
312 if (req.body.scheduleUpdate) {
313 if (!req.body.scheduleUpdate.updateAt) {
314 res.status(400)
315 .json({ error: 'Schedule update at is mandatory.' })
316 .end()
317
318 return true
319 }
320 }
321
322 return false
323}