aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/videos
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-05-14 11:10:26 +0200
committerGitHub <noreply@github.com>2020-05-14 11:10:26 +0200
commit2158ac90341dc3fcae958540de65032da25c8d6e (patch)
treea780923d701f3daa130996768e38c1e1b6a0646c /server/middlewares/validators/videos
parent7405b6ba897dbce1b4fd50c92174f1df5ac15adc (diff)
downloadPeerTube-2158ac90341dc3fcae958540de65032da25c8d6e.tar.gz
PeerTube-2158ac90341dc3fcae958540de65032da25c8d6e.tar.zst
PeerTube-2158ac90341dc3fcae958540de65032da25c8d6e.zip
Add server plugin filter hooks for import with torrent and url (#2621)
* Add server plugin filter hooks for import with torrent and url * WIP: pre and post-import filter hooks * Rebased * Cleanup filters to accept imports Co-authored-by: Chocobozzz <me@florianbigard.com>
Diffstat (limited to 'server/middlewares/validators/videos')
-rw-r--r--server/middlewares/validators/videos/video-imports.ts43
1 files changed, 38 insertions, 5 deletions
diff --git a/server/middlewares/validators/videos/video-imports.ts b/server/middlewares/validators/videos/video-imports.ts
index 5dc5db533..e3d900a9e 100644
--- a/server/middlewares/validators/videos/video-imports.ts
+++ b/server/middlewares/validators/videos/video-imports.ts
@@ -1,15 +1,18 @@
1import * as express from 'express' 1import * as express from 'express'
2import { body } from 'express-validator' 2import { body } from 'express-validator'
3import { isPreImportVideoAccepted } from '@server/lib/moderation'
4import { Hooks } from '@server/lib/plugins/hooks'
5import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model'
3import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' 6import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc'
4import { logger } from '../../../helpers/logger'
5import { areValidationErrors } from '../utils'
6import { getCommonVideoEditAttributes } from './videos'
7import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' 7import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
8import { cleanUpReqFiles } from '../../../helpers/express-utils'
9import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' 8import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
9import { cleanUpReqFiles } from '../../../helpers/express-utils'
10import { logger } from '../../../helpers/logger'
11import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares'
10import { CONFIG } from '../../../initializers/config' 12import { CONFIG } from '../../../initializers/config'
11import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' 13import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
12import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares' 14import { areValidationErrors } from '../utils'
15import { getCommonVideoEditAttributes } from './videos'
13 16
14const videoImportAddValidator = getCommonVideoEditAttributes().concat([ 17const videoImportAddValidator = getCommonVideoEditAttributes().concat([
15 body('channelId') 18 body('channelId')
@@ -64,6 +67,8 @@ const videoImportAddValidator = getCommonVideoEditAttributes().concat([
64 .end() 67 .end()
65 } 68 }
66 69
70 if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req)
71
67 return next() 72 return next()
68 } 73 }
69]) 74])
@@ -75,3 +80,31 @@ export {
75} 80}
76 81
77// --------------------------------------------------------------------------- 82// ---------------------------------------------------------------------------
83
84async function isImportAccepted (req: express.Request, res: express.Response) {
85 const body: VideoImportCreate = req.body
86 const hookName = body.targetUrl
87 ? 'filter:api.video.pre-import-url.accept.result'
88 : 'filter:api.video.pre-import-torrent.accept.result'
89
90 // Check we accept this video
91 const acceptParameters = {
92 videoImportBody: body,
93 user: res.locals.oauth.token.User
94 }
95 const acceptedResult = await Hooks.wrapFun(
96 isPreImportVideoAccepted,
97 acceptParameters,
98 hookName
99 )
100
101 if (!acceptedResult || acceptedResult.accepted !== true) {
102 logger.info('Refused to import video.', { acceptedResult, acceptParameters })
103 res.status(403)
104 .json({ error: acceptedResult.errorMessage || 'Refused to import video' })
105
106 return false
107 }
108
109 return true
110}