diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-05-14 11:10:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-14 11:10:26 +0200 |
commit | 2158ac90341dc3fcae958540de65032da25c8d6e (patch) | |
tree | a780923d701f3daa130996768e38c1e1b6a0646c /server/middlewares/validators | |
parent | 7405b6ba897dbce1b4fd50c92174f1df5ac15adc (diff) | |
download | PeerTube-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')
-rw-r--r-- | server/middlewares/validators/videos/video-imports.ts | 43 |
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body } from 'express-validator' | 2 | import { body } from 'express-validator' |
3 | import { isPreImportVideoAccepted } from '@server/lib/moderation' | ||
4 | import { Hooks } from '@server/lib/plugins/hooks' | ||
5 | import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model' | ||
3 | import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' | 6 | import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' |
4 | import { logger } from '../../../helpers/logger' | ||
5 | import { areValidationErrors } from '../utils' | ||
6 | import { getCommonVideoEditAttributes } from './videos' | ||
7 | import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' | 7 | import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' |
8 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | ||
9 | import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' | 8 | import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' |
9 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | ||
10 | import { logger } from '../../../helpers/logger' | ||
11 | import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares' | ||
10 | import { CONFIG } from '../../../initializers/config' | 12 | import { CONFIG } from '../../../initializers/config' |
11 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' | 13 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
12 | import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares' | 14 | import { areValidationErrors } from '../utils' |
15 | import { getCommonVideoEditAttributes } from './videos' | ||
13 | 16 | ||
14 | const videoImportAddValidator = getCommonVideoEditAttributes().concat([ | 17 | const 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 | |||
84 | async 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 | } | ||