]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-imports.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-imports.ts
1 import * as express from 'express'
2 import { body } from 'express-validator'
3 import { isPreImportVideoAccepted } from '@server/lib/moderation'
4 import { Hooks } from '@server/lib/plugins/hooks'
5 import { HttpStatusCode } from '@shared/models'
6 import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model'
7 import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc'
8 import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports'
9 import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos'
10 import { cleanUpReqFiles } from '../../../helpers/express-utils'
11 import { logger } from '../../../helpers/logger'
12 import { CONFIG } from '../../../initializers/config'
13 import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
14 import { areValidationErrors, doesVideoChannelOfAccountExist } from '../shared'
15 import { getCommonVideoEditAttributes } from './videos'
16
17 const videoImportAddValidator = getCommonVideoEditAttributes().concat([
18 body('channelId')
19 .customSanitizer(toIntOrNull)
20 .custom(isIdValid).withMessage('Should have correct video channel id'),
21 body('targetUrl')
22 .optional()
23 .custom(isVideoImportTargetUrlValid).withMessage('Should have a valid video import target URL'),
24 body('magnetUri')
25 .optional()
26 .custom(isVideoMagnetUriValid).withMessage('Should have a valid video magnet URI'),
27 body('torrentfile')
28 .custom((value, { req }) => isVideoImportTorrentFile(req.files))
29 .withMessage(
30 'This torrent file is not supported or too large. Please, make sure it is of the following type: ' +
31 CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_FILE.EXTNAME.join(', ')
32 ),
33 body('name')
34 .optional()
35 .custom(isVideoNameValid).withMessage(
36 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
37 ),
38
39 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
40 logger.debug('Checking videoImportAddValidator parameters', { parameters: req.body })
41
42 const user = res.locals.oauth.token.User
43 const torrentFile = req.files?.['torrentfile'] ? req.files['torrentfile'][0] : undefined
44
45 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
46
47 if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED !== true && req.body.targetUrl) {
48 cleanUpReqFiles(req)
49
50 return res.fail({
51 status: HttpStatusCode.CONFLICT_409,
52 message: 'HTTP import is not enabled on this instance.'
53 })
54 }
55
56 if (CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED !== true && (req.body.magnetUri || torrentFile)) {
57 cleanUpReqFiles(req)
58
59 return res.fail({
60 status: HttpStatusCode.CONFLICT_409,
61 message: 'Torrent/magnet URI import is not enabled on this instance.'
62 })
63 }
64
65 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
66
67 // Check we have at least 1 required param
68 if (!req.body.targetUrl && !req.body.magnetUri && !torrentFile) {
69 cleanUpReqFiles(req)
70
71 return res.fail({ message: 'Should have a magnetUri or a targetUrl or a torrent file.' })
72 }
73
74 if (!await isImportAccepted(req, res)) return cleanUpReqFiles(req)
75
76 return next()
77 }
78 ])
79
80 // ---------------------------------------------------------------------------
81
82 export {
83 videoImportAddValidator
84 }
85
86 // ---------------------------------------------------------------------------
87
88 async function isImportAccepted (req: express.Request, res: express.Response) {
89 const body: VideoImportCreate = req.body
90 const hookName = body.targetUrl
91 ? 'filter:api.video.pre-import-url.accept.result'
92 : 'filter:api.video.pre-import-torrent.accept.result'
93
94 // Check we accept this video
95 const acceptParameters = {
96 videoImportBody: body,
97 user: res.locals.oauth.token.User
98 }
99 const acceptedResult = await Hooks.wrapFun(
100 isPreImportVideoAccepted,
101 acceptParameters,
102 hookName
103 )
104
105 if (!acceptedResult || acceptedResult.accepted !== true) {
106 logger.info('Refused to import video.', { acceptedResult, acceptParameters })
107
108 res.fail({
109 status: HttpStatusCode.FORBIDDEN_403,
110 message: acceptedResult.errorMessage || 'Refused to import video'
111 })
112 return false
113 }
114
115 return true
116 }