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